Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Quick Answer: Use SVGDocument() to create a blank SVG, SVGDocument(svg_content, base_uri) to parse SVG markup from a string, or SVGDocument(path_or_url) to load an existing file. After editing the SVG DOM, call document.save(output_path) to write the result.
This article explains the fundamental SVGDocument workflows used as a starting point for all SVG processing tasks.
Aspose.SVG for Python via .NET provides the
SVGDocument class, the entry point for creating, loading, and reading SVG content. The SVGDocument serves as the root of the SVG DOM hierarchy, holding the entire content and adhering to
W3C SVG 2.0 and
WHATWG DOM specifications.
The SVGDocument class supports multiple constructors, allowing you to:
Before running the examples, install Aspose.SVG for Python via .NET in your Python environment.
| API | Purpose |
|---|---|
| SVGDocument | Creates, parses, and loads SVG documents |
| document_element | Provides access to the root <svg> element |
| Element.set_attribute() | Sets SVG attributes such as width, height, and viewBox |
| SVGDocument.save() | Saves the SVG document to a file |
A blank document is useful as a canvas for adding SVG elements programmatically. The following example creates a document, sets its viewport attributes, and saves it to the output directory. The saved file is blank until you add a visible shape, text, or image.
1import os
2from aspose.svg import SVGDocument
3
4# Create a blank SVG document with a defined viewport
5output_folder = "output/"
6os.makedirs(output_folder, exist_ok=True)
7output_file = os.path.join(output_folder, "create-empty.svg")
8
9with SVGDocument() as document:
10 svg_element = document.document_element
11 svg_element.set_attribute("width", "300")
12 svg_element.set_attribute("height", "200")
13 svg_element.set_attribute("viewBox", "0 0 300 200")
14 document.save(output_file)More details about SVG file saving are in the Save SVG File article. In the article Navigate SVG, You learn how to use the Aspose.SVG for Python via .NET library to perform a detailed inspection of the SVG document and its elements and how to navigate the SVG document using CSS Selector or XPath.
Use the SVGDocument(content, base_uri) constructor when SVG markup is generated in memory. The base URI is required to resolve relative images, stylesheets, fonts, or links. This example creates an SVG containing a filled circle and saves it as create-svg-from-string.svg.
1from aspose.svg import SVGDocument
2
3svg_content = "<svg xmlns='http://www.w3.org/2000/svg' width='200' height='200' viewBox='0 0 200 200'><circle cx='100' cy='100' r='50' stroke='#2F4F4F' stroke-width='4' fill='#FF7F50' /></svg>"
4
5# Create SVG from markup stored in memory
6with SVGDocument(svg_content, ".") as document:
7 document.save("create-svg-from-string.svg")The output is a 200 × 200 SVG with a coral circle centered in the viewport.
To load SVG from a file, use one of the constructors of the SVGDocument class and pass the file path as the input parameter to it.
1from aspose.svg import SVGDocument
2
3# Load an SVG file located on disk and save a copy
4with SVGDocument("document.svg") as document:
5 document.save("load-svg-from-file.svg")Loading from remote URLs requires network access and may fail in restricted or offline environments. The following Python code example could help you to create a document from a URL referring to the SVG file:
1from aspose.svg import SVGDocument
2
3# Load SVG from a URL and save it locally
4with SVGDocument("https://docs.aspose.com/svg/files/owl.svg") as document:
5 document.save("load-svg-from-url.svg")| Problem | Cause | Solution |
|---|---|---|
| Saved SVG opens as an empty page | The example creates a valid SVG document but does not add visible shapes, or the root <svg> has no viewport. | Add graphical elements such as <circle>, <rect>, or <path>, and set width, height, or viewBox on the root <svg> element. |
| Images, fonts, or CSS are missing after parsing SVG text | The SVG markup contains relative links, but the base_uri argument points to the wrong folder or is omitted. | Pass a base URI that matches the location of linked resources when using SVGDocument(svg_content, base_uri). |
| Output file is not created | The target directory does not exist, or the script saves the file to a different working directory than expected. | Create the output folder before calling document.save() and build paths with os.path.join() for predictable file locations. |
| Loading SVG from a URL fails | The environment has no network access, the URL is blocked, or the remote SVG file is unavailable. | Test the URL in a browser or use a local SVG file in offline and CI environments. |
Document cannot be used after the with block | The context manager releases the SVGDocument resources when the block ends. | Read or modify the SVG and call document.save() inside the with SVGDocument(...) as document: block. |
Can I modify an SVG loaded from a URL without downloading it manually?
Yes. When you pass a URL to the SVGDocument constructor, the library loads the SVG content into memory automatically. You can then modify the document and save it locally.
Does the SVGDocument constructor automatically create the root <svg> element?
Yes. The default SVGDocument() constructor creates a valid SVG document with a root <svg> element accessible via document.document_element.
How should I release resources used by SVGDocument?
Use SVGDocument as a context manager with with. This releases its resources deterministically when the block ends.
Why should I specify a base URI when creating SVG from a string?
The base URI defines how relative resources such as images, stylesheets, or fonts referenced inside the SVG are resolved. Without it, external resources may fail to load correctly.
How can I embed external images directly into an SVG file?
A base URI only helps resolve resource paths. To embed images directly into an SVG, convert them to Base64 data URIs and assign them to the corresponding image attributes.
Aspose.SVG for Python via .NET lets you create, load, inspect, modify, and save SVG through one SVGDocument API. Choose the constructor that matches the source, provide a base URI when parsing markup with relative resources, and use a context manager to release resources reliably.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.