Python ReportLab – Generate QR codes

In this post we will be creating a Python script that will generate QR codes using the ReportLab library. ReportLab is a powerful Python library that lets you create professional-looking PDF documents. It also provides support for generating various types of barcodes as well as QR codes. QR codes store information in 2D barcodes that smartphones can easily scan, making them a convenient way to quickly access data.

See the sample of Python code below where we use the ReportLab library to generate QR codes. We start by creating a new PDF document called ‘qrcode.pdf’ and assign it to the variable ‘myCanvas’. We then create a new QrCodeWidget object named qr_code with the URL “https://www.scriptopia.co.uk” as its parameter. A new Drawing object named qr_code_drawing is then created and the qr_code object is added to this. The qr_code_drawing object is then rendered to the canvas at position (50, 50). The PDF is then saved and the canvas is closed

from reportlab.graphics.shapes import Drawing
from reportlab.graphics import renderPDF
from reportlab.graphics.barcode import qr
from reportlab.pdfgen import canvas

myCanvas = canvas.Canvas("qrcode.pdf")

qr_code = qr.QrCodeWidget('https://www.scriptopia.co.uk')

qr_code_drawing = Drawing()

qr_code_drawing.add(qr_code)

renderPDF.draw(qr_code_drawing, myCanvas, 50, 50)

myCanvas.save()

An image of the barcode in our PDF can be seen below.

Python ReportLab - Generate QR codes - QR code for Scriptopia.co.uk

Take a look at some of our other content around the Python programming language by clicking here.

Leave a Reply