Use python to create a QR code

Submitted by code_admin on Tue, 09/29/2020 - 10:38

This is a simple way I have determined I can use python to create a QR code.

Quick steps I have used: https://medium.com/@rahulmallah785671/create-qr-code-by-using-python-23….
TODO Next time paste code here

Install python3

You must have python and pip installed

Install requirements

This example requires a library called reportlab

  1. pip install reportlab==3.5.48

Run python scipt

The following code will generate a single QR code which links to this page. It is output as both a pdf and png file.

  1. print("Start")
  2.  
  3. from reportlab.graphics.barcode import qr
  4. from reportlab.graphics.shapes import Drawing
  5.  
  6. qrcodeText=qrcodeText.strip()
  7.  
  8. qr_code = qr.QrCodeWidget(qrcodeText)
  9.  
  10. bounds = qr_code.getBounds()
  11. width = bounds[2] - bounds[0]
  12. height = bounds[3] - bounds[1]
  13. x = 4000
  14. drawing = Drawing(x, x, transform=[float(x)/width, 0, 0, float(x)/height, 0, 0])
  15. drawing.add(qr_code)
  16.  
  17.  
  18. drawing.save(
  19.   formats=["pdf", "png"], verbose=None, fnRoot='OutputQRCode', outDir=None, title='ExampleQRCode'
  20. )
  21.  
  22.  
  23. print("End")

You can change the value of x to change the resolution of the output image.

reportlabs has not got good documentation. I found this page useful in working out what save did: https://pydoc.net/reportlab/3.3.0/reportlab.graphics.shapes/

This is their main site https://www.reportlab.com/
(I am using their opensource python library)

RJM Article Type
Step by Step