How to generate QR code in Python

Hi guys,

In this blog post you're going to learn how you can use python to automate generation of QR codes using qr-code module in less than 5 lines of code


pip install qrcode

Let's get started

To create a barcode using the QRcode library is straight forward we just use the make ( ) method to do it just as shown in the example below;

>>> import qrcode 
>>> img = qrcode.make('Codebook price') 


Once you run the above script it will generate an image with a barcode on your project directory with name data.png.

Sample project

Let's assume you own an electronics store, where you sell various electronics components, and you would like each component in your store to have it's own qrcode to simplify selling.

You can use the knowledge we just learned above to do this, whereby you gonna create a list of all the products in your store using python list.

Then at last you just iterate over the list consisting of component descriptions and then generate the qrcode accordingly just as shown below

import qrcode 
products = [
            'Raspberry Pi', 
            'Codebook Manual']

for product in products: 


When the above code is ran, it going to generate images with qrcode of all elements present in our products list.

Hope you find it interesting, now don't be shy share it with fellow developers on how easy its to create QRcode in python.

You can me on Twitter

Kalebu Jordan
I am a Mechatronics engineer and professional Python Developer passionate about innovation, opensource and technical writing, Here I will be writing mostly on Python, DevOps, AI/ML and security
Dar es Salaam, Tanzania