A Python library to create SVG drawings programmatically.
npx @tessl/cli install tessl/pypi-svgwrite@1.4.0SVGWrite is a Python library for creating SVG drawings programmatically. It provides a comprehensive set of tools for generating Scalable Vector Graphics (SVG) documents with vector shapes, text, images, gradients, animations, and advanced effects. The library offers both SVG 1.1 Full Profile and SVG 1.2 Tiny Profile support with full validation and a clean Python API.
pip install svgwriteimport svgwrite
from svgwrite import Drawing, rgbFor accessing specific components:
from svgwrite.drawing import Drawing
from svgwrite.utils import rgb
from svgwrite.extensions.shapes import ngon, starUnit imports for dimensional values:
import svgwrite
# Access predefined units
print(5 * svgwrite.cm) # "5cm"
print(svgwrite.px(10, 20, 30)) # "10px,20px,30px"import svgwrite
# Create a new drawing
dwg = svgwrite.Drawing('example.svg', profile='tiny', size=('200px', '100px'))
# Add basic shapes
dwg.add(dwg.line((0, 0), (100, 50), stroke=svgwrite.rgb(10, 10, 16, '%')))
dwg.add(dwg.rect(insert=(10, 10), size=(80, 30), fill='blue', stroke='black'))
dwg.add(dwg.circle(center=(50, 25), r=15, fill='red', fill_opacity=0.7))
# Add text
dwg.add(dwg.text('Hello SVG', insert=(20, 40), font_family='Arial', font_size='12px'))
# Save to file
dwg.save()
# Or get as string
svg_string = dwg.tostring()SVGWrite is built around several key components:
The Drawing class inherits from both SVG (container) and ElementFactory (element creation), providing unified access to document management and element creation through a single interface.
Primary document management and file I/O operations for creating and saving SVG drawings.
class Drawing:
def __init__(filename="noname.svg", size=('100%', '100%'), profile='full', debug=True, **extra): ...
def save(pretty=False, indent=2): ...
def tostring(): ...
def add(element): ...Comprehensive set of SVG shape elements including basic shapes, paths, and polylines with full styling support.
# Basic shapes
def line(start=(0,0), end=(0,0), **extra): ...
def rect(insert=(0,0), size=(1,1), rx=None, ry=None, **extra): ...
def circle(center=(0,0), r=1, **extra): ...
def ellipse(center=(0,0), r=(1,1), **extra): ...
# Complex shapes
def path(d=None, **extra): ...
def polygon(points=[], **extra): ...
def polyline(points=[], **extra): ...
# Images
def image(href, insert=None, size=None, **extra): ...Text rendering capabilities including basic text, spans, text along paths, and text area containers.
def text(text, insert=None, x=None, y=None, **extra): ...
def tspan(text, insert=None, x=None, y=None, dx=None, dy=None, **extra): ...
def textPath(path, text, startOffset=None, method='align', **extra): ...
def textArea(text=None, insert=None, size=None, **extra): ...Professional graphics capabilities including gradients, patterns, filters, animations, and masking effects.
# Paint servers
def linearGradient(start=None, end=None, **extra): ...
def radialGradient(center=None, r=None, focal=None, **extra): ...
def pattern(insert=None, size=None, **extra): ...
# Animations
def animate(attributeName=None, values=None, **extra): ...
def animateTransform(transform, element=None, **extra): ...
# Filters and effects
def filter(start=None, size=None, **extra): ...
def mask(start=None, size=None, **extra): ...Helper functions, unit handling, color utilities, and extension modules for specialized functionality.
# Color utilities
def rgb(r=0, g=0, b=0, mode='RGB'): ...
# Unit classes
class Unit:
def __init__(unit='cm'): ...
def __rmul__(other): ...
# Shape extensions
def ngon(num_corners, edge_length=None, radius=None, rotation=0.): ...
def star(spikes, r1, r2, rotation=0.): ...