Ishu Miglani
Ishu Miglani

Reputation: 37

Getting an error while working with pdf in python

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.

Upvotes: 1

Views: 5890

Answers (1)

Said Akyuz
Said Akyuz

Reputation: 498

the deprecation is because of newly published(22.12.2022) third version of PyPDF2. You can solve this by specifying the version of PyPDF2 in the requirements.txt file or you can check the document of PyPDF2 to learn about new function to do the same job.

As follows you can see the helpful links:

https://pip.pypa.io/en/stable/user_guide/#requirements-filesstrong text https://pypi.org/project/PyPDF2/#history

Upvotes: 2

Related Questions