Barcodes are ubiquitous. Everything from supermarket product packaging to shipping parcel tracking numbers utilizes barcodes to quicken and stabilize data handling. Being able to create barcode in Python enables you to automate product marking, enhance inventory management, or even develop in-house tools for business processes.
For instance, if you own a small online shop, you can generate barcodes for every product and read them later when processing orders more quickly. Python simplifies this with the python-barcode library so that you create and design barcodes in a few lines of code.
Use Cases of Barcode Generation
Barcodes find application in several industries and day-to-day operations. Some of the typical use cases include:
- Product labeling and inventory management
- Parcel and logistics tracking
- Library management systems for books
- Generating event tickets
- Document tagging and organization
Installing python-barcode and Pillow
Before creating a barcode or moving forward, you need to install the some libraries which provides ready to go functionalities. The python-barcode library generates barcodes, and Pillow helps save them as images.
Run the following commands in your terminal or command prompt:
pip install python-barcode
pip install PillowAfter installation, your system will be ready to create barcode and export barcodes as image files.
Generating Barcodes in Python
The python-barcode library supports various formats such as EAN13, Code128, and UPC. Let’s take an simple example to generate barcode with simple product numbers.
import barcode
from barcode.writer import ImageWriter
number = "123456789012"
ean = barcode.get("ean13", number, writer=ImageWriter())
filename = ean.save("product_barcode")Once you run this python script, it creates a barcode for given product ID and saves it as an image file named product_barcode.png. You can change the number or barcode type based on your needs.
Saving Barcode as Image
When you generate a barcode, it is automatically saved as an image file if you use ImageWriter. The image can be in formats like PNG or JPEG, depending on your use case. These image files can be used for printing stickers for each products and your system can use barcode reader to scan each product and find relevant information.
import barcode
from barcode.writer import ImageWriter
code128 = barcode.get("code128", "ORDER2025", writer=ImageWriter())
code128.save("order_label")
This example will create barcode and save as image. Here, we have taken example of order ID which stores unique ID for order. This order number can be used to identify order information from barcode.
Customizing Barcode in Python
Customizing your barcode gives it a more professional look. The python barcode library allows adjustments such as height, font size, text distance, and quiet zones. These settings help control how your barcode looks when printed or displayed digitally.
Here is an example of barcode generation in Python with custom settings:
import barcode
from barcode.writer import ImageWriter
options = {
    "module_height": 15.0,
    "font_size": 12,
    "text_distance": 2.0,
    "quiet_zone": 4.0
}
ean = barcode.get("ean13", "987654321098", writer=ImageWriter())
ean.save("custom_barcode", options)In simpler terms:
- module_height adjusts how tall the barcode lines appear.
- font_size sets the text size below the barcode.
- text_distance changes the space between the barcode and text.
- quiet_zone adds padding around the barcode for better scanning accuracy.

These small configurations help you align your barcode design with your packaging or printing document layout.
Conclusion
It’s an impressive and useful skill to learn how to generate barcode in Python. You can automate marking, make inventory management easier, and even create personal barcode tools for your enterprise. The python library for barcode makes the whole process easy, adaptable, and fast.
Begin generating barcodes today and streamline data management with Python. For tasks that require barcodes as well as QR codes, check out Generate QR Code in Python Using Segno to generate readable codes holding more data and appearing trendy.

