在python中处理pdf时出错

b4qexyjb  于 2023-05-16  发布在  Python
关注(0)|答案(1)|浏览(862)

我正在使用PyPDF2库使用Python阅读PDF文件,但它抛出错误。这是我用来读取存储在运行脚本的同一文件夹中的PDF的代码:

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)

但它会抛出以下错误:

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.
z9smfwbn

z9smfwbn1#

弃用是因为新发布的(22.12.2022)PyPDF2的第三个版本。您可以通过在requirements.txt文件中指定PyPDF2的版本来解决这个问题,或者您可以检查PyPDF2的文档以了解新功能来完成相同的工作。
如下所示,您可以看到有用的链接:
https://pip.pypa.io/en/stable/user_guide/#requirements-filesstrong text https://pypi.org/project/PyPDF2/#history

相关问题