AttributeError:'_io.BufferedReader'对象没有属性'page

时间:2020-11-02 08:38:16

标签: python pdf text-extraction pypdf2

`我正在尝试从包含文本,表格和图像的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)

1 个答案:

答案 0 :(得分:0)

您是从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)