Tag Archives: Python

Generate a QR Code using Python

If you’re a newb like me and you want to learn Python (or any programming language) you start off with learning little things like this.

What you’ll need first

Open up Visual Studio Code and select the language, which should be asking you on line 1. After you can go ahead save the file as a start to your desktop. I saved it as QR Code Generator.py

Visual Studio Code may offer to download the relevant software, so I’d click yes to that. I did however make sure to PIP (pip is the package installer for Python) was installed after by going to command prompt and typing "pip help" which if you get a response and not an error message then all is well. Also using pip install the module PyQRCode, which is what we’ll use "pip install pyqrcode".

The code

import pyqrcode
from pyqrcode import QRCode

s = "https://savas.co.uk/"

url = pyqrcode.create(s)

url.svg("qr.svg", scale = 8)

The first two lines are importing the module to work with/from. The next line is establishing what “s” string represents. After that you can see we’re bringing the ‘s’ string, which is the actual URL and using “pyqrcode” (from the module) to generate the QR code. The last line is how the QR code will be generated/outputted. In this case as a .svg file, with the size 8.

This should save to the location the file is saved at, so in my case the desktop. If you decide to save it as a .PNG file, then you’ll also need the pypng module.

Links & References

How To Install PIP to Manage Python Packages On Windows