Skip to content

Instantly share code, notes, and snippets.

@pont-us
Last active May 20, 2022 18:01
Show Gist options
  • Save pont-us/60633d8eb904020a4942a9b22aac8ed7 to your computer and use it in GitHub Desktop.
Save pont-us/60633d8eb904020a4942a9b22aac8ed7 to your computer and use it in GitHub Desktop.
Create a WiFi QR code as a PDF or printout
#!/usr/bin/env python3
"""Create a wifi QR code as a PDF or printout.
This script creates a small sign showing a WiFi network name (SSID) and
password along with a QR code encoding this information in a format which
allows Android devices to connect automatically to the network by scanning
the code.
Requires: pdflatex, qrencode
By Pontus Lurcock, 2019. Released into the public domain.
"""
from tempfile import TemporaryDirectory
import argparse
import subprocess
import os.path
import shutil
def main():
parser = argparse.ArgumentParser(
description="Create a WiFi QR code as a PDF or printout. "
"If no filename or device specified, prints to the default printer.")
parser.add_argument("--output", "-o", metavar="filename",
help="write PDF to specified file")
parser.add_argument("--printer", "-p", metavar="printer-id",
help="print to specified printer")
parser.add_argument("network", type=str)
parser.add_argument("password", type=str)
args = parser.parse_args()
with TemporaryDirectory() as tempdir:
make_label(args, tempdir)
def make_label(args, tempdir):
with open(os.path.join(tempdir, "sign.tex"), "w") as fh:
fh.write(template % (args.network, args.password))
wifi_code = "WIFI:T:WPA;S:%s;P:%s;;" % (args.network, args.password)
subprocess.call(["qrencode", "-t", "eps", "-o", "qr-image.eps",
"-m", "3", wifi_code], cwd=tempdir)
subprocess.call(["pdflatex", "sign.tex"],
cwd=tempdir)
subprocess.call(["pdflatex", "sign.tex"],
cwd=tempdir)
if args.output:
shutil.copy2(os.path.join(tempdir, "sign.pdf"),
args.output)
if args.printer:
subprocess.call(["lp",
"-d", args.printer,
os.path.join(tempdir, "sign.pdf")])
if (not args.output) and (not args.printer):
subprocess.call(["lp",
os.path.join(tempdir, "sign.pdf")])
template = r"""
\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[sfdefault]{AlegreyaSans}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\begin{document}
\setlength{\unitlength}{1mm}
\fontsize{5mm}{8mm}\selectfont
\framebox(60,40){\shortstack{Netzwerk: %s\\
\includegraphics[height=25mm]{qr-image}\\
Passwort: %s}}
\end{document}
"""
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment