Create SVG File, Load and Read SVG in Python
How to Create SVG File
To perform any task, you must create or load a document. Aspose.SVG for Python via .NET API enables you to construct an SVG document from scratch or load an existing SVG from different sources. The API provides the
SVGDocument class, which has several constructors allowing you to create new instances. 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.
This article provides some examples of creating or loading SVG files using Aspose.SVG for Python via .NET API. The SVGDocument
class is central to creating and manipulating SVG documents, supporting both creation from scratch and loading from existing sources. It has a wide set of constructors allowing you to create a blank document or load it from a file, URL, string, etc.
To continue following this tutorial, you should install and configure Aspose.SVG for Python via .NET library in your Python project. Our code examples help you to create, load, and read SVG files using the Python library.
Create an Empty SVG Document
Aspose.SVG for Python via .NET API provides the
SVGDocument class that can be used to create an empty document. Once the document object is created, it can be filled later with SVG elements. The following Python code snippet shows the usage of the default SVGDocument()
constructor to create an SVG document.
If you want to save created empty SVG document to a file, use the following Python code snippet:
1from aspose.svg import *
2
3# Create a new SVG document
4document = SVGDocument()
5
6# Save the empty SVG document
7document.save("empty-document.svg")
Creating an empty SVG document in Python using Aspose.SVG for Python via .NET is straightforward. Below is an example code snippet that demonstrates how to create an empty SVG document and save it to a file in the specified output directory. The example shows how you can set attributes such as width
and height
for the root <svg>
element:
1import os
2from aspose.svg import *
3
4# Set up the output directory
5output_folder = "output/"
6if not os.path.exists(output_folder):
7 os.makedirs(output_folder)
8
9# Create a new empty SVG document
10document = SVGDocument()
11
12# Optionally, you can add attributes to the root <svg> element
13svg_element = document.document_element
14svg_element.set_attribute("width", "100%")
15svg_element.set_attribute("height", "100%")
16
17# Define the output file path
18output_file = os.path.join(output_folder, "create-empty.svg")
19
20# Save the SVG document to a file
21document.save(output_file)
22
23print(f"Empty SVG document saved to {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 Python 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.
Create SVG from a Memory String
You can create SVG from a string content using SVGDocument(content, base_uri)
constructor. If your case is to produce a document from a user string directly in your code and you don’t need to save it to a file, the following example could help you: we produce an SVG document that contains a color-filled circle with a radius of 50 pixels and colored stroke.
1from aspose.svg import *
2
3documentContent = "<svg xmlns='http://www.w3.org/2000/svg'><circle cx='100' cy='150' r='50' stroke='#2F4F4F' stroke-width='4' fill='#FF7F50' /></svg>";
4
5# Create a new SVG document
6document = SVGDocument(documentContent, ".")
7svg_element = document.document_element
8
9# Work with the document here...
10
11# Save the document
12document.save("create-svg-from-string.svg")
Load SVG from a File
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 *
2
3# Create a new SVG document
4document = SVGDocument("document.svg")
5
6# Work with the SVG document here...
7
8# Save the document
9document.save("load-svg-from-file.svg")
Load SVG from a URL
The following Python code example could help you to create a document from a URL referring to the SVG file:
1from aspose.svg import *
2
3# Load SVG from a URL
4document = SVGDocument("https://docs.aspose.com/svg/files/owl.svg")
5
6# Work with the SVG document here...
7
8# Save the document
9document.save("load-svg-from-url.svg")
See Also
- The Installation article provides a step-by-step guide on how to install Aspose.SVG for Python via .NET on your computer.
- If you are interested in setting up a license, applying a metered license, or would like to try an evaluation version for the Aspose.SVG Python library, please refer to the Licensing article.