Categories We Write About

Generate barcodes with Python

Generating barcodes with Python is straightforward thanks to libraries specifically designed for barcode creation. Below is a detailed guide on how to generate various types of barcodes using Python, including code examples and explanations.


1. Install Required Libraries

Two popular Python libraries for generating barcodes are:

  • python-barcode — for creating standard 1D barcodes (e.g., EAN, UPC, Code 128).

  • Pillow — for image handling (used internally or for customizing output).

  • treepoem — for generating both 1D and 2D barcodes (requires Ghostscript installed).

To install these, run:

bash
pip install python-barcode Pillow pip install treepoem

You may also need to install Ghostscript on your system for treepoem to work:


2. Generate 1D Barcodes with python-barcode

This library supports barcodes like EAN, UPC, Code 128, and more.

Example: Generate a Code 128 Barcode

python
import barcode from barcode.writer import ImageWriter # Barcode type: code128 code = barcode.get_barcode_class('code128') # Data to encode data = "123456789012" # Generate barcode image barcode_obj = code(data, writer=ImageWriter()) # Save barcode as PNG file filename = barcode_obj.save('code128_barcode') print(f"Barcode saved as {filename}.png")

This saves a file called code128_barcode.png in the current directory.


3. Generate EAN-13 Barcode

python
import barcode from barcode.writer import ImageWriter ean = barcode.get('ean13', '5901234123457', writer=ImageWriter()) filename = ean.save('ean13_barcode') print(f"EAN-13 Barcode saved as {filename}.png")

EAN-13 requires exactly 12 numeric digits input; the library computes the 13th checksum digit automatically.


4. Customize Barcode Appearance

You can customize barcode output with options passed to ImageWriter:

python
options = { 'module_width': 0.2, # Width of each bar 'module_height': 15, # Height of bars 'font_size': 10, # Text font size 'text_distance': 5, # Distance between barcode and text 'background': 'white', # Background color 'foreground': 'black' # Bar color } code = barcode.get('code128', 'HELLO12345', writer=ImageWriter()) filename = code.save('custom_code128', options) print(f"Custom barcode saved as {filename}.png")

5. Generate QR Codes or 2D Barcodes Using treepoem

treepoem supports many barcode symbologies including QR, PDF417, DataMatrix.

Example: Generate a QR code:

python
import treepoem # Generate QR code img = treepoem.generate_barcode( barcode_type='qrcode', data='https://www.example.com' ) # Save image img.convert('1').save('qrcode.png') print("QR code saved as qrcode.png")

6. Generate Code 39 Barcode

python
import barcode from barcode.writer import ImageWriter code39 = barcode.get('code39', 'CODE39DATA', writer=ImageWriter()) filename = code39.save('code39_barcode') print(f"Code39 Barcode saved as {filename}.png")

7. Output Barcode as SVG (Vector Format)

python-barcode can also output SVGs instead of PNGs:

python
code = barcode.get('code128', '1234567890') filename = code.save('barcode_svg') print(f"Barcode saved as {filename}.svg")

Note: This works without ImageWriter.


Summary

  • Use python-barcode for easy 1D barcode generation (EAN, UPC, Code128, Code39).

  • Use treepoem for advanced barcodes including 2D codes like QR, PDF417.

  • Customize barcodes with options for size, colors, fonts.

  • Save output as PNG or SVG.


If you want a ready-to-use script for any specific barcode type or integration with other systems, just ask!

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