python-barcode
python-barcode provides a simple way to create standard barcodes in Python. It requires no external dependencies for generating SVG files. For outputting image formats like PNGs, the optional Pillow library is needed. The current version is 0.16.1, supporting Python 3.9 up to 3.13. The library maintains an active development status with regular updates addressing Python compatibility and bug fixes.
Warnings
- breaking Older versions introduced significant API changes, such as moving writer classes from subpackages to modules (v0.5.0) and modifying the `render` method's API (v0.2.1). Version 0.14.0 changed default dimensions for better PNG/SVG consistency, and v0.15.x dropped support for Python 3.6. Always consult the changelog for major version upgrades.
- gotcha Barcode checksums are automatically calculated by the library for most standard formats (e.g., EAN13, ISBN13). Providing an input number that already includes a checksum, or one with an incorrect length, can lead to invalid barcodes or errors. For EAN13, provide a 12-digit number; the 13th check digit is added automatically.
- gotcha Generating image formats (like PNG, JPEG) using `ImageWriter` requires the `Pillow` library to be installed. Without `Pillow`, attempts to use `ImageWriter` will result in an `ImportError` or other runtime errors related to missing image processing capabilities.
- gotcha When bundling applications with PyInstaller (or similar tools), `python-barcode` may fail to find its internal font files (`DejaVuSansMono.ttf`) for rendering text on barcodes, leading to `IOError` or similar exceptions.
Install
-
pip install python-barcode -
pip install "python-barcode[images]"
Imports
- EAN13
from barcode import EAN13
- SVGWriter
from barcode.writer import SVGWriter
- ImageWriter
from barcode.writer import ImageWriter
- Code128
from barcode.codex import Code128
Quickstart
import os
from io import BytesIO
from barcode import EAN13, Code128
from barcode.writer import SVGWriter, ImageWriter
# Example 1: Generate EAN13 as SVG
number_ean13 = '590123412345' # 12 digits, 13th (checksum) is calculated
ean = EAN13(number_ean13, writer=SVGWriter())
with open('ean13_barcode.svg', 'wb') as f:
ean.write(f)
print('Generated ean13_barcode.svg')
# Example 2: Generate Code128 as PNG (requires Pillow)
try:
# Use a dummy number for Code128, it supports alphanumeric data
code128_data = 'MY-PRODUCT-ABC-123'
code = Code128(code128_data, writer=ImageWriter())
# To write to a file directly, ensure the output path is writable
# code.write(open('code128_barcode.png', 'wb'))
# Or, to an in-memory byte stream, then save
# This pattern is good for web apps or if you don't want to save to disk immediately
fp = BytesIO()
code.write(fp)
# Simulate saving from BytesIO if Pillow is available
from PIL import Image
fp.seek(0) # Rewind to the beginning of the stream
img = Image.open(fp)
img.save('code128_barcode.png')
print('Generated code128_barcode.png (requires Pillow)')
except ImportError:
print("Pillow not installed. Skipping PNG generation. Install with: pip install \"python-barcode[images]\".")
except Exception as e:
print(f"Error generating Code128 PNG: {e}")