Inkscape

Layers, labels, editable stars and arcs, connectors, guides and export hints; see the Inkscape guide (layers are also in the Documents guide). Document.layer, Document.layers, Document.guides, Document.guide and Document.export_hints are documented with Document.

Inkscape documents: layers, labels, editable stars and arcs, connectors, guides and export hints.

Files saved by Inkscape read and write back with their inkscape: and sodipodi: data intact; this package gives that data a Python interface. Document exposes the common parts directly (doc.layer, doc.layers, doc.guides, doc.export_hints).

class Arc(path: Element)[source]

Bases: _Shape

The editable parameters of an Inkscape ellipse, arc or pie slice.

A live view on a <path sodipodi:type="arc">; assigning a property writes the sodipodi: attribute and redraws d.

Parameters:

path (Path) – A path carrying sodipodi:type="arc".

Raises:

ValueError – If path is not an Inkscape arc.

See also

Star

The same for stars and polygons.

Notes

Angles are in degrees, clockwise from the x axis (Inkscape stores radians).

Examples

>>> import svgwriter as svg
>>> pie = svg.inkscape.Arc.create(0, 0, 10, 10, start=0, end=90)
>>> svg.inkscape.Arc(pie).kind
'slice'
What the code above draws
shape_type = 'arc'
classmethod create(cx: float, cy: float, rx: float, ry: float, *, start: float = 0.0, end: float = 0.0, kind: str = 'slice', **kwargs: Any) → Path[source]

Build a new arc path with Inkscape’s parameters.

Parameters:
  • cx (float) – Centre.

  • cy (float) – Centre.

  • rx (float) – Radii.

  • ry (float) – Radii.

  • start (float, optional) – Angles in degrees; equal angles give a whole ellipse. Default to 0.

  • end (float, optional) – Angles in degrees; equal angles give a whole ellipse. Default to 0.

  • kind ({"slice", "arc", "chord"}, optional) – A pie slice, an open arc or a closed chord. Defaults to "slice".

  • **kwargs – Presentation attributes for the path.

Returns:

Path – The arc, with sodipodi: parameters and matching d.

Raises:

ValueError – If kind is unknown.

Examples

>>> import svgwriter as svg
>>> svg.inkscape.Arc.create(5, 5, 4, 2).bbox
BBox(x=1.0, y=3.0, width=8.0, height=4.0)
redraw() → None[source]

Recompute d from the parameters, as Inkscape does.

property rx: float

Horizontal radius (sodipodi:rx).

property ry: float

Vertical radius (sodipodi:ry).

property start: float

Start angle in degrees (sodipodi:start).

property end: float

End angle in degrees (sodipodi:end); a whole turn when unset.

property kind: str

"slice", "arc" (open) or "chord" (sodipodi:arc-type).

class Connection(start: Element | None, end: Element | None)[source]

Bases: NamedTuple

The objects a connector joins; either end may be None when it is loose.

Variables:

end (start,) – The objects at each end.

Examples

>>> from svgwriter.formats.inkscape import Connection
>>> Connection(None, None).start is None
True
start: Element | None

Alias for field number 0

end: Element | None

Alias for field number 1

class ExportHints(dpi: float | None = None, filename: str | None = None, units: str | None = None)[source]

Bases: object

What Inkscape remembers about exporting a document.

Parameters:
  • dpi (float, optional) – Resolution of the last bitmap export (inkscape:export-xdpi); raster exports use it when no scale, dpi or size is given. Defaults to None.

  • filename (str, optional) – Where Inkscape last exported (inkscape:export-filename). Defaults to None.

  • units (str, optional) – The unit Inkscape’s rulers and toolbars show (inkscape:document-units on <sodipodi:namedview>), such as "mm". Defaults to None.

See also

svgwriter.Document.export_hints

Read or set them on a document.

Examples

>>> from svgwriter.formats.inkscape import ExportHints
>>> ExportHints(dpi=300).dpi
300
dpi: float | None = None
filename: str | None = None
units: str | None = None
class Guide(x: float, y: float, angle: float = 0.0, label: str | None = None, color: str | None = None, locked: bool = False)[source]

Bases: object

A guide line, in the document’s user units with y pointing down.

Parameters:
  • x (float) – A point the line passes through.

  • y (float) – A point the line passes through.

  • angle (float, optional) – Direction of the line in degrees, clockwise from the x axis: 0 is horizontal, 90 vertical. Defaults to 0.

  • label (str, optional) – The name shown next to the guide. Defaults to None.

  • color (str, optional) – Guide colour, as Inkscape writes it ("#0000ff"). Defaults to None.

  • locked (bool, optional) – Whether the guide can be dragged. Defaults to False.

See also

svgwriter.Document.guides

Read or replace a document’s guides.

Notes

Inkscape stores guide positions from the bottom-left of the viewBox with y pointing up, and the orientation as the line’s normal; reading and writing convert both, so guides line up with the drawing’s own coordinates.

Examples

>>> from svgwriter.formats.inkscape import Guide
>>> Guide.vertical(10).angle, Guide.horizontal(5, label="base").y
(90.0, 5)
x: float
y: float
angle: float = 0.0
label: str | None = None
color: str | None = None
locked: bool = False
classmethod horizontal(y: float, **kwargs: object) → Guide[source]

