Creating Barcodes using the python-barcode Library

The python-barcode library is a Python package that allows users to generate various types of barcodes easily. It provides a simple interface to create barcodes for different purposes, such as product labeling, inventory management, shipping, and more. The library supports a wide range of barcode formats, including EAN-13, Code39, Code128, UPC-A, and many others.

In this article, we will cover how to install the python-barcode library, where it can be used, and provide a step-by-step guide to using it to generate an EAN-13 barcode. Additionally, we will create a final project to illustrate a practical use case of the library.


To get started with the python-barcode library, you need to install it first. Open your terminal or command prompt and use the following command to install the library using pip:

pip install python-barcode

Where to Use python-barcode

The python-barcode library is useful in various scenarios, such as:

  1. Product Labeling: Generate barcodes for products to be scanned at the point of sale or for inventory management.
  2. Shipping and Logistics: Create barcodes for tracking shipments and packages.
  3. Library Systems: Generate barcodes for books or other library items for easy cataloging and check-out.
  4. ID Cards: Create barcodes for employee or member ID cards.
  5. Asset Management: Generate barcodes for tracking and managing assets in an organization.

Step-by-Step Guide to Using python-barcode

In this section, we will walk through the process of using the python-barcode library to generate an EAN-13 barcode. EAN-13 barcodes are commonly used for product identification and are 13 digits long.

Import Required Modules

First, start by importing the necessary modules from the python-barcode library:

from barcode import EAN13
from barcode.writer import ImageWriter

Define the Barcode Generation Function

Next, define a function that will generate the barcode. This function will take a 12-digit number (excluding the check digit) as input and save the generated barcode image as a file.

def generator(number):
    my_code = EAN13(number, writer=ImageWriter())"bar_code")

Get User Input

In the __main__ section of your script, get user input for the 12-digit number:

if __name__ == "__main__":
    user_input = input('Enter 12-digit number to generate a barcode: ')

Run the Script

Run the script, and it will prompt you to enter a 12-digit number. After entering the number, the script will generate an EAN-13 barcode and save it as "bar_code.png" in the same directory as the script.

You can watch the video below:

Final Project: Inventory Management System

Let's create a final project to demonstrate a practical use case of the python-barcode library. We will build a simple inventory management system that generates barcodes for products and stores relevant information in a CSV file.

Step 1: Install Required Packages

Ensure you have the python-barcode and pandas libraries installed. Pandas will be used to handle the data and save it to a CSV file.

pip install python-barcode pandas

Step 2: Import Modules

import pandas as pd
from barcode import EAN13
from barcode.writer import ImageWriter

Step 3: Define Functions

def generate_barcode(number):
    return EAN13(number, writer=ImageWriter())

def save_barcode(barcode, filename):

def add_to_inventory(name, barcode, quantity):
    data = {'Product Name': [name], 'Barcode': [barcode], 'Quantity': [quantity]}
    df = pd.DataFrame(data)
    if not pd.api.types.is_string_dtype(df['Barcode']):
        df['Barcode'] = df['Barcode'].astype(str)
    df.to_csv('inventory.csv', mode='a', header=not pd.api.types.is_string_dtype(df['Barcode']), index=False)

Step 4: Get User Input and Generate Barcode

if __name__ == "__main__":
    product_name = input('Enter the product name: ')
    quantity = int(input('Enter the quantity: '))
    barcode_number = input('Enter a 12-digit number to generate a barcode: ')

    barcode = generate_barcode(barcode_number)
    barcode_filename = f"{product_name}_barcode.png"
    save_barcode(barcode, barcode_filename)

    add_to_inventory(product_name, barcode_number, quantity)
    print(f"Barcode generated and saved as {barcode_filename}.")
    print("Data added to inventory.")

Step 5: Run the Inventory Management System

Run the script, and the program will prompt you to enter the product name, quantity, and 12-digit number for the product. The script will generate a barcode, save it as an image file, and add the product information to the CSV file 'inventory.csv'.


The python-barcode library is a powerful tool that simplifies the process of generating barcodes in Python. It supports various barcode formats and can be used in a wide range of applications, from product labeling to asset management. By following the step-by-step guide and final project in this article, you can easily integrate barcode generation into your Python projects.