Bulk Create SVG QR Codes from CSV file with Python

Yesterday a client reached out asking if I could generate url SVG QR codes from a CSV file. For those who don't, url QR codes are a great way to connect your physical offerings to your online resources. Creating it in SVG makes it easy to print in any resolution.

My first move was to google for existing services that would allow me to do this quickly. To my surprise, I've found that most companies in this space either only create Dynamic Codes (which encode the QR code with their redirect url, meaning unless you subscribe to a plan, it's cap) or allow for Static Codes (your entered url) but no bulk creation.

After a few hours of frustration, I've decided to hack it myself.

So, for the purpose of this article, let's assume this is the csv file we got from the client:

http://domain.test/foo,foo
http://domain.test/bar,bar
http://domain.test/baz,baz

So, we basically have [URL, NAME] format. So let's create a directory and files where we will code this baby:

./qrcode-gen/
├── qrcode-gen.py
├── urls.csv
├── codes/

We will be coding this in Python, since it can run virtually anywhere. Installing and configuring Python in your machine is beyond the scope of this article, but I assume you have python3 running on your machine.

You will need qrcode 6.1. For a standard install (which will include pillow for generating images), run:

pip install qrcode[pil]

Open the qrcode-gen.py file and let's start coding. First we will load urls.csv and loop through the lines:

import csv

with open('urls.csv') as csvFile:
  csvReader = csv.reader(csvFile, delimiter=',')
  for line in csvReader:
    # destructure the line array into variables!
    url, filename = line
    # let's just print for now
    print(url)

If you run python qrcode-gen.py, you should see a print out of the urls on the file:

~ python qrcode-gen.py
http://domain.test/foo
http://domain.test/bar
http://domain.test/baz
~

Now, let's add the code that will actually generate the QR codes:

import csv
import qrcode
import qrcode.image.svg

factory = qrcode.image.svg.SvgPathImage

with open('urls.csv') as csvFile:
  csvReader = csv.reader(csvFile, delimiter=',')
  for line in csvReader:
    # destructure the line array into variables!
    url, filename = line
    # let's just print for now
    print(url)
    # generate the QR code SVG using the url
    img = qrcode.make(url, image_factory = factory)
    img.save(f'codes/{filename}.svg')
    print(f'{filename}.svg created with {url} url')

Let's unpack: the qrcode.make function takes the url as the data parameter, and an image_factory parameter that generates the code as SVG. You can change the SVG factory to fit your needs. For more you can check the python-qrcode library.

Now when we run the python qrcode-gen.py again, you should see the following:

~ python qrcode-gen.py
foo.svg created with http://domain.test/foo url
bar.svg created with http://domain.test/bar url
baz.svg created with http://domain.test/baz url
~

And inside the codes directory, the SVG QR codes.

All in all, this is pretty performant, and it's a quick way to generate the codes. Keep in mind that once create and printed, the URL can't be change unless regenerated. But you can encode with a url shortener, like Bitly, and update the url destination as needed.

Here is the complete python code:

import csv
import qrcode
import qrcode.image.svg

factory = qrcode.image.svg.SvgPathImage

with open('urls.csv') as csvFile:
  csvReader = csv.reader(csvFile, delimiter=',')
  for line in csvReader:
    # destructure the line array into variables!
    url, filename = line

    # generate the QR code SVG using the url
    img = qrcode.make(url, image_factory = factory)

    img.save(f'codes/{filename}.svg')

    print(f'{filename}.svg created with {url} url')