How to Generate Barcodes and QR Codes in Python: A Guide to Code 128, EAN-13, and Beyond
Barcodes and QR codes have become the backbone of modern data exchange and storage. By encoding information into machine-readable graphics, they make physical items become digital data for processing, powering everything from logistics and retail to healthcare and education.
In this guide, we’ll dive into how you can leverage Python to programmatically generate these identifiers. Whether you need standard linear barcodes like Code 128 and EAN-13 or high-density QR codes, I’ll show you the most efficient libraries and code snippets to get the job done quickly.
Get Ready with Spire.Barcode for Python
To follow along with this tutorial, we will be using the Spire.Barcode for Python library. This is a versatile tool designed for both generating and recognizing a vast array of 1D and 2D barcodes—ranging from standard retail codes like EAN and UPC to industrial formats such as Code 128, Code 39, QR Code, and Data Matrix.
Beyond basic generation, the library provides a rich set of APIs that allow for deep customization. You can easily adjust image borders, colors, margins, and other visual attributes to ensure the output fits your specific design requirements.
Installation:
Setting up the library is straightforward using pip. Run the following command in your terminal:
pip install Spire.Barcode
Additionally, a free version is available for those working on smaller-scale or personal projects.
Core Classes and Methods of Spire.Barcode
To generate barcodes effectively using Spire.Barcode for Python, you should familiarize yourself with several key classes and methods. These components handle everything from data input to visual styling and image rendering.
The BarcodeSettings Class
This class is the foundation of your barcode design. It allows you to configure various global settings, including the barcode type, data content, background colors, dimensions (width and height), margins, and resolution.
Type Property: Defines the specific symbology of the barcode (e.g., Code 128, QR Code).
Data Property: Sets the alphanumeric data for 1D (linear) barcodes.
Data2D Property: Specifically used to set the text or data payload for 2D barcodes like QR codes.
The BarcodeGenerator Class
This class is responsible for the actual rendering process. It takes the configurations defined in BarcodeSettings and transforms them into a visual format.
- GenerateImage() Method: This core function processes the settings and generates the barcode as an image object, which can then be saved or displayed.
Generating Code 128 Barcodes in Python
Code 128 is a high-density, versatile linear barcode symbology capable of encoding all 128 ASCII characters. It is widely favored in logistics and supply chain management because it can store a significant amount of data in a relatively compact space.
Below is a Python implementation using Spire.Barcode to generate a standard Code 128 barcode.
from spire.barcode import *
def WriteAllBytes(fname: str, data):
with open(fname, "wb") as fp:
fp.write(data)
fp.close()
# Create a BarcodeSettings object
barcodeSettings = BarcodeSettings()
# Set the barcode type to Code128
barcodeSettings.Type = BarCodeType.Code128
# Set the barcode data
barcodeSettings.Data = "XD00359"
# Set the Code128 set mode (Auto, A, B, or C)
barcodeSettings.Code128SetMode = Code128SetMode.Auto
# Create a BarCodeGenerator object
barCodeGenerator = BarCodeGenerator(barcodeSettings)
# Generate the barcode image
barcodeimage = barCodeGenerator.GenerateImage()
WriteAllBytes("/output/Code128.png", barcodeimage)
The preview of the generated code:

Generating EAN-13 Barcodes in Python
The EAN-13 (European Article Number) is the international standard for product identification in retail. It consists of 13 digits: the first 12 represent the manufacturer and product details, while the final digit is a check digit calculated to ensure scanning accuracy.
Here is how you can generate a retail-ready EAN-13 barcode using the following script:
from spire.barcode import *
def WriteAllBytes(fname: str, data):
with open(fname, "wb") as fp:
fp.write(data)
fp.close()
# Create a BarcodeSettings object
barcodeSettings = BarcodeSettings()
# Set the barcode type to EAN13
barcodeSettings.Type = BarCodeType.EAN13
# Set the barcode data (Must be 12 or 13 digits)
barcodeSettings.Data = "50196387659372"
# Create a BarCodeGenerator object
barCodeGenerator = BarCodeGenerator(barcodeSettings)
# Generate the barcode image
barcodeimage = barCodeGenerator.GenerateImage()
WriteAllBytes("/output/EAN13.png", barcodeimage)
Here’s what the EAD13 code looks like:

Generating QR Codes in Python
Unlike linear barcodes, the QR Code (Quick Response Code) is a 2-dimensional matrix symbology capable of storing thousands of alphanumeric characters. Its robust error correction and high-speed readability make it the standard for everything from digital payments and marketing to inventory management.
The following script demonstrates how to create a high-resolution QR code with customized background colors and error correction levels:
from spire.barcode import *
def WriteAllBytes(fname:str,data):
fp = open(fname,"wb")
fp.write(data)
fp.close()
# Create a BarcodeSettings object
barcodeSettings = BarcodeSettings()
# Set the barcode type to QR Code
barcodeSettings.Type = BarCodeType.QRCode
# Set the background color
barcodeSettings.BackColor = Color.get_WhiteSmoke()
# Set the QR Code encoding mode (Numeric, AlphaNumeric, Byte, or Kanji)
barcodeSettings.QRCodeDataMode = QRCodeDataMode.Byte
# Set the Error Correction Level (L, M, Q, or H)
barcodeSettings.QRCodeECL = QRCodeECL.M
# Set whether to display text at the bottom of the QR code
barcodeSettings.ShowTextOnBottom = True
# Set the resolution (DPI)
barcodeSettings.DpiX = 500
barcodeSettings.DpiY = 500
# Set the QR code data
barcodeSettings.Data2D = "Hello, World!"
# Create a BarCodeGenerator object
barCodeGenerator = BarCodeGenerator(barcodeSettings)
# Generate the QR code image
barcodeimage = barCodeGenerator.GenerateImage()
WriteAllBytes("/output/QRCode.png", barcodeimage)
The preview of the QR code created by Spire.Barcode:

To Wrap Up
Generating professional-grade barcodes and QR codes in Python is efficient and highly customizable with Spire.Barcode. From standard retail EAN-13 tags to industrial Code 128 labels and versatile QR codes, this library provides the flexibility needed for any data-tracking project.
By mastering these classes and properties, you can seamlessly integrate automated barcode generation into your logistics, retail, or inventory management workflows.