I am working on reading PDF files with Python using the PyPDF2 library but it is throwing an error. This is the code I use to read a PDF stored in the same folder I run my script from:
def pdf_reader():
book = open("spm.pdf","rb")
pdfReader = PyPDF2.PdfFileReader(book)
pages = pdfReader.numPages
speak(f"Total number of pages in this book are {pages}")
speak("Sir please enter the page number which you want me to read")
pg = int(input("enter the page number here : "))
page = pdfReader.getPage(pg)
text = page.extractText()
speak(text)
But it is throwing the following error:
Traceback (most recent call last):
File "c:\Users\Ishu\Desktop\jarvis\jarvis.py", line 269, in run
TaskExecution()
File "c:\Users\Ishu\Desktop\jarvis\jarvis.py", line 261, in TaskExecution
pdf_reader()
File "c:\Users\Ishu\Desktop\jarvis\jarvis.py", line 93, in pdf_reader
pdfReader = PyPDF2.PdfFileReader(book)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Ishu\AppData\Local\Programs\Python\Python311\Lib\site-packages\PyPDF2\_reader.py", line 1974,
in __init__
deprecation_with_replacement("PdfFileReader", "PdfReader", "3.0.0")
File "C:\Users\Ishu\AppData\Local\Programs\Python\Python311\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\Ishu\AppData\Local\Programs\Python\Python311\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.