Convert SVG Files in Python

Quick Answer: Aspose.SVG for Python via .NET converts SVG to PDF, PNG, JPG, BMP, TIFF, GIF, and XPS. Use Converter.convert_svg() for direct file conversion, or load an SVGDocument and call render_to(device) when you need a rendering-device workflow.

SVG Conversion in Python

SVG is ideal for scalable graphics, but many workflows require fixed output formats. Use PDF for print and document exchange, PNG or JPG for web previews and raster assets, TIFF or BMP for image pipelines, GIF for simple raster output, and XPS for fixed-layout document workflows.

This page gives a short overview of the SVG conversion API. For detailed options and format-specific examples, use the linked articles below.

Supported Output Formats

Output formatTypical useDetailed article
PDFPrint, archiving, document exchangeConvert SVG to PDF
PNGWeb images, transparent raster output, previewsConvert SVG to PNG
JPGRaster previews without transparencyConvert SVG to JPG
BMPSimple bitmap outputConvert SVG to Image
TIFFMulti-purpose raster workflows and publishingConvert SVG to Image
GIFLightweight raster outputConvert SVG to Image
XPSFixed-layout document outputSee the XPS example below

Convert SVG Using Converter.convert_svg()

The Converter class is the easiest way to convert SVG files. Create save options for the target format, load the SVG document with SVGDocument, and call Converter.convert_svg().

1from aspose.svg import SVGDocument
2from aspose.svg.converters import Converter
3from aspose.svg.saving import ImageSaveOptions
4
5# Convert SVG to PNG using the high-level converter API
6options = ImageSaveOptions()
7
8with SVGDocument("image.svg") as document:
9    Converter.convert_svg(document, options, "image.png")

Convert SVG to XPS in Python

XPS is a niche fixed-layout output format. Use it when a Windows document, print, or archival workflow specifically requires XPS. For most document exchange scenarios, PDF is usually the better default.

1from aspose.svg import SVGDocument
2from aspose.svg.converters import Converter
3from aspose.svg.saving import XpsSaveOptions
4
5# Convert SVG to XPS when the target workflow requires XPS output
6options = XpsSaveOptions()
7
8with SVGDocument("document.svg") as document:
9    Converter.convert_svg(document, options, "document.xps")

Convert SVG Using render_to()

The render_to() method sends SVG content to a rendering device. Use this workflow when your application needs explicit control over a target device such as PdfDevice, XpsDevice, or ImageDevice.

 1import os
 2from aspose.svg import SVGDocument
 3from aspose.svg.rendering.pdf import PdfDevice, PdfRenderingOptions
 4
 5# Render SVG to PDF using a rendering device
 6input_folder = "data/"
 7output_folder = "output/"
 8input_path = os.path.join(input_folder, "document.svg")
 9output_path = os.path.join(output_folder, "document.pdf")
10os.makedirs(output_folder, exist_ok=True)
11
12options = PdfRenderingOptions()
13options.jpeg_quality = 80
14
15with SVGDocument(input_path) as document:
16    with PdfDevice(options, output_path) as device:
17        document.render_to(device)

Conversion Options

Most SVG conversion tasks use save options or rendering options to control output quality, page setup, background color, and resource handling.

Option groupUsed withExamples
ImageSaveOptionsPNG, JPG, BMP, TIFF, GIF outputImage format, background color, page setup, resolution
PdfSaveOptionsPDF output through Converter.convert_svg()Page setup, JPEG quality, metadata, encryption, background color
XpsSaveOptionsXPS output through Converter.convert_svg()Page setup, background color, resolution
PdfRenderingOptionsPDF output through PdfDevicePage setup, JPEG quality, resolution, background color
RenderingOptionsDevice-based rendering workflowsCommon rendering settings inherited by format-specific options

Batch Conversion

Batch conversion uses the same APIs as single-file conversion. Iterate through a folder of SVG files, create an output path for each file, and call Converter.convert_svg() for every document. See Batch Convert SVG Files in Python for PNG, JPG, and PDF examples.

Online SVG Converter

Use the free online SVG Converter to test SVG conversion before implementing it in Python. Upload an SVG file, select an output format, and compare the generated file.

                
            

Online SVG converter for PDF, PNG, JPG, BMP, TIFF, GIF, and XPS

Common Mistakes and Fixes

ProblemLikely causeFix
Output file is missingThe output folder does not exist or the file was saved in another working directoryCreate the folder with os.makedirs() and use explicit paths
Converted image has the wrong backgroundThe SVG contains transparent areas and no background was configuredSet the background color in the save or rendering options
PDF or image size is unexpectedPage setup does not match the SVG viewport or content boundsConfigure page size, margins, or sizing mode in the relevant options object
External images or fonts are missingRelative resources cannot be resolved during conversionKeep resources next to the SVG or use correct absolute paths/base paths
Output is too largeThe SVG is complex or raster settings are too highReduce resolution, JPEG quality, image size, or simplify the SVG before conversion

FAQ

How do I convert SVG files in Python?
Use SVGDocument to load the file, create save options for the target format, and call Converter.convert_svg().

Which SVG output formats are supported?
Aspose.SVG for Python via .NET supports conversion to PDF, XPS, PNG, JPG, BMP, TIFF, and GIF.

Should I use Converter.convert_svg() or render_to()?
Use Converter.convert_svg() for ordinary file conversion. Use render_to() when you need a rendering-device workflow with PdfDevice, XpsDevice, or ImageDevice.

Can I convert SVG from a URL?
Yes. Load the SVG URL with SVGDocument when network access is available, then convert it with the same API used for local files.

Can I convert SVG to XPS in Python?
Yes. Create XpsSaveOptions and pass it with the loaded SVGDocument to Converter.convert_svg(). Use XPS only when your target workflow requires that format.

Can I batch convert multiple SVG files?
Yes. Loop through the input folder, load each .svg file with SVGDocument, and call Converter.convert_svg() for each output file.

Can I change the output size during conversion?
Yes. Configure page setup, sizing, margins, and resolution through the target format’s save or rendering options.

Related Articles