Need to install Cairosvg module in advance, download address http://cairosvg.org/download/
Code:
#! Encoding:utf-8 Import cairosvg import os loop = True while loop: svgdir = raw_input ("Please enter SVG File directory ") if os.path.exists (Svgdir) and Os.path.isdir (Svgdir): loop = False else: print "Error: The SVG file directory you entered does not exist or is not a valid directory, please re-enter" loop = True while loop: exportdir = Raw_input (" Please enter the Export directory ") if os.path.exists (exportdir): Loop = False else: print "error: Export directory you entered [", exportdir , "] does not exist, do you want to create this directory? " loops = true while Loops: msg = "" &nbsP; cmd = Raw_input (Create (Y) Re (R)) if cmd.upper () = = "R": loops = False elif cmd.upper () = = "Y": os.makedirs (Exportdir, True) if os.path.exists (exportdir): loop = False Loops = False else: print "Create directory failed [, Exportdir,"], please re-enter " else: Print "Could not find the command you entered, please re-enter" Cate = ("png", "PDF") print "Export type:" For I in Cate: print i loop = True W Hile loop: exportformat = raw_input ("Please enter export type") if ExportFormat.lower () in cate: loop = False else: print "The type you entered does not exist, please re-enter" DEF export (Fromdir, TargetDir, Exporttype): print "Start execution of conversion commands ..." files = Os.listdir (fromdir) &Nbsp; num = 0 for fileName in Files: path = Os.path.join (fromdir,filename) if Os.path.isfile (Path) and filename[-3:] = = "svg":
num + + 1 filehandle = open (path) svg = FileHandle.read () filehandle.close () exportpath = Os.path.join (TargetDir, filename[:-3] + exporttype) exportfilehandle = open (ExportPath, ' W ') &nBsp; if Exporttype = = "png": cairosvg.svg2png (Bytestring=svg, Write_to=exportpath) elif Exporttype = = "PDF": cairosvg.svg2pdf (Bytestring=svg, write_to= Exportpath) exportfilehandle.close () print "Success Export", Exporttype, "->", Exportpath print "exported", num, "Files" Export (Svgdir, Exportdir, ExportFormat)
Use:
Copy Code code as follows:
Please enter the SVG file directory D:\svg
Please enter the export directory D:\Images
Error: The export directory you entered [D:\Images] does not exist, do you want to create this directory?
Create (Y) re-(R) y
Export Type:
Png
Pdf
Please enter the export type png
Start Executing conversion command ...
Success Export PNG-> d:\images\a.png
Success Export PNG-> d:\images\db2.png
Success Export PNG-> d:\images\db3.png
3 Files Exported
Copy Code code as follows:
D:\>tree svg/f
Folder PATH Listing
Volume Serial number is 4603-09B2
D:\SVG
A.svg
Db2.svg
Db3.svg
No subfolders Exist
D:\>tree images/f
Folder PATH Listing
Volume Serial number is 4603-09B2
D:\IMAGES
A.png
Db2.png
Db3.png
No subfolders Exist