SVGWrite
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.
Warnings
- breaking Python 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.
- gotcha The 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.
- gotcha Using `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.
- gotcha The 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.
Install
-
pip install svgwrite
Imports
- Drawing
import svgwrite dwg = svgwrite.Drawing('output.svg', profile='tiny') - rgb
import svgwrite color = svgwrite.rgb(10, 10, 16, '%')
Quickstart
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()