A horizontal guide at height y.

Examples

>>> from svgwriter.formats.inkscape import Guide
>>> Guide.horizontal(20)
Guide(x=0.0, y=20, angle=0.0, label=None, color=None, locked=False)
classmethod vertical(x: float, **kwargs: object) → Guide[source]

A vertical guide at x.

Examples

>>> from svgwriter.formats.inkscape import Guide
>>> Guide.vertical(20).x
20
class Star(path: Element)[source]

Bases: _Shape

The editable parameters of an Inkscape star or polygon.

A live view on a <path sodipodi:type="star">: reading a property reads the sodipodi: attribute, and assigning one writes it and redraws d the way Inkscape does, so the file stays an editable star in Inkscape.

Parameters:

path (Path) – A path carrying sodipodi:type="star".

Raises:

ValueError – If path is not an Inkscape star.

See also

Arc

The same for ellipses and arcs.

svgwriter.library.paths.star

inkscape=True builds one.

Notes

Angles are in degrees, clockwise from the x axis (Inkscape stores radians). inkscape:randomized is kept but not applied to the redrawn outline; Inkscape applies it again when it opens the file.

Examples

>>> import svgwriter as svg
>>> star = svg.inkscape.Star.create(50, 50, 40, 20, 5)
>>> view = svg.inkscape.Star(star)
>>> view.sides = 6
>>> star.get("sodipodi:sides"), len(star.d)
(6, 13)
What the code above draws
shape_type = 'star'
classmethod create(cx: float, cy: float, outer: float, inner: float, sides: int, *, angle: float = -90.0, flatsided: bool = False, rounded: float = 0.0, **kwargs: Any) → Path[source]

Build a new star path with Inkscape’s parameters.

Parameters:
  • cx (float) – Centre.

  • cy (float) – Centre.

  • outer (float) – Radii of the tips and of the notches.

  • inner (float) – Radii of the tips and of the notches.

  • sides (int) – Number of tips (corners for a polygon).

  • angle (float, optional) – Direction of the first tip in degrees. Defaults to -90 (up).

  • flatsided (bool, optional) – A regular polygon instead of a star. Defaults to False.

  • rounded (float, optional) – Inkscape’s rounding factor; 0 keeps sharp corners. Defaults to 0.

  • **kwargs – Presentation attributes for the path.

Returns:

Path – The star, with sodipodi: parameters and matching d.

Examples

>>> import svgwriter as svg
>>> svg.inkscape.Star.create(0, 0, 10, 5, 4).get("sodipodi:type")
'star'
redraw() → None[source]

Recompute d from the parameters, as Inkscape does.

Examples

>>> import svgwriter as svg
>>> star = svg.inkscape.Star.create(0, 0, 10, 5, 3)
>>> star.d = "M0 0"
>>> svg.inkscape.Star(star).redraw()
>>> len(star.d)
7
What the code above draws
property outer: float

Radius of the tips (sodipodi:r1).

property inner: float

Radius of the notches (sodipodi:r2).

property sides: int

Number of tips or corners (sodipodi:sides); the notch angle follows.

property angle: float

Direction of the first tip in degrees (sodipodi:arg1); the notches turn with it.

property skew: float

How far the notches turn away from halfway between tips, in degrees.

property flatsided: bool

A regular polygon (inkscape:flatsided) instead of a star.

property rounded: float

Inkscape’s rounding factor (inkscape:rounded); 0 is sharp.

by_label(root: Element, label: str) → Element | None[source]

The element named label (inkscape:label) under root, if exactly one has it.

Parameters:
  • root (Element) – Where to look.

  • label (str) – The readable name.

Returns:

Element or None – The element, or None when no element has that label.

Raises:

KeyError – If several elements share the label.

Examples

>>> import svgwriter as svg
>>> root = svg.svg(svg.rect(0, 0, 1, 1, inkscape_label="Logo"))
>>> svg.inkscape.by_label(root, "Logo").tag
'rect'
What the code above draws
connect(path: Element, start: Element | None, end: Element | None, *, kind: str = 'polyline') → None[source]

Make path an Inkscape connector from start to end.

Both ends receive an id if they have none. When both ends have known bounds, the path is redrawn as a straight line between their centres; Inkscape reroutes it around obstacles when the file is edited there.

Parameters:
  • path (Element) – A <path>.

  • start (Element or None) – The objects to join; None leaves that end loose, and None for both turns the path back into a plain path.

  • end (Element or None) – The objects to join; None leaves that end loose, and None for both turns the path back into a plain path.

  • kind ({"polyline", "orthogonal"}, optional) – Inkscape’s routing style. Defaults to "polyline".

Raises:

ValueError – If kind is unknown.

Examples

>>> import svgwriter as svg
>>> a, b = svg.circle(0, 0, 5, id="a"), svg.circle(40, 0, 5, id="b")
>>> line = svg.Path(stroke="black")
>>> root = svg.svg(a, b, line)
>>> svg.inkscape.connect(line, a, b)
>>> str(line.d), line.attributes["inkscape:connection-end"]
('M0 0 L40 0', '#b')
What the code above draws
connection(path: Element) → Connection | None[source]

