python bar code generator in python

How to Generate Barcode in Python 3?

Generate Barcode in Python using python-barcode library. it is a simple way to create barcodes. The barcodes are created as SVG objects. This is more simple than Generating QR codes in Python.

In this post, we’ll use Python to create a small script that generates barcodes. We will use the python-barcode module.

This module gives us the ability to create barcodes in the SVG format. To create barcodes in picture formats, you’ll need the Pillow module.

What modules do we need to Generate Barcode in Python?

We need to install python-barcode using PIP. This module is used to generate SVG bar codes.

It gives us the ability to create EAN-8, EAN-13, EAN-14, UPC-A, JAN, ISBN-10, ISBN-13, and a variety of other common barcodes. In the terminal, execute the following command to install this module.

pip install python-barcode

The second module is Pillow. This is where the barcodes in the picture format are created. This library supports a wide range of file formats, has a fast internal representation, and can perform some image processing.

In the terminal, execute the following command to install this module.

pip install Pillow

We’re going to make a barcode in the EAN-13 format. Let’s start by creating it as an SVG file.

# import EAN13 from barcode module
from barcode import EAN13

# Make sure to pass the 12 digit number as string
number = '123456789012'

# Now, let's create an object of EAN13
# class and pass the number
bar_code = EAN13(number)

# Our barcode is ready. Let's save it.
bar_code.save("bar_code")

Then the above code will generate a bar code similar to this with an SVG extension in the same directory. You can open it with a browser to preview it.

1234567890128

If you want to create the barcode in PNG format then it is simple to do as well.

from barcode import EAN13

# Make sure to pass the 12 digit number as string
number = '123456789012'

# Now, let's create an object of EAN13 class and 
# pass the number with the ImageWriter() as the 
# writer
bar_code = EAN13(number, writer=ImageWriter())

# Our barcode is ready. Let's save it.
bar_code.save("bar_code")

The python-barcode module was written so that pure python could generate barcodes. Pillow is necessary for image export such as png, and jpg but not for SVG export.

Read the full documentation of the Python barcode module on the official site.