Save SVG File – Aspose.SVG for Python via .NET

Save an SVG document

Most of the tasks, you need to perform, require saving a document. Once you’ve loaded an existing SVG file or created a new SVG document from scratch, you can save your changes using one of the SVGDocument.save() methods. These methods allow you to save the SVG document to various destinations, including files, URLs, or local file storage.

In this article, we review the SVG documents saving to the same format. You find out how to save your SVG file using save() methods of the SVGDocument class. Moreover, Aspose.SVG for Python via .NET provides the ResourceHandler class that allows save SVG documents with resources to local file storage and manage them.

The scenarios of converting and rendering SVG to other formats are viewed in the Convert SVG Files in Python chapter.

Save SVG to a File

The following Python code snippet demonstrates the use of the SVGDocument.save() method to save an SVG document to a file:

 1import os
 2from aspose.svg import *
 3
 4# Prepare a path to the source and output SVG file
 5data_dir = "data/"
 6output_dir = "output/"
 7input_path = os.path.join(data_dir, "with-resources.svg")
 8output_path = os.path.join(output_dir, "modified_example.svg")
 9if not os.path.exists(output_dir):
10    os.makedirs(output_dir)
11
12# Load the existing SVG document from a file
13with SVGDocument(input_path) as document:
14
15    # Work with the document here
16
17    # Save the modified SVG document to a file
18    document.save(output_path)

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.

Save SVG to a Url

It is necessary to specify a full Url path for the document saving and pass the url object to the save() method that saves the document to a file specified by Url. The following code example shows how to save an SVG document to a Url:

 1import os
 2from aspose.svg import *
 3
 4# Setup directories
 5data_dir = "data/"
 6output_dir = "output/"
 7if not os.path.exists(output_dir):
 8    os.makedirs(output_dir)
 9
10# Set a full path for saving an SVG document
11url = Url(os.path.join(output_dir, "text_out.svg"), os.getcwd())
12
13# Load the SVG document from a file
14document_path = os.path.join(data_dir, "text.svg")
15with SVGDocument(document_path) as document:
16
17    # Work with the document here
18
19    # Save the SVG document to the specified URL
20    document.save(url)

Save SVG to a Local File System Storage

The SVG document can contain different resources like CSS, external images and files. Aspose.SVG provides a way to save SVG with all linked files – the ResourceHandler class is developed for saving SVG content and resources to streams. This class is responsible for handling resources and provides methods that allow you to control what is done with each resource.

Let’s consider an example of saving SVG with resourses to user-specified local file storage. The source with-resources.svg document and its linked image file are in the same directory. The FileSystemResourceHandler class has constructors that take a path or url indicating where the document with resources will be saved and creates a FileSystemResourceHandler object. The save(resource_handler) method takes this object and saves SVG to the output storage.

This Python code uses the Aspose.SVG Python library to load an existing SVG file and save it, including any associated resources, to a specified output directory.

 1import os
 2from aspose.svg import *
 3from aspose.svg.saving.resourcehandlers import *
 4
 5# Setup directories
 6data_dir = "data/"
 7output_dir = os.path.abspath(os.path.join(os.getcwd(), "../tests-out/save/"))
 8if not os.path.exists(output_dir):
 9    os.makedirs(output_dir)
10
11# Prepare a path to the source SVG file
12input_path = os.path.join(data_dir, "with-resources.svg")
13
14# Load the SVG document from a file
15with SVGDocument(input_path) as doc:
16
17    # Save SVG with resources
18    doc.save(FileSystemResourceHandler(output_dir))
19
20print(f"SVG document and resources saved to {output_dir}")

After running the code example, check the ../tests-out/save/ directory. You should find the saved SVG file along with any associated resources that were included in the original SVG document.

See Also

You can try to convert SVG documents to various other formats with our Free Online SVG Converter. Just upload SVG, convert it, and get results in seconds! It’s fast, easy, and completely free!

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.