Generate and Copy QR Codes to Your Clipboard Instantly with Python

Michael T. Wagner
2 min readDec 11, 2024

--

simple python script to generate a qr code from a link and copy it to the clipboard

This Python script (use with Jupyter Notebook) generates a QR code for a specified link, saves it as an image file, and copies the QR code directly to the Windows clipboard.
It’s a quick and efficient solution for sharing links without needing third-party services or additional tools.

%pip install qrcode
%pip install pywin32
import qrcode
from PIL import Image
import io
# copy image bitmap to clipboard
import win32clipboard
from io import BytesIO

def generate_qr_code(link, file_name):
qr = qrcode.QRCode(
version=1, # Controls the size of the QR Code
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4, # Minimum is 4 for most QR readers
)
qr.add_data(link)
qr.make(fit=True)

img = qr.make_image(fill_color="black", back_color="white")
img.save(file_name)


# ===================================
# SET YOUR LINK HERE
link = "https://www.synctwin.ai"

# make file name from link
for pattern in ["https://", "http://", "/", "."]:
link = link.replace(pattern, "_")
file_name = link + "_qr_code.png"
generate_qr_code(link, file_name)
print(f"QR code saved as {file_name}")

# Open the image and convert to bitmap format
image = Image.open(file_name)
output = BytesIO()
image.convert("RGB").save(output, "BMP")
data = output.getvalue()[14:] # The file header offset of BMP is 14 bytes
output.close()

# Send to clipboard
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32clipboard.CF_DIB, data)
win32clipboard.CloseClipboard()

print("Image copied to clipboard")


Here’s the result of the code above:

https://www.synctwin.ai

--

--

Michael T. Wagner
Michael T. Wagner

Written by Michael T. Wagner

CTO and Co-Founder @ipolog.ai & synctwin.ai, creating clever solutions for smart factory

No responses yet