Fundamentals 5 min read

Generate Barcodes and QR Codes Instantly with Python and Spire.Barcode

This tutorial demonstrates how to quickly create common 1‑D barcodes like Code 128 and EAN‑13, as well as QR codes, using the Spire.Barcode library in Python, including installation, key classes, and complete code examples.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Generate Barcodes and QR Codes Instantly with Python and Spire.Barcode

Barcodes and QR codes are essential tools for data exchange and storage. This article shows how to quickly generate common barcodes such as Code 128, EAN‑13, and QR codes using Python.

Python Barcode Library

The tutorial uses the Spire.Barcode for Python library, which supports many 1‑D and 2‑D barcode formats and allows customization of border style, color, and margins. Install it via:

pip install Spire.Barcode

Key Classes and Methods

BarcodeSettings – class for configuring barcode generation options such as type, data, background color, size, margins, resolution, etc.

BarcodeSettings.Type – property to set the barcode type.

BarcodeSettings.Data – property to set data for 1‑D barcodes.

BarcodeSettings.Data2D – property to set text for 2‑D codes.

BarcodeGenerator – class used for rendering the barcode.

BarcodeGenerator.GenerateImage() – method that generates the barcode image.

Generate a Code 128 Barcode

from spire.barcode import *

def WriteAllBytes(fname: str, data):
    with open(fname, "wb") as fp:
        fp.write(data)
        fp.close()

# Create settings
barcodeSettings = BarcodeSettings()
barcodeSettings.Type = BarCodeType.Code128
barcodeSettings.Data = "XD00555"
barcodeSettings.Code128SetMode = Code128SetMode.Auto

# Generate image
barCodeGenerator = BarCodeGenerator(barcodeSettings)
barcodeimage = barCodeGenerator.GenerateImage()
WriteAllBytes("Code128.png", barcodeimage)
Code128 barcode
Code128 barcode

Generate an EAN‑13 Barcode

from spire.barcode import *

def WriteAllBytes(fname: str, data):
    with open(fname, "wb") as fp:
        fp.write(data)
        fp.close()

# Create settings
barcodeSettings = BarcodeSettings()
barcodeSettings.Type = BarCodeType.EAN13
barcodeSettings.Data = "5019632805254"

# Generate image
barCodeGenerator = BarCodeGenerator(barcodeSettings)
barcodeimage = barCodeGenerator.GenerateImage()
WriteAllBytes("EAN13.png", barcodeimage)
EAN13 barcode
EAN13 barcode

Generate a QR Code

from spire.barcode import *

def WriteAllBytes(fname: str, data):
    with open(fname, "wb") as fp:
        fp.write(data)
        fp.close()

# Create settings
barcodeSettings = BarcodeSettings()
barcodeSettings.Type = BarCodeType.QRCode
barcodeSettings.BackColor = Color.get_WhiteSmoke()
barcodeSettings.QRCodeDataMode = QRCodeDataMode.Byte
barcodeSettings.QRCodeECL = QRCodeECL.M
barcodeSettings.ShowTextOnBottom = True
barcodeSettings.DpiX = 500
barcodeSettings.DpiY = 500
barcodeSettings.Data2D = "Hello, World"

# Generate image
barCodeGenerator = BarCodeGenerator(barcodeSettings)
barcodeimage = barCodeGenerator.GenerateImage()
WriteAllBytes("QRCode.png", barcodeimage)

During testing, readers can discuss any issues in the comments, and a one‑month trial license can be applied to remove watermarks from generated barcodes.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonCode ExampleQR codebarcodecode128ean13spire.barcode
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.