Save SVG File in Python

Quick Answer

Use SVGDocument.save(output_path) to save an SVG document to a file. If the SVG references external resources such as images or CSS, use FileSystemResourceHandler to save the SVG together with its linked resources in local storage.

Save SVG Documents

Most SVG workflows end with saving a document. After creating a new SVG, loading an existing file, or editing the SVG DOM, call document.save() to write the result. Saving keeps the output in SVG format. To export SVG to PDF, PNG, JPG, or other formats, use the conversion APIs described in Convert SVG Files in Python.

Before running the examples, install Aspose.SVG for Python via .NET in your Python environment.

Aspose.SVG APIs Used

APIPurpose
SVGDocumentLoads, edits, and saves SVG documents
SVGDocument.save()Saves SVG content to a file, URL, stream, or resource handler
UrlRepresents a URL or file URL used as a save target
FileSystemResourceHandlerSaves SVG and linked resources to local file storage
ResourceHandlerBase resource handler for controlling external resource output

Save SVG to a File

The following example loads an SVG file, modifies the root element, and saves the result to another file.

 1import os
 2from aspose.svg import SVGDocument
 3
 4# Load an SVG file, edit it, and save the result
 5input_folder = "data/"
 6output_folder = "output/"
 7input_path = os.path.join(input_folder, "with-resources.svg")
 8output_path = os.path.join(output_folder, "modified-example.svg")
 9os.makedirs(output_folder, exist_ok=True)
10
11with SVGDocument(input_path) as document:
12    document.document_element.set_attribute("data-edited", "true")
13    document.save(output_path)

Save SVG to a URL Path

You can also pass a Url object to save(). This is useful when your code works with URL-based paths rather than plain file-system strings.

 1import os
 2from aspose.svg import SVGDocument, Url
 3
 4# Save an SVG document to a URL-based file path
 5input_folder = "data/"
 6output_folder = "output/"
 7os.makedirs(output_folder, exist_ok=True)
 8
 9input_path = os.path.join(input_folder, "text.svg")
10output_url = Url(os.path.join(output_folder, "text-out.svg"), os.getcwd())
11
12with SVGDocument(input_path) as document:
13    document.save(output_url)

Save SVG with Linked Resources

An SVG document can reference external resources such as images, CSS files, or fonts. Use FileSystemResourceHandler when you want to save the SVG and its linked resources into local storage.

The following example saves with-resources.svg and associated resources to the output folder.

 1import os
 2from aspose.svg import SVGDocument
 3from aspose.svg.saving.resourcehandlers import FileSystemResourceHandler
 4
 5# Save an SVG document and its linked resources to local storage
 6input_folder = "data/"
 7output_folder = os.path.abspath(os.path.join("output", "with-resources"))
 8input_path = os.path.join(input_folder, "with-resources.svg")
 9os.makedirs(output_folder, exist_ok=True)
10
11with SVGDocument(input_path) as document:
12    document.save(FileSystemResourceHandler(output_folder))

After running the code, check the output folder. It should contain the saved SVG document and the resources that were included by the original SVG.

Save SVG from a Stream with Resources

If the SVG file is opened as a stream, pass a base URI when creating SVGDocument so relative resources can be resolved. Then save the document and resources explicitly.

 1import os
 2from aspose.svg import SVGDocument
 3from aspose.svg.saving.resourcehandlers import FileSystemResourceHandler
 4
 5# Load SVG from a stream and save it with linked resources
 6input_folder = "data/"
 7output_folder = os.path.abspath(os.path.join("output", "stream-save"))
 8input_path = os.path.join(input_folder, "with-resources.svg")
 9output_path = os.path.join(output_folder, "with-resources.svg")
10os.makedirs(output_folder, exist_ok=True)
11
12with open(input_path, "rb") as file_stream:
13    with SVGDocument(file_stream, ".") as document:
14        document.save(output_path, FileSystemResourceHandler(output_folder))

Common Mistakes and Fixes

ProblemLikely causeFix
Saved file is not createdThe output directory does not existCreate the folder with os.makedirs(output_folder, exist_ok=True) before saving
Linked images or CSS are missingThe SVG references external resources that were not copiedUse FileSystemResourceHandler to save SVG with resources
Resources are not resolved when loading from a streamThe document has no base URI for relative linksPass a valid base URI to SVGDocument(file_stream, base_uri)
Edits are not present in the saved SVGsave() was called before editing or outside the active document workflowModify the loaded document and call document.save() after edits
Conversion output was expectedSVGDocument.save() saves SVG, not PDF or raster imagesUse Converter.convert_svg() to export SVG to PDF, PNG, JPG, and other formats

FAQ

How do I save an SVG file in Python?

Load or create an SVGDocument, edit it if needed, and call document.save(output_path).

Does SVGDocument.save() convert SVG to PNG or PDF?

No. SVGDocument.save() saves SVG. Use the conversion APIs when you need PDF, PNG, JPG, or another output format.

How do I save SVG with external images?

Use FileSystemResourceHandler so the SVG document and its linked resources are written to local storage.

Why do I need a base URI when loading SVG from a stream?

The base URI tells the document how to resolve relative links to images, CSS, fonts, and other resources.

Can I save an edited SVG to a different file?

Yes. Load the source SVG, make changes, and pass a new output path to document.save().

Related Articles