Categories We Write About

Create Python-powered printable labels

Creating Python-powered printable labels involves generating customized labels using Python libraries, typically in PDF or image formats. This allows for programmatic control of label content such as text, barcodes, and layout, which is ideal for inventory, shipping, and product packaging.

Here’s a complete example using reportlab (for PDF label generation) and python-barcode (for barcode creation):


Requirements

Install the necessary libraries:

bash
pip install reportlab python-barcode pillow

Step-by-Step Code to Generate Printable Labels

python
from reportlab.lib.pagesizes import letter from reportlab.pdfgen import canvas from reportlab.lib.units import mm from barcode import Code128 from barcode.writer import ImageWriter from PIL import Image import os # Configuration LABEL_WIDTH = 70 * mm LABEL_HEIGHT = 35 * mm LABEL_MARGIN = 5 * mm LABELS_PER_ROW = 3 LABELS_PER_COLUMN = 8 PAGE_WIDTH, PAGE_HEIGHT = letter output_pdf = "labels_output.pdf" # Sample data for labels labels_data = [ {"name": "Product A", "sku": "A1001"}, {"name": "Product B", "sku": "B1002"}, {"name": "Product C", "sku": "C1003"}, {"name": "Product D", "sku": "D1004"}, {"name": "Product E", "sku": "E1005"}, {"name": "Product F", "sku": "F1006"}, {"name": "Product G", "sku": "G1007"}, {"name": "Product H", "sku": "H1008"}, ] # Function to create barcode images def generate_barcode(sku, path): code = Code128(sku, writer=ImageWriter(), add_checksum=False) barcode_path = os.path.join(path, f"{sku}.png") code.save(barcode_path, {"module_width": 0.2, "module_height": 10.0}) return barcode_path # Create PDF def create_labels(labels, output_file): c = canvas.Canvas(output_file, pagesize=letter) x_start = LABEL_MARGIN y_start = PAGE_HEIGHT - LABEL_MARGIN - LABEL_HEIGHT x = x_start y = y_start temp_barcode_dir = "temp_barcodes" os.makedirs(temp_barcode_dir, exist_ok=True) label_count = 0 for label in labels: # Generate barcode barcode_img_path = generate_barcode(label['sku'], temp_barcode_dir) # Draw border (optional) c.rect(x, y, LABEL_WIDTH, LABEL_HEIGHT) # Add product name c.setFont("Helvetica", 9) c.drawString(x + 5 * mm, y + LABEL_HEIGHT - 10, label['name']) # Add SKU c.setFont("Helvetica", 8) c.drawString(x + 5 * mm, y + LABEL_HEIGHT - 20, f"SKU: {label['sku']}") # Draw barcode c.drawImage(barcode_img_path, x + 5 * mm, y + 5 * mm, width=60, height=15, preserveAspectRatio=True) # Move to next label position label_count += 1 x += LABEL_WIDTH + LABEL_MARGIN if label_count % LABELS_PER_ROW == 0: x = x_start y -= LABEL_HEIGHT + LABEL_MARGIN if label_count % (LABELS_PER_ROW * LABELS_PER_COLUMN) == 0: c.showPage() x = x_start y = y_start c.save() # Cleanup temporary barcode images for f in os.listdir(temp_barcode_dir): os.remove(os.path.join(temp_barcode_dir, f)) os.rmdir(temp_barcode_dir) # Generate the labels PDF create_labels(labels_data, output_pdf)

How It Works

  • Each label includes a product name, SKU, and barcode.

  • The layout is configured for A4/letter paper with multiple rows and columns of labels.

  • Barcodes are generated as PNG images using python-barcode and embedded into a PDF using reportlab.

  • All temporary files are deleted after PDF creation.


Customization Tips

  • Label Size: Adjust LABEL_WIDTH and LABEL_HEIGHT for different label sheets.

  • Font Size & Style: Modify setFont() calls.

  • Label Sheet Compatibility: Match layout to Avery or other label sheet specifications.

  • Dynamic Data Source: Replace labels_data with CSV/Excel input for bulk generation.

This approach offers a flexible, automated way to create printable labels from product or inventory data using Python.

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories We Write About