Python implements batch conversion of SVG format to PNG, PDF code sharing _python

Source: Internet
Author: User
Tags create directory db2

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

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.