SoFunction
Updated on 2024-11-10

Generate various common barcodes and QR codes using python

preamble

Barcodes and QR codes are important tools for modern information exchange and data storage. They encode information in the form of graphics for easy machine identification and data processing, and are widely used in various fields such as logistics, retail, healthcare and education.

This article will explain how toUse Python to quickly generate a variety of common barcodes such as Code 128, EAN-13, as well as to generate QR codes

Python Barcode Library

This article requires the use of for Python The library supports generating and recognizing all kinds of 1D and 2D barcodes, including EAN, UPC, Code128, Code39, QR Code, Data Matrix and so on, to meet various application requirements. In addition, it provides rich interfaces to customize the border style, color, margin and other attributes of barcode images.
This Python barcode generation library can be easily installed via pip:

pip install 

Introduction to classes and methods for generating barcodes

To generate barcodes using the for Python library, the following classes, properties, and methods are used:

  1. BarcodeSettings Class: Various settings for barcode generation. Support settings for barcode type, data, background color, width, height, margins, resolution, etc..
  2. Property: Sets the barcode type.
  3. Properties: Sets the barcode data.
  4. BarcodeSettings.Data2D Properties: Set the QR code text.
  5. BarcodeGenerator Class: for barcode rendering.
  6. () Method: Used to generate barcode images.

The following is an example of Python code to generate barcodes

Python Generate Code128

from  import *

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

# Create the BarcodeSettings object
barcodeSettings = BarcodeSettings()

# Set barcode type to Code128
 = BarCodeType.Code128

# Setting up barcode data
 = "XD00555"

# Setting the Code128 barcode code set
barcodeSettings.Code128SetMode = 

# Create the BarCodeGenerator object
barCodeGenerator = BarCodeGenerator(barcodeSettings)

# Generate barcode images
barcodeimage = ()
WriteAllBytes("", barcodeimage)

Barcode Chart:

Python Generate EAN-13 Code

from  import *

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

# Create the BarcodeSettings object
barcodeSettings = BarcodeSettings()

# Set barcode type to EAN13
 = BarCodeType.EAN13

# Setting up barcode data
 = "5019632805254"

# Create the BarCodeGenerator object
barCodeGenerator = BarCodeGenerator(barcodeSettings)

# Generate barcode images
barcodeimage = ()
WriteAllBytes("", barcodeimage)

Barcode Chart:

Python Generate QR Code QR Code

from  import *

def WriteAllBytes(fname:str,data):
    fp = open(fname,"wb")
    (data)
    ()

# Create the BarcodeSettings object
barcodeSettings = BarcodeSettings()

# Set barcode type to QR Code
 = 

# Set the background color
 = Color.get_WhiteSmoke()

# Set the encoding mode of QR Code
 = 

# Set the error correction level (ECL)
 = 

# Set whether to display text at the bottom of the QR code
 = True

# Set resolution
 = 500
 = 500

# Setting up QR code data
barcodeSettings.Data2D = "Hello, World"

# Create the BarCodeGenerator object
barCodeGenerator = BarCodeGenerator(barcodeSettings)

# Generate QR code images
barcodeimage = ()
WriteAllBytes("", barcodeimage)

Barcode Chart:

to this article on the use of python to generate a variety of common barcode and two-dimensional code of the article is introduced to this, more related to python to generate barcodes and two-dimensional code content, please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!