0
# SVGWrite
1
2
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.
3
4
## Package Information
5
6
- **Package Name**: svgwrite
7
- **Package Type**: Python library
8
- **Language**: Python
9
- **Installation**: `pip install svgwrite`
10
- **Version**: 1.4.3
11
- **SVG Support**: SVG 1.1 Full Profile, SVG 1.2 Tiny Profile
12
13
## Core Imports
14
15
```python
16
import svgwrite
17
from svgwrite import Drawing, rgb
18
```
19
20
For accessing specific components:
21
22
```python
23
from svgwrite.drawing import Drawing
24
from svgwrite.utils import rgb
25
from svgwrite.extensions.shapes import ngon, star
26
```
27
28
Unit imports for dimensional values:
29
30
```python
31
import svgwrite
32
# Access predefined units
33
print(5 * svgwrite.cm) # "5cm"
34
print(svgwrite.px(10, 20, 30)) # "10px,20px,30px"
35
```
36
37
## Basic Usage
38
39
```python
40
import svgwrite
41
42
# Create a new drawing
43
dwg = svgwrite.Drawing('example.svg', profile='tiny', size=('200px', '100px'))
44
45
# Add basic shapes
46
dwg.add(dwg.line((0, 0), (100, 50), stroke=svgwrite.rgb(10, 10, 16, '%')))
47
dwg.add(dwg.rect(insert=(10, 10), size=(80, 30), fill='blue', stroke='black'))
48
dwg.add(dwg.circle(center=(50, 25), r=15, fill='red', fill_opacity=0.7))
49
50
# Add text
51
dwg.add(dwg.text('Hello SVG', insert=(20, 40), font_family='Arial', font_size='12px'))
52
53
# Save to file
54
dwg.save()
55
56
# Or get as string
57
svg_string = dwg.tostring()
58
```
59
60
## Architecture
61
62
SVGWrite is built around several key components:
63
64
- **Drawing Class**: The main entry point that acts as both an SVG document container and element factory
65
- **Element Factory Pattern**: All SVG elements are created through the Drawing instance using method calls
66
- **Mixin System**: Elements inherit capabilities through mixins (Transform, Presentation, ViewBox, etc.)
67
- **Profile Support**: Handles different SVG specification levels (Full 1.1, Tiny 1.2) with validation
68
- **Lazy Generation**: Elements are built incrementally and serialized to XML only when needed
69
70
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.
71
72
## Capabilities
73
74
### Core Drawing Operations
75
76
Primary document management and file I/O operations for creating and saving SVG drawings.
77
78
```python { .api }
79
class Drawing:
80
def __init__(filename="noname.svg", size=('100%', '100%'), profile='full', debug=True, **extra): ...
81
def save(pretty=False, indent=2): ...
82
def tostring(): ...
83
def add(element): ...
84
```
85
86
[Core Operations](./core.md)
87
88
### Shape Creation
89
90
Comprehensive set of SVG shape elements including basic shapes, paths, and polylines with full styling support.
91
92
```python { .api }
93
# Basic shapes
94
def line(start=(0,0), end=(0,0), **extra): ...
95
def rect(insert=(0,0), size=(1,1), rx=None, ry=None, **extra): ...
96
def circle(center=(0,0), r=1, **extra): ...
97
def ellipse(center=(0,0), r=(1,1), **extra): ...
98
99
# Complex shapes
100
def path(d=None, **extra): ...
101
def polygon(points=[], **extra): ...
102
def polyline(points=[], **extra): ...
103
104
# Images
105
def image(href, insert=None, size=None, **extra): ...
106
```
107
108
[Shape Creation](./shapes.md)
109
110
### Text Handling
111
112
Text rendering capabilities including basic text, spans, text along paths, and text area containers.
113
114
```python { .api }
115
def text(text, insert=None, x=None, y=None, **extra): ...
116
def tspan(text, insert=None, x=None, y=None, dx=None, dy=None, **extra): ...
117
def textPath(path, text, startOffset=None, method='align', **extra): ...
118
def textArea(text=None, insert=None, size=None, **extra): ...
119
```
120
121
[Text Handling](./text.md)
122
123
### Advanced Features
124
125
Professional graphics capabilities including gradients, patterns, filters, animations, and masking effects.
126
127
```python { .api }
128
# Paint servers
129
def linearGradient(start=None, end=None, **extra): ...
130
def radialGradient(center=None, r=None, focal=None, **extra): ...
131
def pattern(insert=None, size=None, **extra): ...
132
133
# Animations
134
def animate(attributeName=None, values=None, **extra): ...
135
def animateTransform(transform, element=None, **extra): ...
136
137
# Filters and effects
138
def filter(start=None, size=None, **extra): ...
139
def mask(start=None, size=None, **extra): ...
140
```
141
142
[Advanced Features](./advanced.md)
143
144
### Utilities and Extensions
145
146
Helper functions, unit handling, color utilities, and extension modules for specialized functionality.
147
148
```python { .api }
149
# Color utilities
150
def rgb(r=0, g=0, b=0, mode='RGB'): ...
151
152
# Unit classes
153
class Unit:
154
def __init__(unit='cm'): ...
155
def __rmul__(other): ...
156
157
# Shape extensions
158
def ngon(num_corners, edge_length=None, radius=None, rotation=0.): ...
159
def star(spikes, r1, r2, rotation=0.): ...
160
```
161
162
[Utilities](./utilities.md)