Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
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.
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.
| API | Purpose |
|---|---|
| SVGDocument | Loads, edits, and saves SVG documents |
| SVGDocument.save() | Saves SVG content to a file, URL, stream, or resource handler |
| Url | Represents a URL or file URL used as a save target |
| FileSystemResourceHandler | Saves SVG and linked resources to local file storage |
| ResourceHandler | Base resource handler for controlling external resource output |
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)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)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.
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))| Problem | Likely cause | Fix |
|---|---|---|
| Saved file is not created | The output directory does not exist | Create the folder with os.makedirs(output_folder, exist_ok=True) before saving |
| Linked images or CSS are missing | The SVG references external resources that were not copied | Use FileSystemResourceHandler to save SVG with resources |
| Resources are not resolved when loading from a stream | The document has no base URI for relative links | Pass a valid base URI to SVGDocument(file_stream, base_uri) |
| Edits are not present in the saved SVG | save() was called before editing or outside the active document workflow | Modify the loaded document and call document.save() after edits |
| Conversion output was expected | SVGDocument.save() saves SVG, not PDF or raster images | Use Converter.convert_svg() to export SVG to PDF, PNG, JPG, and other formats |
Load or create an SVGDocument, edit it if needed, and call document.save(output_path).
No. SVGDocument.save() saves SVG. Use the conversion APIs when you need PDF, PNG, JPG, or another output format.
Use FileSystemResourceHandler so the SVG document and its linked resources are written to local storage.
The base URI tells the document how to resolve relative links to images, CSS, fonts, and other resources.
Yes. Load the source SVG, make changes, and pass a new output path to document.save().
save().Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.