Python enables bulk transcoding of SVG formats into PNG, PDF-style code sharing

Source: Internet
Author: User
Tags create directory
Need to install Cairosvg module in advance, download address http://cairosvg.org/download/

Code:

#! Encoding:utf-8import cairosvgimport os loop = Truewhile Loop:svgdir = raw_input ("Please enter SVG file directory") if Os.path.exists (SVG Dir) 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 export directory") if Os.path.exists (exportdir): loop = False Else:print "Error: The export directory you entered [", Exportdir, "] does not exist, do you want to create this directory?            "loops = True while loops:msg =" "cmd = Raw_input (" Create (Y) Re (R) ") If cmd.upper () = = "R": Loops = False elif cmd.upper () = = "Y": Os.makedirs (EXPORTD                IR, True) if Os.path.exists (exportdir): loop = false loops = False Else:print "failed to create directory [", Exportdir, "], please reenter" else:print "cannot find the life you entered Please reenter "cate = (" png "," PDF ") print" Export type: "For I in cate:print i loop = TrUewhile 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 to execute conversion command ..." files = Os.listdir (fromdi r) num = 0 for fileName in Files:path = Os.path.join (fromdir,filename) if Os.path.isfile (path) and fi            Lename[-3:] = = "svg": num + = 1 filehandle = open (path) SVG = Filehandle.read () Filehandle.close () Exportpath = Os.path.join (TargetDir, filename[:-3] + exporttype) Exportfilehandl E = open (Exportpath, ' W ') if Exporttype = = "png": Cairosvg.svg2png (Bytestring=svg, W Rite_to=exportpath) elif Exporttype = = "pdf": Cairosvg.svg2pdf (Bytestring=svg, WRITE_TO=EXPORTPA TH) exportfilehandle.close () print "Success Export", Exporttype, "and", Expor Tpath print "Exported ", num," Files "Export (Svgdir, Exportdir, ExportFormat) 

Use:
Copy the 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 to execute 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 have been exported

Copy the 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
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.