The objects joined by an Inkscape connector, or None when path is not one.

Parameters:

path (Element) – Usually a <path> read from an Inkscape file.

Returns:

Connection or None – The start and end objects, found by id in the path’s document.

Examples

>>> import svgwriter as svg
>>> a, b = svg.rect(0, 0, 10, 10, id="a"), svg.rect(50, 0, 10, 10, id="b")
>>> line = svg.Path()
>>> root = svg.svg(a, b, line)
>>> svg.inkscape.connect(line, a, b)
>>> svg.inkscape.connection(line).end is b
True
What the code above draws
export_hints(root: Element) → ExportHints[source]

The export hints stored on a document’s <svg> and <sodipodi:namedview>.

Parameters:

root (Element) – A document’s <svg>.

Returns:

ExportHints – Fields are None where the file has no hint.

Examples

>>> import svgwriter as svg
>>> svg.inkscape.export_hints(svg.svg(inkscape_export_xdpi=150)).dpi
150.0
guides(root: Element) → list[Guide][source]

The guides of an Inkscape document.

Parameters:

root (Element) – A document’s <svg>.

Returns:

list of Guide – In file order, in user units with y pointing down.

Examples

>>> import svgwriter as svg
>>> root = svg.svg(view_box="0 0 100 50")
>>> svg.inkscape.set_guides(root, [svg.inkscape.Guide.horizontal(10)])
>>> svg.inkscape.guides(root)[0].y
10.0
What the code above draws
is_layer(element: Element) → bool[source]

Whether element is an Inkscape layer or sublayer.

Parameters:

element (Element) – Any element.

Returns:

bool – True for a <g> with inkscape:groupmode="layer".

Examples

>>> import svgwriter as svg
>>> svg.inkscape.is_layer(svg.inkscape.new_layer("Sky")), svg.inkscape.is_layer(svg.group())
(True, False)
layers(root: Element) → dict[str, Group][source]

Every layer and sublayer under root, in document order, by label.

Parameters:

root (Element) – Where to look, usually a document’s <svg>.

Returns:

dict of str to Group – Layers by inkscape:label (their id when unlabelled); when two share a label, the first one is kept.

Examples

>>> import svgwriter as svg
>>> root = svg.svg(svg.inkscape.new_layer("Back"), svg.inkscape.new_layer("Front"))
>>> list(svg.inkscape.layers(root))
['Back', 'Front']
What the code above draws
namedview(root: Element, *, create: bool = False) → Element | None[source]

The <sodipodi:namedview> holding Inkscape’s view settings and guides.

Parameters:
  • root (Element) – A document’s <svg>.

  • create (bool, optional) – Add an empty one when there is none. Defaults to False.

Returns:

Element or None – The element, or None when there is none and create is False.

Examples

>>> import svgwriter as svg
>>> root = svg.svg()
>>> svg.inkscape.namedview(root) is None, svg.inkscape.namedview(root, create=True).tag
(True, 'namedview')
What the code above draws
new_layer(label: str, **kwargs: Any) → Group[source]

Build an empty Inkscape layer.

Parameters:
  • label (str) – The name Inkscape shows in its Layers panel (inkscape:label).

  • **kwargs – Attributes for the <g>.

Returns:

Group – The layer.

See also

svgwriter.Document.layer

Find or add a layer of a document.

Examples

>>> import svgwriter as svg
>>> svg.inkscape.new_layer("Sky").label
'Sky'
set_export_hints(root: Element, hints: ExportHints) → None[source]

Store export hints on a document; None fields remove the hint.

Parameters:

Examples

>>> import svgwriter as svg
>>> root = svg.svg()
>>> svg.inkscape.set_export_hints(root, svg.inkscape.ExportHints(dpi=300, units="mm"))
>>> root.attributes["inkscape:export-xdpi"], svg.inkscape.export_hints(root).units
(300, 'mm')
What the code above draws
set_guides(root: Element, lines: list[Guide]) → None[source]

Replace the guides of a document.

Parameters:
  • root (Element) – A document’s <svg>; a <sodipodi:namedview> is added if needed.

  • lines (list of Guide) – The new guides.

Examples

>>> import svgwriter as svg
>>> root = svg.svg(view_box="0 0 100 50")
>>> svg.inkscape.set_guides(root, [svg.inkscape.Guide.vertical(30)])
>>> svg.inkscape.namedview(root).children[0].attributes["position"]
'30,50'
What the code above draws
shape(element: Element) → Star | Arc | None[source]

The editable Inkscape star or arc behind element, or None for any other element.

Parameters:

element (Element) – Any element, typically one read from an Inkscape file.

Returns:

Star, Arc or None – A live view of the parameters.

Examples

>>> import svgwriter as svg
>>> type(svg.inkscape.shape(svg.inkscape.Star.create(0, 0, 4, 2, 5))).__name__
'Star'
>>> svg.inkscape.shape(svg.rect(0, 0, 1, 1)) is None
True