A Python library to create SVG drawings programmatically.
Text rendering capabilities including basic text elements, inline formatting spans, text along paths, and text area containers with comprehensive typography control.
Core text rendering elements for displaying text content in SVG documents.
def text(text, insert=None, x=None, y=None, dx=None, dy=None, rotate=None, **extra):
"""
Create main text element for displaying text
Args:
text: Text content to display
insert: Text insertion point as (x, y) tuple (default: None)
x: X coordinate(s) - single value or list (default: None)
y: Y coordinate(s) - single value or list (default: None)
dx: Relative X offset(s) - single value or list (default: None)
dy: Relative Y offset(s) - single value or list (default: None)
rotate: Rotation angle(s) - single value or list (default: None)
**extra: Additional SVG attributes (font-family, font-size, fill, etc.)
Returns:
Text: Text element with Transform and Presentation mixins
"""
def tspan(text, insert=None, x=None, y=None, dx=None, dy=None, rotate=None, **extra):
"""
Create text span for inline text formatting within text elements
Args:
text: Text content for the span
insert: Span insertion point as (x, y) tuple (default: None)
x: X coordinate(s) - single value or list (default: None)
y: Y coordinate(s) - single value or list (default: None)
dx: Relative X offset(s) - single value or list (default: None)
dy: Relative Y offset(s) - single value or list (default: None)
rotate: Rotation angle(s) - single value or list (default: None)
**extra: Additional SVG attributes (font-style, fill, etc.)
Returns:
TSpan: Text span element with Presentation mixin
"""Usage Examples:
import svgwrite
dwg = svgwrite.Drawing('text.svg', size=('400px', '300px'))
# Basic text with positioning
title = dwg.text('SVG Text Example', insert=(50, 30),
font_family='Arial', font_size='20px', fill='navy')
dwg.add(title)
# Text with multiple coordinate lists
multipoint = dwg.text('A B C D', x=[50, 80, 110, 140], y=[60, 70, 65, 72],
font_size='16px', fill='red')
dwg.add(multipoint)
# Text with relative positioning
offset_text = dwg.text('Offset Text', insert=(50, 100),
dx=[0, 5, 10, 15], dy=[0, -2, -4, -6],
font_size='14px', fill='green')
dwg.add(offset_text)
# Text with character rotation
rotated_chars = dwg.text('ROTATED', insert=(50, 140),
rotate=[0, 15, 30, 45, 60, 75, 90],
font_size='18px', fill='purple')
dwg.add(rotated_chars)
# Styled text with spans
styled_text = dwg.text('', insert=(50, 180), font_family='serif', font_size='16px')
styled_text.add(dwg.tspan('Regular '))
styled_text.add(dwg.tspan('Bold ', font_weight='bold', fill='red'))
styled_text.add(dwg.tspan('Italic ', font_style='italic', fill='blue'))
styled_text.add(dwg.tspan('Underlined', text_decoration='underline', fill='green'))
dwg.add(styled_text)Elements for referencing and reusing existing text content.
def tref(element, **extra):
"""
Create text reference to existing text content
Args:
element: Element to reference (must have an ID)
**extra: Additional SVG attributes
Returns:
TRef: Text reference element with XLink and Presentation mixins
"""Usage Examples:
import svgwrite
dwg = svgwrite.Drawing('textref.svg', size=('400px', '200px'))
# Create reusable text definition
defs = dwg.defs()
original_text = dwg.text('Reusable Text Content', id='shared-text',
font_size='14px', font_family='Arial')
defs.add(original_text)
dwg.add(defs)
# Reference the text multiple times
text1 = dwg.text('', insert=(50, 50), fill='red')
text1.add(dwg.tref(original_text))
dwg.add(text1)
text2 = dwg.text('', insert=(50, 100), fill='blue', font_size='18px')
text2.add(dwg.tref(original_text))
dwg.add(text2)
text3 = dwg.text('', insert=(50, 150), fill='green', transform='rotate(15)')
text3.add(dwg.tref(original_text))
dwg.add(text3)Advanced text rendering that follows curved or complex path shapes.
def textPath(path, text, startOffset=None, method='align', spacing='exact', **extra):
"""
Create text that follows along a path shape
Args:
path: Path element or path reference to follow
text: Text content to render along path
startOffset: Distance along path to start text (default: None)
method: Text alignment method 'align' or 'stretch' (default: 'align')
spacing: Letter spacing method 'auto' or 'exact' (default: 'exact')
**extra: Additional SVG attributes (font-size, fill, etc.)
Returns:
TextPath: Text path element with XLink and Presentation mixins
"""Usage Examples:
import svgwrite
dwg = svgwrite.Drawing('textpath.svg', size=('400px', '300px'))
# Create path definition
defs = dwg.defs()
curve_path = dwg.path(d='M 50,150 Q 200,50 350,150', id='text-curve')
defs.add(curve_path)
dwg.add(defs)
# Make path visible for reference
visible_path = dwg.path(d='M 50,150 Q 200,50 350,150',
stroke='lightgray', stroke_width=1, fill='none')
dwg.add(visible_path)
# Text following the path
curved_text = dwg.text('', font_size='16px', font_family='Arial')
text_path = dwg.textPath(curve_path, 'This text follows the curved path!',
startOffset='10%', fill='blue')
curved_text.add(text_path)
dwg.add(curved_text)
# Multiple text paths on same curve with different offsets
for i, (text, color, offset) in enumerate([
('Start', 'red', '0%'),
('Middle', 'green', '50%'),
('End', 'purple', '90%')
]):
positioned_text = dwg.text('', font_size='14px', font_weight='bold')
text_path = dwg.textPath(curve_path, text, startOffset=offset, fill=color)
positioned_text.add(text_path)
dwg.add(positioned_text)
# Circular text path
circle_path = dwg.path(d='M 200,250 m -50,0 a 50,50 0 1,1 100,0 a 50,50 0 1,1 -100,0',
id='circle-path')
defs.add(circle_path)
circular_text = dwg.text('', font_size='12px', font_family='sans-serif')
circle_textpath = dwg.textPath(circle_path, 'Circular text around a path • ',
fill='darkred')
circular_text.add(circle_textpath)
dwg.add(circular_text)Text container elements with automatic line wrapping for SVG 1.2 Tiny profile.
def textArea(text=None, insert=None, size=None, **extra):
"""
Create text area container with automatic line wrapping
Available only in SVG 1.2 Tiny profile
Args:
text: Initial text content (default: None)
insert: Top-left corner as (x, y) tuple (default: None)
size: Area dimensions as (width, height) tuple (default: None)
**extra: Additional SVG attributes (font-family, font-size, etc.)
Returns:
TextArea: Text area element with Transform and Presentation mixins
"""
def line_increment(value):
"""
Set line spacing for text area
Args:
value: Line height value
"""
def write(text, **extra):
"""
Add text with automatic line breaks
Args:
text: Text content to add
**extra: Additional formatting attributes
"""
def tbreak(**extra):
"""
Create explicit line break element for TextArea
Available only in SVG 1.2 Tiny profile
Args:
**extra: Additional SVG attributes
Returns:
TBreak: Line break element
"""Usage Examples:
import svgwrite
# Note: TextArea only works with SVG 1.2 Tiny profile
dwg = svgwrite.Drawing('textarea.svg', profile='tiny', size=('300px', '200px'))
# Basic text area with wrapping
text_area = dwg.textArea(
insert=(20, 20),
size=(250, 150),
font_family='Arial',
font_size='12px',
fill='black'
)
# Add text content with line spacing
text_area.line_increment('16px')
text_area.write('This is a text area that supports automatic line wrapping. ')
text_area.write('Text will wrap to fit within the specified width. ')
# Add explicit line break
text_area.add(dwg.tbreak())
text_area.write('This text appears on a new line after the break.')
dwg.add(text_area)
# Text area with border for visualization
border = dwg.rect(insert=(20, 20), size=(250, 150),
fill='none', stroke='gray', stroke_width=1)
dwg.add(border)Comprehensive typography control through presentation attributes and CSS styling.
# Font presentation attributes
font_family='family-name' # Font family name
font_size='length' # Font size
font_weight='weight' # normal, bold, bolder, lighter, 100-900
font_style='style' # normal, italic, oblique
text_decoration='decoration' # none, underline, overline, line-through
# Text layout attributes
text_anchor='anchor' # start, middle, end
dominant_baseline='baseline' # auto, middle, central, hanging, etc.
direction='ltr|rtl' # Text direction
writing_mode='mode' # lr-tb, rl-tb, tb-rl, tb-lr
# Letter and word spacing
letter_spacing='length' # Space between characters
word_spacing='length' # Space between wordsUsage Examples:
import svgwrite
dwg = svgwrite.Drawing('typography.svg', size=('500px', '400px'))
# Font family variations
fonts = ['Arial', 'serif', 'monospace', 'fantasy', 'cursive']
for i, font in enumerate(fonts):
text = dwg.text(f'{font} Font Family', insert=(20, 30 + i*25),
font_family=font, font_size='16px')
dwg.add(text)
# Font weights
weights = ['normal', 'bold', '300', '600', '900']
for i, weight in enumerate(weights):
text = dwg.text(f'Weight: {weight}', insert=(20, 170 + i*20),
font_family='Arial', font_size='14px', font_weight=weight)
dwg.add(text)
# Font styles and decorations
text = dwg.text('Normal text', insert=(250, 50), font_size='14px')
dwg.add(text)
italic = dwg.text('Italic text', insert=(250, 75),
font_size='14px', font_style='italic')
dwg.add(italic)
underlined = dwg.text('Underlined text', insert=(250, 100),
font_size='14px', text_decoration='underline')
dwg.add(underlined)
# Text anchoring
anchor_line = dwg.line((350, 130), (350, 200), stroke='lightgray')
dwg.add(anchor_line)
start_text = dwg.text('Start anchor', insert=(350, 150),
text_anchor='start', font_size='12px', fill='red')
dwg.add(start_text)
middle_text = dwg.text('Middle anchor', insert=(350, 170),
text_anchor='middle', font_size='12px', fill='green')
dwg.add(middle_text)
end_text = dwg.text('End anchor', insert=(350, 190),
text_anchor='end', font_size='12px', fill='blue')
dwg.add(end_text)
# Letter and word spacing
spaced_text = dwg.text('L e t t e r S p a c i n g', insert=(20, 320),
font_size='16px', letter_spacing='3px')
dwg.add(spaced_text)
word_spaced = dwg.text('Word Spacing Example', insert=(20, 350),
font_size='16px', word_spacing='10px')
dwg.add(word_spaced)Complex text layout and formatting capabilities for sophisticated typography.
# Text with transformations
def text(..., transform='transform-list'):
"""Text with transform attribute for rotation, scaling, etc."""
# Multi-language support
def text(..., direction='ltr|rtl', xml:lang='language'):
"""Text with language and direction attributes"""
# Accessibility features
def text(..., xml:space='preserve|default'):
"""Control whitespace handling in text content"""Usage Examples:
import svgwrite
dwg = svgwrite.Drawing('advanced-text.svg', size=('400px', '300px'))
# Transformed text
rotated = dwg.text('Rotated Text', insert=(100, 50),
font_size='18px', fill='red',
transform='rotate(30 100,50)')
dwg.add(rotated)
scaled = dwg.text('Scaled Text', insert=(50, 100),
font_size='14px', fill='blue',
transform='scale(1.5,0.8)')
dwg.add(scaled)
# Multi-line text with preserved spacing
multiline = dwg.text('', insert=(50, 150), font_family='monospace',
font_size='12px', xml_space='preserve')
lines = ['Line 1: Code example', 'Line 2: Indented', 'Line 3: Normal']
for i, line in enumerate(lines):
tspan = dwg.tspan(line, x=[50], dy=['1.2em'] if i > 0 else [0])
multiline.add(tspan)
dwg.add(multiline)
# Right-to-left text (for demonstration)
rtl_text = dwg.text('Right to Left Text', insert=(300, 200),
font_size='14px', direction='rtl', text_anchor='end')
dwg.add(rtl_text)
# Text with gradient fill (requires gradient definition)
defs = dwg.defs()
gradient = dwg.linearGradient(start=(0, 0), end=(100, 0), id='text-gradient')
gradient.add_stop_color(0, 'red')
gradient.add_stop_color(1, 'blue')
defs.add(gradient)
dwg.add(defs)
gradient_text = dwg.text('Gradient Text', insert=(50, 250),
font_size='24px', font_weight='bold',
fill=gradient.get_paint_server())
dwg.add(gradient_text)Install with Tessl CLI
npx tessl i tessl/pypi-svgwrite