or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

advanced.mdcore.mdindex.mdshapes.mdtext.mdutilities.md
tile.json

tessl/pypi-svgwrite

A Python library to create SVG drawings programmatically.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/svgwrite@1.4.x

To install, run

npx @tessl/cli install tessl/pypi-svgwrite@1.4.0

index.mddocs/

SVGWrite

SVGWrite 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.

Package Information

  • Package Name: svgwrite
  • Package Type: Python library
  • Language: Python
  • Installation: pip install svgwrite
  • Version: 1.4.3
  • SVG Support: SVG 1.1 Full Profile, SVG 1.2 Tiny Profile

Core Imports

import svgwrite
from svgwrite import Drawing, rgb

For accessing specific components:

from svgwrite.drawing import Drawing
from svgwrite.utils import rgb
from svgwrite.extensions.shapes import ngon, star

Unit imports for dimensional values:

import svgwrite
# Access predefined units
print(5 * svgwrite.cm)  # "5cm"
print(svgwrite.px(10, 20, 30))  # "10px,20px,30px"

Basic Usage

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()

Architecture

SVGWrite is built around several key components:

  • Drawing Class: The main entry point that acts as both an SVG document container and element factory
  • Element Factory Pattern: All SVG elements are created through the Drawing instance using method calls
  • Mixin System: Elements inherit capabilities through mixins (Transform, Presentation, ViewBox, etc.)
  • Profile Support: Handles different SVG specification levels (Full 1.1, Tiny 1.2) with validation
  • Lazy Generation: Elements are built incrementally and serialized to XML only when needed

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.

Capabilities

Core Drawing Operations

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): ...

Core Operations

Shape Creation

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): ...

Shape Creation

Text Handling

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): ...

Text Handling

Advanced Features

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): ...

Advanced Features

Utilities and Extensions

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.): ...

Utilities