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
- filename (string) – filesystem filename valid for
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
- filename (string) – filesystem filename valid for
-
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.Lineobject.
-
Drawing.rect(insert=(0, 0), size=(1, 1), rx=None, ry=None, **extra)¶ Create a
svgwrite.shapes.Rectobject.
-
Drawing.circle(center=(0, 0), r=1, **extra)¶ Create a
svgwrite.shapes.Circleobject.
-
Drawing.ellipse(center=(0, 0), r=(1, 1), **extra)¶ Create a
svgwrite.shapes.Ellipseobject.
-
Drawing.polyline(points=[], **extra)¶ Create a
svgwrite.shapes.Polylineobject.
-
Drawing.polygon(points=[], **extra)¶ Create a
svgwrite.shapes.Polygonobject.
-
Drawing.text(text, insert=None, x=[], y=[], dx=[], dy=[], rotate=[], **extra)¶ Create a
svgwrite.text.Textobject.
-
Drawing.tspan(text, insert=None, x=[], y=[], dx=[], dy=[], rotate=[], **extra)¶ Create a
svgwrite.text.TSpanobject.
-
Drawing.tref(element, **extra)¶ Create a
svgwrite.text.TRefobject.
-
Drawing.textPath(path, text, startOffset=None, method='align', spacing='exact', **extra)¶ Create a
svgwrite.text.TextPathobject.
-
Drawing.textArea(text=None, insert=None, size=None, **extra)¶ Create a
svgwrite.text.TextAreaobject.
-
Drawing.path(d=None, **extra)¶ Create a
svgwrite.path.Pathobject.
-
Drawing.image(href, insert=None, size=None, **extra)¶ Create a
svgwrite.image.Imageobject.
-
Drawing.g(**extra)¶ Create a
svgwrite.container.Groupobject.
-
Drawing.symbol(**extra)¶ Create a
svgwrite.container.Symbolobject.
-
Drawing.svg(insert=None, size=None, **extra)¶ Create a
svgwrite.container.SVGobject.
-
Drawing.use(href, insert=None, size=None, **extra)¶ Create a
svgwrite.container.Useobject.
-
Drawing.a(href, target='_blank', **extra)¶ Create a
svgwrite.container.Hyperlinkobject.
-
Drawing.marker(insert=None, size=None, orient=None, **extra)¶ Create a
svgwrite.container.Markerobject.
-
Drawing.script(href=None, content='', **extra)¶ Create a
svgwrite.container.Scriptobject.
-
Drawing.style(content='', **extra)¶ Create a
svgwrite.container.Styleobject.
-
Drawing.linearGradient(start=None, end=None, inherit=None, **extra)¶ Create a
svgwrite.gradients.LinearGradientobject.
-
Drawing.radialGradient(center=None, r=None, focal=None, inherit=None, **extra)¶ Create a
svgwrite.gradients.RadialGradientobject.
-
Drawing.mask(start=None, size=None, **extra)¶ Create a
svgwrite.masking.Maskobject.
-
Drawing.clipPath(**extra)¶ Create a
svgwrite.masking.ClipPathobject.
-
Drawing.set(element=None, **extra)¶ Create a
svgwrite.animate.Setobject.
-
Drawing.animate(element=None, **extra)¶ Create a
svgwrite.animate.Animateobject.
-
Drawing.animateColor(element=None, **extra)¶ Create a
svgwrite.animate.AnimateColorobject.
-
Drawing.animateMotion(element=None, **extra)¶ Create a
svgwrite.animate.AnimateMotionobject.
-
Drawing.animateTransform(transform, element=None, **extra)¶ Create a
svgwrite.animate.AnimateTransformobject.
-
Drawing.filter(start=None, size=None, resolution=None, inherit=None, **extra)¶ Create a
svgwrite.filters.Filterobject. (Filter Primitives are created by factory-methods of the class Filter)
Parent Classes¶
svgwrite.base.BaseElementsvgwrite.container.Symbolsvgwrite.container.SVGsvgwrite.mixins.Transformsvgwrite.mixins.ViewBoxsvgwrite.mixins.Presentationsvgwrite.mixins.Clippingsvgwrite.elementfactory.ElementFactory