Skip to main content

pypi PyPI Downloads

pylabels2

pylabels2 is a Python library for creating PDFs to print sheets of labels. It uses the ReportLab PDF toolkit to produce the PDF.

This is a fork of davis-junior/pylabels

The original project was written by Blair Bonnett found at bcbnz/pylabels

See also erikvw/django-pylabels for integration into Django.

Installation

pip install pylabels2

Usage

Note: In this fork, the module has been renamed from labels to pylabels.

Install:

from pylabels import Specification, Sheet

Create a callable that adds content to a single label:

def draw_label(label, width, height, label_data):
    label.add(shapes.String(2, 2, str(label_data), fontName="Helvetica", fontSize=40))

Create a Specification for the layout of the labels on a sheet:

specs = Specification(210, 297, 2, 8, 90, 25, corner_radius=2)

Create a Sheet and pass it the spec and callable:

sheet = Sheet(specs, draw_label, border=True)

Add labels to the sheet:

sheet.add_label("Hello World1")
sheet.add_label("Hello World2")
# etc ...

Save the sheet to file as PDF:

sheet.save("basic.pdf")

Or save to BytesIO buffer:

buffer = sheet.save_to_buffer()

See detailed examples below.

Overview

Basically, the user creates a set of specifications of the label sizes etc., writes a callback function which does the actual drawing, and gives these two items to a Sheet object. Items are then added to the sheet using the add_label() method (or add_labels() to add all items from an iterable).

The callback function is called once for each item, being given a ReportLab Drawing object representing the label, its width and height, and the item to draw on the label. Any of the standard ReportLab drawing methods can be used, with pylabels automatically adding a clipping path around each label to prevent it interfering with other labels.

Once all the items have been added, the labels can be saved as a PDF, a preview of a page can be saved as an image, or returned as a BytesIO buffer.

Watermarks during testing

You can add a watermark to each label by passing watermark parameters to the save or save_buffer methods:

watermark = ("Sample", ("Helvetica", 12))
sheet.save_buffer(watermark)

or

watermark = ("Sample", ("Helvetica", 12))
sheet.save(file_obj, watermark)

Examples

The following examples are available in the demos directory:

  • Basic - an introduction to the basic use of pylabels.
  • Partial pages - how to produce partial pages (i.e., pages with some of the labels previously used).
  • Repeated - how to use the count parameter to add multiple copies of the same label.
  • Background colours - examples of solid, striped and hatched backgrounds of different colours on each label.
  • Page background - how to add a background image for each page.
  • Padding - how to add padding to the labels.
  • Nametags - creates a set of nametags from the list of names in the names.txt file. Includes the use of two custom fonts, font size selection, and centred text.
  • Image preview - generates image previews of two of the pages from the nametags demo.
  • Addresses - print mailing labels (From a CSV file) on a standard Avery 5160 label page.
  • Django demo - Download a PDF of labels with barcodes directly from the browser in a Django project (uses save_to_buffer instead of save).

Demo fonts

The following fonts are used in the demo scripts and are included in the demos folder:

License

Copyright (C) 2012, 2013, 2014, 2015 Blair Bonnett

pylabels is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

pylabels is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with pylabels. If not, see http://www.gnu.org/licenses/.

Metadata

Release files for pylabels2 1.4.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pylabels2 1.4.4
File Size Uploaded Provenance Actions
pylabels2-1.4.4.tar.gz 658.5 kB Without provenance Details for pylabels2-1.4.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pylabels2 1.4.4
File Interpreter ABI Platform Actions
pylabels2-1.4.4-py3-none-any.whl Python 3 none any Details for pylabels2-1.4.4-py3-none-any.whl

Total release size: 1.3 MB

Release files / pylabels2-1.4.4.tar.gz

Download URL pylabels2-1.4.4.tar.gz
Size 658.5 kB
Tags Source
SHA-256 checksum
How to use checksums
12dd72ef717ff6f551350f10616ae163799fa4501f56541bd82061c63dfa64f8
BLAKE2b-256 checksum
How to use checksums
efbf1887bfb7ec2a43032af1ca87c4d099dba9ca4875d77c2ea8f53c5c9ff509
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.4

Release files / pylabels2-1.4.4-py3-none-any.whl

Download URL pylabels2-1.4.4-py3-none-any.whl
Size 659.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
edac8c8bfba0e17f95dfce1f018554561756518408c897e0196a646fdb0ce7cd
BLAKE2b-256 checksum
How to use checksums
80669b7f3d95591f520f5d5101544ca53abbddb395fc2c79efb4620a02f7c4a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.4

Security for pylabels2 1.4.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page