SoFunction
Updated on 2025-03-01

Python implements code sharing in batches converting SVG formats into png and pdf formats

Need to install cairosvg module in advance, download address/download/

Code:

#! encoding:UTF-8
import cairosvg
import os
 
loop = True
while loop:
    svgDir = raw_input("Please enter the SVG file directory")
    if (svgDir) and (svgDir):
        loop = False
    else:
        print "Error: The directory of the SVG file 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 (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) again (R)")
            if () == "R":
                loops = False
            elif () == "Y":
                (exportDir, True)
                if (exportDir):
                    loop = False
                    loops = False
                else:
                    print "Create directory failed[",exportDir,"], please re-enter"
            else:
                print "The command you entered cannot be found, please re-enter"
             
 
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 () in cate:
        loop = False
    else:
        print "The type you entered does not exist, please re-enter"
 
def export(fromDir, targetDir, exportType):
    print "Start the conversion command..."
    files = (fromDir)
    num = 0
    for fileName in files:
        path = (fromDir,fileName)
        if (path) and fileName[-3:] == "svg":
            num += 1
            fileHandle = open(path)
            svg = ()
            ()
            exportPath = (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)
                 
            ()
            print "Success Export ", exportType, " -> " , exportPath
     
    print "Exported", num, "A file"
export(svgDir, exportDir, exportFormat)

use:

Copy the codeThe code is as follows:

Please enter the SVG file directory d:\svg
Please enter the export directory d:\images
Error: The export directory [d:\images] you entered 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 the conversion command...
Success Export  png  ->  d:\images\
Success Export  png  ->  d:\images\
Success Export  png  ->  d:\images\
3 files have been exported

Copy the codeThe code is as follows:

D:\>tree svg /F
Folder PATH listing
Volume serial number is 4603-09B2
D:\SVG
    
    
    
 
No subfolders exist
 
 
D:\>tree images /F
Folder PATH listing
Volume serial number is 4603-09B2
D:\IMAGES
    
    
    
 
No subfolders exist