Python to bulk SVG format into PNG PDF format code sharing

  • 2020-04-02 13:58:17
  • OfStack

Need to install cairosvg module, download address (link: http://cairosvg.org/download/)

Code:


#! encoding:UTF-8
import cairosvg
import os
 
loop = True
while loop:
    svgDir = raw_input(" Please enter the SVG File directory ")
    if os.path.exists(svgDir) and os.path.isdir(svgDir):
        loop = False
    else:
        print " Error: you entered SVG The file directory 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: the export directory you entered [" , exportDir  , "]  No, do you want to create this directory? "
        loops = True     
        while loops:
            msg = ""
            cmd = raw_input(" create  (Y)  again  (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 " Directory creation failed [",exportDir,"] .   Please re-enter "
            else:
                print " The command you entered could not be found, please retype "
             
 
cate = ("png", "pdf")
print " Export type: "
for i in cate:
    print i
     
loop = True
while loop:
    exportFormat = raw_input(" Please enter the export type ")
    if exportFormat.lower() in cate:
        loop = False
    else:
        print " The type you entered does not exist. Please enter again "
 
def export(fromDir, targetDir, exportType):
    print " Start executing the transition 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 " Has been derived  ", num, " A file "
export(svgDir, exportDir, exportFormat)

Use:


Please enter the SVG File directory d:svg
Please enter the export directory d:images
Error: the export directory you entered [ d:images ] No, do you want to create this directory?
create (Y) again (R)Y
Export type:
png
pdf
Please enter the export type png
Start executing the transition command ...
Success Export  png  ->  d:imagesa.png
Success Export  png  ->  d:imagesdb2.png
Success Export  png  ->  d:imagesdb3.png
Has been derived   3 A file


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 articles: