Writing Various Barcodes with Python
Explore how to write 1D barcodes in Python using the python-barcode library. Understand barcode types like EAN-13, how to generate random or custom codes, and produce barcode images in different formats. Learn to print multiple barcode labels on an A4 sheet using image processing techniques.
We'll cover the following...
We will use the python-barcode library in this course to implement barcodes in Python.
Available barcode types
The following types of 1D barcodes are available from this library:
- Code 39
- Code 128
- PZN7 (aka: PZN)
- ISBN-13
- ISBN-10
- ISSN
- UPC-A
- EAN-8
- EAN-13
- EAN14
- JAN
- GS1-128
Writing barcodes
For our example here, we’ll create a few labels using the EAN-13 type barcode for some in-store products. As mentioned before, we can set the first two digits to any number between 20 and 29 for this specific purpose.
The last digit will be a modulo 10 check digit. We are now free to use the remaining 10 digits as we wish. It is also possible to avoid using the last digit as a check code. In this case, the last digit will be set to 0.
Let’s first import our main libraries. We’ll use the EAN13 class to create our EAN-13 barcodes. The ImageWriter and SVGWriter are used to output the rendered barcode to an image or to an SVG file, respectively.
from barcode import EAN13
from barcode.writer import ImageWriter ...