Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Quick Answer: To batch convert SVG files in Python, iterate through an input folder, load each .svg file with
SVGDocument, create save options for the target format, and call
Converter.convert_svg() for every output file.
Batch conversion is useful when your application needs thumbnails, web previews, exported report graphics, or PDF copies for a folder of SVG files. The workflow is the same as single-file conversion, but you repeat it for every source file:
.svg files.Before running the examples, install Aspose.SVG for Python via .NET in your Python environment.
PNG is a good default for web previews and transparent raster output. The following example converts every .svg file in data/svg/ to a .png file in output/png/.
1import os
2from aspose.svg import SVGDocument
3from aspose.svg.converters import Converter
4from aspose.svg.saving import ImageSaveOptions
5
6# Batch convert SVG files to PNG
7input_folder = "data/svg/"
8output_folder = "output/png/"
9os.makedirs(output_folder, exist_ok=True)
10
11options = ImageSaveOptions()
12
13for file_name in os.listdir(input_folder):
14 if not file_name.lower().endswith(".svg"):
15 continue
16
17 input_path = os.path.join(input_folder, file_name)
18 output_name = os.path.splitext(file_name)[0] + ".png"
19 output_path = os.path.join(output_folder, output_name)
20
21 with SVGDocument(input_path) as document:
22 Converter.convert_svg(document, options, output_path)When converting to JPG, set ImageFormat.JPEG and choose a background color because JPG does not support transparency.
1import os
2from aspose.svg import SVGDocument
3from aspose.svg.converters import Converter
4from aspose.svg.rendering.image import ImageFormat
5from aspose.svg.saving import ImageSaveOptions
6from aspose.pydrawing import Color
7
8# Batch convert SVG files to JPG with a solid background
9input_folder = "data/svg/"
10output_folder = "output/jpg/"
11os.makedirs(output_folder, exist_ok=True)
12
13options = ImageSaveOptions()
14options.format = ImageFormat.JPEG
15options.background_color = Color.from_argb(255, 255, 255)
16
17for file_name in os.listdir(input_folder):
18 if not file_name.lower().endswith(".svg"):
19 continue
20
21 input_path = os.path.join(input_folder, file_name)
22 output_name = os.path.splitext(file_name)[0] + ".jpg"
23 output_path = os.path.join(output_folder, output_name)
24
25 with SVGDocument(input_path) as document:
26 Converter.convert_svg(document, options, output_path)Use PdfSaveOptions when each SVG file should become a separate PDF file.
1import os
2from aspose.svg import SVGDocument
3from aspose.svg.converters import Converter
4from aspose.svg.saving import PdfSaveOptions
5
6# Batch convert SVG files to separate PDF files
7input_folder = "data/svg/"
8output_folder = "output/pdf/"
9os.makedirs(output_folder, exist_ok=True)
10
11options = PdfSaveOptions()
12
13for file_name in os.listdir(input_folder):
14 if not file_name.lower().endswith(".svg"):
15 continue
16
17 input_path = os.path.join(input_folder, file_name)
18 output_name = os.path.splitext(file_name)[0] + ".pdf"
19 output_path = os.path.join(output_folder, output_name)
20
21 with SVGDocument(input_path) as document:
22 Converter.convert_svg(document, options, output_path)The previous examples repeat the same folder loop for PNG, JPG, and PDF. In application code, it is usually cleaner to keep that loop in one helper function and pass only the parts that change: the output folder, output extension, and save options.
Use this pattern when your project supports several export formats and you do not want to maintain three separate loops. The helper below still creates one output file per SVG; it only removes repeated code.
1import os
2from aspose.svg import SVGDocument
3from aspose.svg.converters import Converter
4from aspose.svg.rendering.image import ImageFormat
5from aspose.svg.saving import ImageSaveOptions, PdfSaveOptions
6from aspose.pydrawing import Color
7
8def batch_convert_svg(input_folder, output_folder, output_extension, options):
9 os.makedirs(output_folder, exist_ok=True)
10
11 for file_name in os.listdir(input_folder):
12 if not file_name.lower().endswith(".svg"):
13 continue
14
15 input_path = os.path.join(input_folder, file_name)
16 output_name = os.path.splitext(file_name)[0] + output_extension
17 output_path = os.path.join(output_folder, output_name)
18
19 with SVGDocument(input_path) as document:
20 Converter.convert_svg(document, options, output_path)
21
22input_folder = "data/svg/"
23
24# Reuse the same helper for PNG output
25png_options = ImageSaveOptions()
26batch_convert_svg(input_folder, "output/png/", ".png", png_options)
27
28# Reuse the same helper for JPG output
29jpg_options = ImageSaveOptions()
30jpg_options.format = ImageFormat.JPEG
31jpg_options.background_color = Color.from_argb(255, 255, 255)
32batch_convert_svg(input_folder, "output/jpg/", ".jpg", jpg_options)
33
34# Reuse the same helper for PDF output
35pdf_options = PdfSaveOptions()
36batch_convert_svg(input_folder, "output/pdf/", ".pdf", pdf_options)| Problem | Likely cause | Fix |
|---|---|---|
| No files are converted | The script is looking in the wrong input folder or filtering the wrong extension | Print input_folder and check that it contains .svg files |
| Output files overwrite each other | Different input files produce the same base output name | Preserve subfolders or add a suffix to generated output names |
| Linked images or CSS are missing | Relative resources are not available next to the source SVG | Load each SVG from its original file path and keep resources in the expected relative locations |
| JPG output has an unexpected background | JPG does not support transparency | Set ImageSaveOptions.background_color |
| One bad SVG stops the whole batch | Exceptions are not handled per file | Wrap each conversion in try/except and log failed file names |
Loop through an input folder, load each .svg file with SVGDocument, and call Converter.convert_svg() with the required save options.
Yes. Use ImageSaveOptions, set options.format = ImageFormat.JPEG, set a background color, and save each output file with a .jpg extension.
Yes. Use PdfSaveOptions and write each converted document to a .pdf output path.
Yes. Use recursive folder traversal in your Python code and create matching output folders before saving the converted files.
This batch workflow creates one output file per SVG. If you need one combined multi-page PDF, use the SvgRenderer class with a PdfDevice output device; the renderer can send multiple SVG documents to the same PDF device.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.