Python Generate QR Code using pyqrcode module?

A QR code consists of black squares arranged in a square grid on a white background, which can be read by an imaging device such as a camera. It is widely used for commercial tracking applications, payment systems, and website login authentication. The pyqrcode module is used to generate QR codes in Python. There are four standardized encoding modes: numeric, alphanumeric, byte/binary, and kanji to store data efficiently.

Installation

First, install the pyqrcode module using pip ?

pip install pyqrcode

Basic QR Code Generation

We use the pyqrcode.create() function to generate a QR code. The code is saved as an SVG file and scaled to the proper size as required ?

import pyqrcode

# String input for the QR code
text = "learnPython"

# Generate QR code
qr_code = pyqrcode.create(text)

# Create and save the SVG file
qr_code.svg("qrcode_basic.svg", scale=8)
print("QR code generated successfully!")
QR code generated successfully!

Alphanumeric Mode QR Code

The alphanumeric mode is optimized for data containing only digits, uppercase letters, and certain symbols. This mode provides better compression for such data ?

import pyqrcode

# Alphanumeric string input
text = "HELLO123"

# Generate QR code in alphanumeric mode
qr_code = pyqrcode.create(text, mode='alphanumeric')

# Create and save the SVG file
qr_code.svg("qrcode_alpha.svg", scale=8)
print(f"Alphanumeric QR code created for: {text}")
Alphanumeric QR code created for: HELLO123

Binary Mode QR Code

Binary mode can encode any type of data including special characters and non-ASCII text. The QR code pattern may look different due to the encoding method used ?

import pyqrcode

# String input for the QR code
text = "learnPython@2024!"

# Generate QR code in binary mode
qr_code = pyqrcode.create(text, mode='binary')

# Create and save the SVG file
qr_code.svg("qrcode_binary.svg", scale=8)
print(f"Binary QR code created for: {text}")
Binary QR code created for: learnPython@2024!

Numeric Mode QR Code

Numeric mode is the most efficient for encoding only digits. It provides the highest data density for numeric data ?

import pyqrcode

# Numeric string input
number = "1234567890"

# Generate QR code in numeric mode
qr_code = pyqrcode.create(number, mode='numeric')

# Create and save the SVG file
qr_code.svg("qrcode_numeric.svg", scale=8)
print(f"Numeric QR code created for: {number}")
Numeric QR code created for: 1234567890

Comparison of QR Code Modes

Mode Data Type Efficiency Use Case
Numeric Digits only Highest Phone numbers, IDs
Alphanumeric A-Z, 0-9, symbols High URLs, codes
Binary Any byte data Medium Text with special chars
Kanji Japanese characters High (for Kanji) Japanese text

Complete Example with Error Handling

import pyqrcode

def generate_qr_code(data, filename, mode='binary', scale=8):
    try:
        # Generate QR code
        qr_code = pyqrcode.create(data, mode=mode)
        
        # Save as SVG file
        qr_code.svg(filename, scale=scale)
        print(f"QR code saved as {filename}")
        
        # Display QR code properties
        print(f"Mode: {qr_code.mode}")
        print(f"Version: {qr_code.version}")
        
    except Exception as e:
        print(f"Error generating QR code: {e}")

# Generate QR codes with different modes
generate_qr_code("https://tutorialspoint.com", "website.svg", "alphanumeric")
generate_qr_code("Contact: +1-234-567-8900", "contact.svg", "binary")
QR code saved as website.svg
Mode: alphanumeric
Version: 3
QR code saved as contact.svg
Mode: binary
Version: 4

Conclusion

The pyqrcode module provides an easy way to generate QR codes in Python with different encoding modes. Choose the appropriate mode based on your data type: numeric for digits, alphanumeric for uppercase text, and binary for general text with special characters.

Updated on: 2026-03-15T18:38:20+05:30

585 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements