Skip to main content

Label generator with SVG templates supporting embedded Python code and CSV data

Project description

pysvglabel

Label generator with SVG templates supporting embedded Python code and CSV data

Template Output
Design Template
source SVG
with supporting code
Generated Labels
from data CSV

Usage

Install: pip install pysvglabel

To generate a sheet of labels from a template file and csv, run: python -m pysvglabel.generate <template.svg> <data.csv> <output.svg | output.pdf>

Additional options (see command help for details):

  • generate (Inkscape nonstandard) multi-page SVGs
  • send to printer (Windows only)

If PDF output is requested, Inkscape is used as the renderer and must be installed and on your system PATH.

Label sheet data is embedded in the template .svg file. All text in the .svg file is treated as Python f-strings, and the contents of the row (where the column name is a legal Python variable name) are made available. Specially formatted commands (see Template Reference) that call Python functions (such as generating barcodes) can also be written into the template.

Security notes: running the labelmaker can execute arbitrary Python code embedded in the template SVG and supporting scripts. There should be no issues if you're designing your own template from scratch. Treat templates from the internet as you might treat a document with macros - make sure you trust it before running it. Not all embedded Python code may be visible graphical elements.

Windows Live Printing

The live printing utility watches for changes in a .csv file and prints changed rows. This is useful for single label printers (as opposed to sheets), like a thermal printer.

This needs a few more dependencies: pip install pysvglabel[winprint]

Inkscape must be installed and on your system PATH.

Run python -m pysvglabel.printer <printer-name> <template.svg> <data.csv>

API

The SVG templating engine can be used in other Python code with the SvgTemplate class.

from pysvglabel import SvgTemplate
import xml.etree.ElementTree as ET

template = SvgTemplate("template.svg")
# create the blank sheet
sheet = template.create_sheet()
# instantiate a page of labels and append to the sheet, with values specified as a list of dicts, one per label
sheet.append(template.apply_page([
  {'title': 'cat', 'barcode': '0000'},
  {'title': 'duck', 'barcode': '0001'},
]))

# ... which can be written out
ET.tostring(sheet, 'utf-8')

# ... or rendered to PNG
import cairosvg
cairosvg.svg2png(bytestring=ET.tostring(sheet, 'utf-8'))

Template Reference

Control Block

The control block is a textbox starting with # pysvglabel: init This is interpreted as a block of Python code, executed for each template. Variables and imports are available to code running per label instance.

It must define the sheet as a LabelSheet object, for example sheet = LabelSheet(page=(40, 10)*mm, space=(0, 0)*mm, count=(1, 1))

Labels

For each label instance, the CSV data is made available as local variables of the column name. Values can also be accessed through row['column name'], especially if the column name is not a valid Python variable name.

All text is interpreted as f-strings, use {} to substitute in variable values.

Rectangular Area Generators

These are defined as a rectangle (specifying the area) and a textbox (with the Python code, starting with the snake emoji 🐍) in a group.

Barcodes:

  • Code128(data: str, thickness: LengthDimension, quiet: bool = True, align: Align = Align.CENTER, fill: str = '#000000'): code 128 linear barcode generator, a general-purpose barcode
  • QrCode(data: str, size: LengthDimension, align: Align = Align.CENTER, fill: str = '#000000', border: Optional[int] = None, error_correction: Optional[int] = None): QR code generator
  • DataMatrix(data: str, size: LengthDimension, align: Align = Align.CENTER, fill: str = '#000000'): datamatrix 2d barcode generator

Sub-elements:

  • Svg(filename: Optional[str], scaling: Scaling = Scaling.FIT, align: Align = Align.CENTER): include a SVG, with the filename relative to the template SVG's path. If filename is None, this is ignored.
  • Subtemplate(filename: Optional[str], env: Dict[str, Any], scaling: Scaling = Scaling.FIT, align: Align = Align.CENTER): include a template SVG file, applying the templating engine to it.

Modifiers

These are defined as some elements and a textbox (with the Python code, starting with the snake emoji 🐍) in a group.

  • Hide(cond: bool): if cond is true, hides the elements
  • StyleModifier(attribs: Dict[str, str]): sets the argument style attributes of all elements in the group.
  • FillColor(color: str), StrokeColor(color: str): sets the fill / stroke color of all elements in the group. Alpha value (if specified as #rrggbbaa) is mapped to fill-opacity / stroke-opacity.
  • DimensionModifier(set: Dict[str, LengthDimension], add: Dict[str, LengthDimension] = {}): sets or adds attributes of all elements in the group.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pysvglabel-0.1.0.tar.gz (25.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pysvglabel-0.1.0-py3-none-any.whl (31.2 kB view details)

Uploaded Python 3

File details

Details for the file pysvglabel-0.1.0.tar.gz.

File metadata

  • Download URL: pysvglabel-0.1.0.tar.gz
  • Upload date:
  • Size: 25.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pysvglabel-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cbbeae2c4e89e7cebd4adb9de75a0a879d61b8fc8f08338d1e931f364b1e6aee
MD5 32c2f24fca346096878fd77dc6d7658c
BLAKE2b-256 d77b0419c26ce065f606a50ec82edab8ff3818fcbf1602495e7ab06be86eef71

See more details on using hashes here.

Provenance

The following attestation bundles were made for pysvglabel-0.1.0.tar.gz:

Publisher: workflow.yml on ducky64/pysvglabel

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pysvglabel-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pysvglabel-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 31.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pysvglabel-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 01e28db0e63840ca538246b1500677f766c14a60c60329de3bd34e2bf7d40543
MD5 9477deab9ac28ace0a9d11cd5178be58
BLAKE2b-256 4b7cce13d2e226aa54eafdf06e730de2cb91414cf888e23ca79782b0da7c71ac

See more details on using hashes here.

Provenance

The following attestation bundles were made for pysvglabel-0.1.0-py3-none-any.whl:

Publisher: workflow.yml on ducky64/pysvglabel

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page