Qr code read

Joined
May 8, 2023
Messages
5
Reaction score
0
how can we read qr codes in 1st page from a pdf file
and verify the same qr code is present in all the other pages of the file
 
Last edited:
Joined
Mar 31, 2023
Messages
95
Reaction score
8
You can use the PyPDF2 and pyzbar libraries to read QR codes from a PDF file and verify if the same QR code is present in all the other pages of the file. Here's an example Python code that shows how to do this:
Python:
import PyPDF2
from pyzbar.pyzbar import decode
from PIL import Image

# Open the PDF file and get the first page
pdf_file = open('file.pdf', 'rb')
pdf_reader = PyPDF2.PdfFileReader(pdf_file)
page = pdf_reader.getPage(0)

# Convert the first page to an image
page_image = page.getPixmap().getImage()

# Decode the QR code from the first page image
qr_code = decode(page_image)[0].data.decode('utf-8')

# Loop through all the other pages and check if the same QR code is present
for i in range(1, pdf_reader.getNumPages()):
    page = pdf_reader.getPage(i)
    page_image = page.getPixmap().getImage()
    if qr_code not in [code.data.decode('utf-8') for code in decode(page_image)]:
        print(f"The QR code {qr_code} is not present on page {i+1}")
        break
else:
    print("The QR code is present in all pages")
    
pdf_file.close()

This code reads the first page of the PDF file and converts it to an image. Then it uses the pyzbar library to decode the QR code from the image. Next, it loops through all the other pages of the PDF file and checks if the same QR code is present on each page. If the QR code is not present on any page, it breaks the loop and prints a message saying that the QR code is not present on that page. If the loop completes successfully without finding any page without the QR code, it prints a message saying that the QR code is present in all pages.
 
Joined
May 8, 2023
Messages
5
Reaction score
0
Sir,some errors occur
Traceback (most recent call last):
File "c:\Users\php\PycharmProjects\newforum\new.py", line 15, in <module>
pdf_reader = PyPDF2.PdfFileReader(pdf_file)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\php\PycharmProjects\pythonwork\benv\Lib\site-packages\PyPDF2\reader.py", line 1974, in __init_
deprecation_with_replacement("PdfFileReader", "PdfReader", "3.0.0")
File "C:\Users\php\PycharmProjects\pythonwork\benv\Lib\site-packages\PyPDF2\_utils.py", line 369, in deprecation_with_replacement
deprecation(DEPR_MSG_HAPPENED.format(old_name, removed_in, new_name))
File "C:\Users\php\PycharmProjects\pythonwork\benv\Lib\site-packages\PyPDF2\_utils.py", line 351, in deprecation
raise DeprecationError(msg)
PyPDF2.errors.DeprecationError: PdfFileReader is deprecated and was removed in PyPDF2 3.0.0. Use PdfReader instead.
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top