Generate a Wi-Fi QR code using Python in 4 Lines

Hello Pythonistas, welcome back today we will generate a wi-fi QR code using python.

It’s a fun mini-program that can be used for various Python scripts and projects.

Install and Import

pip install wifi-qrcode-generator
from wifi_qrcode_generator import wifi_qrcode

What it does: This line imports the wifi_qrcode function from the wifi_qrcode_generator module.

Purpose: The wifi_qrcode function is used to generate a QR code for a Wi-Fi network that can be scanned to connect a device to the network without manually entering the credentials.

Encode Credentials In Qr Code Object

qr_code = wifi_qrcode("python-hub", hidden=False, authentication_type="WPA", password="P1@3#slfje")

What it does: This line calls the wifi_qrcode function with specific arguments to generate a Wi-Fi QR code.

  • "python-hub": The SSID (network name) of the Wi-Fi.
  • hidden=False: Specifies whether the Wi-Fi network is hidden (True) or visible (False).
  • authentication_type="WPA": Specifies the authentication type used by the network (e.g., "WPA", "WEP", or "nopass" for open networks).
  • password="P1@3#slfje": The password for the Wi-Fi network.

Purpose: This line creates a QR code object that encodes the Wi-Fi credentials provided.

Generate Image

qr_code_img = qr_code.make_image()

What it does: This line generates an image from the QR code object created in the previous step.

Purpose: Converts the QR code data into an image object that can be saved or displayed.

Save The Image

qr_code_img.save("wifi_qr_code.png")

What it does: This line saves the generated QR code image as a file named "wifi_qr_code.png".

Purpose: Stores the QR code image on disk so it can be shared or printed for others to scan.

Full Source Code: Generate a Wi-Fi QR code using Python

from wifi_qrcode_generator import wifi_qrcode

qr_code = wifi_qrcode("python-hub", hidden = False, authentication_type = "WPA", password = "P1@3#slfje")
 
qr_code_img = qr_code.make_image()

qr_code_img.save("wifi_qr_code.png")

Leave a Reply