Python implements bulk conversion of SVG format into png, pdf format code sharing, pythonsvg

Source: Internet
Author: User

Python implements bulk conversion of SVG format into png, pdf format code sharing, pythonsvg

Need to install cairosvg module in advance, http://cairosvg.org/download/

Code:

#! Encoding: UTF-8import cairosvgimport OS loop = Truewhile loop: svgDir = raw_input ("Enter the 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 enter" loop = Truewhile loop: exportDir = raw_input ("Enter the export directory") if OS. path. exists (exportDir): loop = False else: print "error: the exported directory [", 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 (exportDir, True) if OS. path. exists (exportDir): loop = False loops = False else: print "failed to create directory [", exportDir, "]. Please enter" else: print "the command you entered cannot be found. Enter" cate = ("png", "pdf") print "Export type:" for I in cate: print I loop = Truewhile loop: exportFormat = raw_input ("Enter the export type") if exportFormat. lower () in cate: loop = False else: print "the type you entered does not exist. Please enter" def export (fromDir, targetDir, exportType) again ): print "start to execute the conversion command... "files = OS. listdir (fromDir) 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') 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)

Usage:
Copy codeThe Code is as follows:
Enter the SVG file directory d: \ svg
Enter the export Directory d: \ images
Error: the exported directory [d: \ images] does not exist. Do you want to create this directory?
Create (Y) Re (R) Y
Export type:
Png
Pdf
Enter the export type png.
Start to execute the 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 codeThe Code is 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


Software that converts svg to png in batches

Find a software called ConversionSVG that can be converted in batches. However, Inkscape must be installed in the system. In addition, the interface of the software is not Chinese or English, and it seems to be French, but it should not affect the use, which is simple and clear at a glance.

Home page:
Sourceforge.net/projects/conversionsvg/

Download directly:
Sourceforge.net/projects/conversionsvg/files/conversionsvg/1.0.0.0/Installation_ConversionSVG_1.0.0.0.exe/download

In addition, you can also refer to this (you need to operate in Linux ):
Wiki.ubuntu.org.cn/...bapng
Reference: wiki.ubuntu.org.cn/....BApng

How can we convert the svg format to images in other formats such as png?

A large number of image format conversion software on the Internet

All-Around format conversion (Konvertor) v3.70 Beta 1
Wt.crsky.com/soft/976.html

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.