【问题标题】:AttributeError: '_io.BufferedReader' object has no attribute 'pageAttributeError: '_io.BufferedReader' 对象没有属性 'page
【发布时间】:2020-11-02 08:38:16
【问题描述】:

`我正在尝试从包含文本、表格和图像的 pdf 文件中提取文本。并希望将该文件保存在本地系统上。这是我正在开发的代码。

from PyPDF2 import PdfFileReader
# Load the pdf to the PdfFileReader object with default settings
with open("SHKelkar.pdf", "rb") as pdf_file:
    pdf_reader = PdfFileReader(pdf_file)
    total_pages = pdf_reader.numPages
    print(total_pages)
    print(f"The total number of pages in the pdf document is {pdf_reader.numPages}")
    for i in range(total_pages):
        page = pdf_file.page[i]
        textdata = page.extract_text()
        print(textdata)

【问题讨论】:

    标签: python pdf text-extraction pypdf2


    【解决方案1】:

    您正在从pdf_file 提取而不是pdf_reader

    检查下面的工作代码。

    from PyPDF2 import PdfFileReader
    # Load the pdf to the PdfFileReader object with default settings
    with open("sample.pdf", "rb") as pdf_file:
        pdf_reader = PdfFileReader(pdf_file)
        total_pages = pdf_reader.getNumPages()
        print(total_pages)
        print(f"The total number of pages in the pdf document is {pdf_reader.numPages}")
        for i in range(total_pages):
            page = pdf_reader.getPage(i)
            textdata = page.extractText()
            print(textdata)
    

    【讨论】:

    • 如何创建CDQA数据集?例如,我从 pdf 中提取数据。从提取的文本数据中,我们如何创建 CDQA 数据集?如何生成 CDQA 之类的数据?
    • 以上答案是否解决了您的问题?对于 cdqa,您可以查看此博客 towardsdatascience.com/…
    猜你喜欢
    • 2016-08-11
    • 2021-08-20
    • 1970-01-01
    • 2022-11-24
    • 2020-08-15
    • 2012-12-01
    • 2021-04-19
    • 2022-09-28
    • 2021-11-22
    相关资源
    最近更新 更多