Registry / serialization / svgwrite

svgwrite

JSON →
library1.4.3pypypi✓ verified 22d ago

SVGWrite is a Python library for programmatically creating SVG (Scalable Vector Graphics) drawings. It enables developers to generate vector graphic shapes, text, and images as XML files. The library is currently in maintenance mode (version 1.4.3, last updated July 14, 2022), meaning it receives bug fixes but no new feature development. It focuses solely on writing SVG files and does not support reading or modifying existing SVG documents, though it can embed other SVGs as images.

pip install svgwrite
INSTALL
IMPORT
SIG · SVGWRITE
S
svgwrite
serializationpythonv1.4.3
Install
1.6s avg
Import
313ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.4.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.328s · 18.4MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.298s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Drawing
import svgwrite dwg = svgwrite.Drawing('output.svg', profile='tiny')
rgb
import svgwrite color = svgwrite.rgb(10, 10, 16, '%')

This example creates a simple SVG file named 'test.svg' with a line and some red text. It demonstrates the basic steps of initializing a `Drawing` object, adding elements like `line` and `text`, and saving the output.

import svgwrite def create_simple_svg(filename='test.svg'): dwg = svgwrite.Drawing(filename, profile='tiny', size=('200px', '100px')) # Add a line dwg.add(dwg.line((0, 0), (100, 50), stroke=svgwrite.rgb(10, 10, 16, '%'))) # Add text text_element = dwg.text('Hello svgwrite!', insert=(10, 70), fill='red') text_element.set_font_size('20px') dwg.add(text_element) dwg.save() print(f"SVG saved to {filename}") if __name__ == '__main__': create_simple_svg()
Debug
Known issues
breakingPython 2 support was officially dropped in version 1.4. Projects using older Python versions must upgrade to Python 3.6+ to use svgwrite 1.4 and later.
fix
Ensure your project runs on Python 3.6 or newer. For older Python 3 versions, upgrade your environment. For Python 2, consider an older svgwrite version (e.g., <1.4) or migrating to Python 3.
affects: >=1.4
gotchaThe library is exclusively for *writing* SVG files. It does not provide functionality to read, parse, or modify existing SVG documents. While you can include other SVGs using the <image> entity, direct manipulation is not supported.
fix
If you need to read or modify existing SVG files, consider using a different library like `lxml` to parse the XML structure, or a dedicated SVG manipulation library.
affects: All versions
gotchaUsing `svgwrite.Drawing(debug=True)` should be strictly avoided in production environments. The validation algorithm, while useful for development, is unoptimized and can significantly degrade performance for larger SVG files.
fix
Only enable `debug=True` during development for error detection. Remove or set `debug=False` for production builds to maintain performance.
affects: All versions
gotchaThe package is in 'maintenance mode' (also described as 'inactive'). This means no new features will be added, and only critical bug fixes will be merged. The project maintains an infrequent release cadence for fixes.
fix
Be aware that new features or significant API changes are unlikely. Plan your usage around the existing feature set and consider community forks or alternatives if advanced features or active development are critical.
affects: >=1.4.1
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'svgwrite'
The 'svgwrite' library is not installed in the Python environment where the script is being executed.
fix
pip install svgwrite
AttributeError: 'Drawing' object has no attribute 'parse_file'
The svgwrite library is designed solely for generating SVG files and does not provide functionality to parse or read existing SVG documents.
fix
Remove the line attempting to parse or read an SVG file, as svgwrite only supports writing; use a different library (e.g., lxml, svgutils) for parsing SVG files.
TypeError: 'int' object is not subscriptable
The `points` argument for `Polyline` or `Polygon` methods expects an iterable of (x, y) coordinate tuples or lists, not a flat list of numbers.
fix
Convert the flat list of coordinates into a list of (x, y) tuples: `dwg.polyline([(x1, y1), (x2, y2), ...])`
TypeError: add() argument must be an svgwrite.base.BaseElement instance, not Element
The `dwg.add()` method expects instances of svgwrite's own element classes, not generic XML element objects from other libraries like `xml.etree.ElementTree` or `lxml`.
fix
Create SVG elements using svgwrite's methods (e.g., `dwg.rect()`, `dwg.text()`) instead of attempting to add elements from other XML parsing libraries.
Upgrade
Version history
1.4.3latest on PyPI · released Jul 14, 2022
Audit
Dependencies

No dependency data recorded yet.

Agent activity
18 hits · last 30 days
node
16
Resources
svgwrite — pip install svgwrite · libregistry