Line

class svgwrite.shapes.Line(start=(0, 0), end=(0, 0), **extra)

The line element defines a line segment that starts at one point and ends at another.

Line.__init__(start=(0, 0), end=(0, 0), **extra)
Parameters:
  • start (2-tuple) – start point (x1, y1)
  • end (2-tuple) – end point (x2, y2)
  • extra – additional SVG attributes as keyword-arguments

SVG Attributes

  • x1<coordinate>start parameter
  • y1<coordinate>start parameter
  • x2<coordinate>end parameter
  • y2<coordinate>end parameter

Common SVG Attributes

These are the common SVG Attributes for Line, Rect, Circle, Ellipse, Poliyline and Polygon.

  • classstring

    assigns one or more css-class-names to an element

  • stylestring

    allows per-element css-style rules to be specified directly on a given element

  • externalResourcesRequiredbool

    False: if document rendering can proceed even if external resources are unavailable else: True

  • transform – use svgwrite.mixins.Transform interface

Common Standard SVG Attributes

These are the common Standard SVG Attributes for Line, Rect, Circle, Ellipse, Poliyline and Polygon.

Rect

class svgwrite.shapes.Rect(insert=(0, 0), size=(1, 1), rx=None, ry=None, **extra)

The rect element defines a rectangle which is axis-aligned with the current user coordinate system. Rounded rectangles can be achieved by setting appropriate values for attributes rx and ry.

Rect.__init__(insert=(0, 0), size=(1, 1), rx=None, ry=None, **extra)
Parameters:
  • insert (2-tuple) – insert point (x, y), left-upper point
  • size (2-tuple) – (width, height)
  • rx (<length>) – corner x-radius
  • ry (<length>) – corner y-radius
  • extra – additional SVG attributes as keyword-arguments

SVG Attributes

  • x<coordinate>insert parameter

    The x-axis coordinate of the side of the rectangle which has the smaller x-axis coordinate value

  • y<coordinate>insert parameter

    The y-axis coordinate of the side of the rectangle which has the smaller y-axis coordinate value

  • width<length>size parameter

  • height<length>size parameter

  • rx<length>rx parameter

    For rounded rectangles, the y-axis radius of the ellipse used to round off the corners of the rectangle.

  • ry<length>ry parameter

    For rounded rectangles, the y-axis radius of the ellipse used to round off the corners of the rectangle.

Circle

class svgwrite.shapes.Circle(center=(0, 0), r=1, **extra)

The circle element defines a circle based on a center point and a radius.

Circle.__init__(center=(0, 0), r=1, **extra)
Parameters:
  • center (2-tuple) – circle center point (cx, cy)
  • r (length) – circle-radius r
  • extra – additional SVG attributes as keyword-arguments

SVG Attributes

  • cx<coordinate>center parameter

    The x-axis coordinate of the center of the circle.

  • cy<coordinate>center parameter

    The y-axis coordinate of the center of the circle.

  • r<length>r parameter

    The radius of the circle.

Ellipse

class svgwrite.shapes.Ellipse(center=(0, 0), r=(1, 1), **extra)

The ellipse element defines an ellipse which is axis-aligned with the current user coordinate system based on a center point and two radii.

Ellipse.__init__(center=(0, 0), r=(1, 1), **extra)
Parameters:
  • center (2-tuple) – ellipse center point (cx, cy)
  • r (2-tuple) – ellipse radii (rx, ry)
  • extra – additional SVG attributes as keyword-arguments

SVG Attributes

  • cx<coordinate>center parameter

    The x-axis coordinate of the center of the ellipse.

  • cy<coordinate>center parameter

    The y-axis coordinate of the center of the ellipse.

  • rx<length>r parameter

    The x-axis radius of the ellipse.

  • ry<length>r parameter

    The y-axis radius of the ellipse.

Polyline

class svgwrite.shapes.Polyline(points=[], **extra)

The polyline element defines a set of connected straight line segments. Typically, polyline elements define open shapes.

Polyline.__init__(points=[], **extra)
Parameters:
  • points (iterable) – iterable of points (points are 2-tuples)
  • extra – additional SVG attributes as keyword-arguments

Attributes

Polyline.points

list of points, a point is a 2-tuple (x, y): x, y = <number>

SVG Attributes

  • pointslist of points – points parameter

    The points that make up the polyline. All coordinate values are in the user coordinate system (no units allowed).

How to append points:

Polyline.points.append( point )
Polyline.points.extend( [point1, point2, point3, ...] )

Polygon

class svgwrite.shapes.Polygon(points=[], **extra)

The polygon element defines a closed shape consisting of a set of connected straight line segments.

Same as Polyline but closed.

Basic Shapes Examples


import svgwrite
from svgwrite import cm, mm   


def basic_shapes(name):
    dwg = svgwrite.Drawing(filename=name, debug=True)
    hlines = dwg.add(dwg.g(id='hlines', stroke='green'))
    for y in range(20):
        hlines.add(dwg.line(start=(2*cm, (2+y)*cm), end=(18*cm, (2+y)*cm)))
    vlines = dwg.add(dwg.g(id='vline', stroke='blue'))
    for x in range(17):
        vlines.add(dwg.line(start=((2+x)*cm, 2*cm), end=((2+x)*cm, 21*cm)))
    shapes = dwg.add(dwg.g(id='shapes', fill='red'))

    # set presentation attributes at object creation as SVG-Attributes
    circle = dwg.circle(center=(15*cm, 8*cm), r='2.5cm', stroke='blue', stroke_width=3)
    circle['class'] = 'class1 class2'
    shapes.add(circle)

    # override the 'fill' attribute of the parent group 'shapes'
    shapes.add(dwg.rect(insert=(5*cm, 5*cm), size=(45*mm, 45*mm),
                        fill='blue', stroke='red', stroke_width=3))

    # or set presentation attributes by helper functions of the Presentation-Mixin
    ellipse = shapes.add(dwg.ellipse(center=(10*cm, 15*cm), r=('5cm', '10mm')))
    ellipse.fill('green', opacity=0.5).stroke('black', width=5).dasharray([20, 20])
    dwg.save()


if __name__ == '__main__':
    basic_shapes('basic_shapes.svg')

basic_shapes.svg

Your browser can't render SVG images.