Welcome to pyInkscape’s documentation!¶
pyInkscape is a library for manipulating Inkscape SVG content using Python 3.
Getting Started¶
pyInkscape is a tiny library that helps reading and editing Inkscape SVG graphic files.
Installation¶
pyInkscape is available on PyPI and can be installed using pip.
pip install --user pyinkscape
To make sure that pyinkscape has been installed properly, try:
python -c "import pyinkscape; print(pyinkscape.__version__)"
0.1a2
Or inside Python:
>>> import pyinkscape
>>> pyinkscape.__version__
'0.1a2'
First pyInkscape script¶
This script create an empty canvas (i.e. Inkscape page), finds the layer with the name “Layer 1”, and then write “Hello World” onto that layer. The result is then written into the file hello.svg.
>>> from pyinkscape import Canvas
>>> t = Canvas()
>>> l = t.layer('Layer 1')
>>> l.text("Hello World", center=(100, 100))
>>> t2.render('hello.svg')
Recipes for Common Usecases¶
These are common usecases of pyInkscape.
Load an existing Inkscape file¶
>>> c = Canvas('/home/user/Pictures/my_file.svg')
Finding layers¶
Layers can be searched by either names or IDs.
c = Canvas('/home/user/Pictures/my_file.svg')
# get all existing layers
layers = c.layers()
# get a layer by name
layer1 = c.layer("Layer 1")
# get a layer by ID
layer1 = c.layer_by_id("layer1")
Draw a text¶
Use the text() method of a Layer object to draw a text onto that layer.
>>> from pyInkscape import Canvas
>>> c = Canvas()
>>> l = c.layer("Layer 1")
>>> l.text("Hello World", (50, 50))
>>> c.render("output.svg")
pyInkscape APIs¶
The most important class in pyInkscape is the pyinkscape.Canvas
.
This represents an Inkscape canvas (i.e. SVG file).
Graphical objects (text, circle, rectangle, etc.) can be drawn onto available pyinkscape.inkscape.Group
objects.
TODO: Explain about common APIs
-
class
pyinkscape.
Canvas
(filepath=':memory:', *args, **kwargs)[source]¶ This class represents an Inkscape drawing page (i.e. a SVG file)
-
layer
(name: str) → pyinkscape.inkscape.Group[source]¶ Find the first layer with a name
Layer names are not unique. If there are multiple layers with the same name, only the first one will be returned
- Parameters
name – Name of the layer to search (Note: Layer names a not unique)
- Returns
A Group object if found, or None
- Return type
-
Useful Links¶
pyInkscape documentation: https://pyinkscape.readthedocs.io/
pyInkscape on PyPI: https://pypi.org/project/pyinkscape/
Soure code: https://github.com/letuananh/pyinkscape/