Make your own QR code generator in Python (30 lines)
When you need to bulk-generate hundreds or thousands of QR codes from a spreadsheet, a script beats clicking. Here is a 30-line Python CLI.
When to script it
The Smartees QR generator is perfect for one-off codes. For batches — event tickets with unique URLs, asset tags with serial numbers, voucher campaigns — you want a script that reads a CSV and outputs a folder of PNGs or SVGs.
Python's qrcode library (MIT) is the easiest path. It is one dependency, the API is two lines, and it produces clean output identical to what server-side QR APIs would generate.
Install
pip install qrcode[pil]The [pil] extra pulls in Pillow for PNG output. Without it you can still write SVG.
The 30-line script
#!/usr/bin/env python3
"""bulk_qr.py — read input.csv, write QR PNGs into ./out/
input.csv format:
filename,content
ticket-001,https://event.example/t/001
ticket-002,https://event.example/t/002
"""
import csv
import sys
from pathlib import Path
import qrcode
from qrcode.constants import ERROR_CORRECT_M
def main(csv_path: str, out_dir: str = "out") -> None:
Path(out_dir).mkdir(exist_ok=True)
with open(csv_path, newline="", encoding="utf-8") as f:
reader = csv.DictReader(f)
for i, row in enumerate(reader, 1):
qr = qrcode.QRCode(
version=None,
error_correction=ERROR_CORRECT_M,
box_size=10,
border=2,
)
qr.add_data(row["content"])
qr.make(fit=True)
img = qr.make_image(fill_color="#0059b5", back_color="#ffffff")
img.save(Path(out_dir) / f"{row['filename']}.png")
print(f"[{i:>4}] {row['filename']}.png")
if __name__ == "__main__":
main(sys.argv[1] if len(sys.argv) > 1 else "input.csv")Run it
python bulk_qr.py input.csvThis produces out/ticket-001.png, out/ticket-002.png and so on. On a modern laptop, 1,000 QR codes take about 20 seconds.
Variations
- SVG output: replace the
img = qr.make_image(...)+img.save(...)block withfrom qrcode.image.svg import SvgImage; img = qr.make_image(image_factory=SvgImage); img.save(Path(out_dir) / f"{row['filename']}.svg"). - Higher error correction: change
ERROR_CORRECT_MtoERROR_CORRECT_H. Use H if you plan to overlay a logo. - Centre logo: load a logo with Pillow, paste it onto the QR image centre. There are recipes in the qrcode library docs.
- PDF sheet: pipe the PNGs into ReportLab or weasyprint to lay out a printable sheet of 8 per A4 page.
When to switch back to a web generator
If your non-technical teammate needs to make one Wi-Fi QR for the office, do not make them install Python. Send them to the Smartees generator and let them click through.
The Python script is for the cases where clicking 500 times would be a bad use of anyone's morning.
Other languages
- Node.js:
npm i qrcode— almost identical API. - Go:
github.com/skip2/go-qrcode— fastest of the bunch for huge batches. - Rust:
qrcodecrate — pairs nicely with image manipulation. - Shell one-liner:
qrencode -o ticket.png "https://event.example/t/001"if you just need one off the command line.
For everyday single-QR work, stick with the Smartees generator. The script above is for the day you need a thousand.
QR Generator
Free, browser-side, one sign-in for downloads.