Drawing

The Drawing object is the overall container for all SVG elements. It provides the methods to store the drawing into a file or a file-like object. If you want to use stylesheets, the reference links to this stylesheets were also stored (add_stylesheet) in the Drawing object.

set/get SVG attributes:

element['attribute'] = value
value = element['attribute']

The Drawing object also includes a defs section, add elements to the defs section by:

drawing.defs.add(element)
class svgwrite.drawing.Drawing(filename='noname.svg', size=('100%', '100%'), **extra)

This is the SVG drawing represented by the top level svg element.

A drawing consists of any number of SVG elements contained within the drawing element, stored in the elements attribute.

A drawing can range from an empty drawing (i.e., no content inside of the drawing), to a very simple drawing containing a single SVG element such as a rect, to a complex, deeply nested collection of container elements and graphics elements.

Drawing.__init__(filename='noname.svg', size=('100%', '100%'), **extra)
Parameters:
  • filename (string) – filesystem filename valid for open()
  • size (2-tuple) – width, height
  • extra (keywords) – additional svg-attributes for the SVG object

Important (and not SVG Attributes) extra parameters:

Parameters:
  • profile (string) – 'tiny | full' - define the SVG baseProfile
  • debug (bool) – switch validation on/off

Attributes

Drawing.filename

string should be valid for open().

Drawing.defs

SVG defs section - as Defs object.

Methods

Drawing.add(element)

Add an SVG element as subelement.

Parameters:element – append this SVG element
Returns:the added element
Drawing.write(fileobj, pretty=False, indent=2)

Write XML string to fileobj.

Parameters:
  • fileobj – a file-like object
  • pretty – True for easy readable output
  • indent – how much to indent if pretty is enabled, by default 2 spaces

Python 3.x - set encoding at the open command:

open('filename', 'w', encoding='utf-8')
Drawing.save(pretty=False, indent=2)

Write the XML string to self.filename.

Parameters:
  • pretty – True for easy readable output
  • indent – how much to indent if pretty is enabled, by default 2 spaces
Drawing.saveas(filename, pretty=False, indent=2)

Write the XML string to filename.

Parameters:
  • filename (string) – filesystem filename valid for open()
  • pretty – True for easy readable output
  • indent – how much to indent if pretty is enabled, by default 2 spaces
Drawing.add_stylesheet(href, title, alternate='no', media='screen')

Add a stylesheet reference.

Parameters:
  • href (string) – link to stylesheet <URI>
  • title (string) – name of stylesheet
  • alternate (string) – 'yes'|'no'
  • media (string) – 'all | aureal | braille | embossed | handheld | print | projection | screen | tty | tv'
Drawing.get_xml()

Get the XML representation as ElementTree object.

Returns:XML ElementTree of this object and all its subelements
Drawing.tostring()

Get the XML representation as unicode string. If you embed the SVG object into a XHTML page, you have to link to the CSS files (if you use CSS classes) in the header section of the surrounding XHTML page.

Returns:unicode XML string of this object and all its subelements

Factory Methods

Drawing.line(start=(0, 0), end=(0, 0), **extra)

Create a svgwrite.shapes.Line object.

Drawing.rect(insert=(0, 0), size=(1, 1), rx=None, ry=None, **extra)

Create a svgwrite.shapes.Rect object.

Drawing.circle(center=(0, 0), r=1, **extra)

Create a svgwrite.shapes.Circle object.

Drawing.ellipse(center=(0, 0), r=(1, 1), **extra)

Create a svgwrite.shapes.Ellipse object.

Drawing.polyline(points=[], **extra)

Create a svgwrite.shapes.Polyline object.

Drawing.polygon(points=[], **extra)

Create a svgwrite.shapes.Polygon object.

Drawing.text(text, insert=None, x=[], y=[], dx=[], dy=[], rotate=[], **extra)

Create a svgwrite.text.Text object.

Drawing.tspan(text, insert=None, x=[], y=[], dx=[], dy=[], rotate=[], **extra)

Create a svgwrite.text.TSpan object.

Drawing.tref(element, **extra)

Create a svgwrite.text.TRef object.

Drawing.textPath(path, text, startOffset=None, method='align', spacing='exact', **extra)

Create a svgwrite.text.TextPath object.

Drawing.textArea(text=None, insert=None, size=None, **extra)

Create a svgwrite.text.TextArea object.

Drawing.path(d=None, **extra)

Create a svgwrite.path.Path object.

Drawing.image(href, insert=None, size=None, **extra)

Create a svgwrite.image.Image object.

Drawing.g(**extra)

Create a svgwrite.container.Group object.

Drawing.symbol(**extra)

Create a svgwrite.container.Symbol object.

Drawing.svg(insert=None, size=None, **extra)

Create a svgwrite.container.SVG object.

Drawing.use(href, insert=None, size=None, **extra)

Create a svgwrite.container.Use object.

Drawing.a(href, target='_blank', **extra)

Create a svgwrite.container.Hyperlink object.

Drawing.marker(insert=None, size=None, orient=None, **extra)

Create a svgwrite.container.Marker object.

Drawing.script(href=None, content='', **extra)

Create a svgwrite.container.Script object.

Drawing.style(content='', **extra)

Create a svgwrite.container.Style object.

Drawing.linearGradient(start=None, end=None, inherit=None, **extra)

Create a svgwrite.gradients.LinearGradient object.

Drawing.radialGradient(center=None, r=None, focal=None, inherit=None, **extra)

Create a svgwrite.gradients.RadialGradient object.

Drawing.mask(start=None, size=None, **extra)

Create a svgwrite.masking.Mask object.

Drawing.clipPath(**extra)

Create a svgwrite.masking.ClipPath object.

Drawing.set(element=None, **extra)

Create a svgwrite.animate.Set object.

Drawing.animate(element=None, **extra)

Create a svgwrite.animate.Animate object.

Drawing.animateColor(element=None, **extra)

Create a svgwrite.animate.AnimateColor object.

Drawing.animateMotion(element=None, **extra)

Create a svgwrite.animate.AnimateMotion object.

Drawing.animateTransform(transform, element=None, **extra)

Create a svgwrite.animate.AnimateTransform object.

Drawing.filter(start=None, size=None, resolution=None, inherit=None, **extra)

Create a svgwrite.filters.Filter object. (Filter Primitives are created by factory-methods of the class Filter)