svgutils - Python SVG Editor

raw JSON →
0.3.4 verified Fri May 01 auth: no python

svgutils is a Python library for editing and manipulating SVG files. It allows you to create, transform, and combine SVG elements programmatically. Current version is 0.3.4. The library is in maintenance mode with infrequent releases.

pip install svgutils
error ImportError: cannot import name 'SVGFigure' from 'svgutils'
cause Incorrect import path. SVGFigure is in svgutils.compose.
fix
Use from svgutils.compose import SVGFigure
error AttributeError: 'NoneType' object has no attribute 'write'
cause Attempting to save a figure with no attached elements or invalid path.
fix
Ensure the figure has at least one element appended and the output path is writable.
error lxml.etree.XMLSyntaxError: Start tag expected, ...
cause Opening a non-XML file or malformed SVG.
fix
Verify the input SVG file is well-formed XML. Use fromstring() with proper encoding.
deprecated svgutils.compose module is considered stable but older transform methods may be deprecated. Check for deprecation warnings.
fix Use the compose API where possible; refer to examples on GitHub.
gotcha Element positions (x, y) are in SVG user units, not pixels. Scaling depends on viewBox.
fix Set viewBox explicitly via SVGFigure attributes to control coordinate space.
gotcha All elements must be appended to a figure before saving. Attaching elements after save does not update file.
fix Call fig.append() before fig.save().
breaking Python 2 support was dropped in version 0.3.0. Python 3.6+ required.
fix Upgrade to Python 3.6 or later.

Create a simple SVG with text and a line.

from svgutils.compose import SVGFigure, Element, Text, Line
# Create a new SVG figure
fig = SVGFigure(width=200, height=200)
# Add a text element
txt = Text('Hello, svgutils!', x=10, y=50, size=16, color='black')
# Add a line
line = Line(x1=10, y1=10, x2=100, y2=100, stroke='red')
# Attach elements to figure
fig.append(txt)
fig.append(line)
# Save to file
fig.save('output.svg')