1

所以我为 python 3.6 安装了 pdfminer3k。我试图在打开 PDF 文件并将其转换为文本时遵循一些示例,它们都需要 PDFPage 导入。这对我来说不存在。有什么解决方法吗?我尝试从在线复制 PDFPage.py 并保存到 python 搜索 pdfminer 的目录,但我刚刚得到......“导入错误:无法导入名称 PDFObjectNotFound”。

谢谢!

4

2 回答 2

1

啊。我猜 PDFPage 不适用于 python 3.6。以下示例来自如何使用 pdfminer3k 读取 pdf 文件?解决了我的问题!

于 2017-06-19T18:41:11.837 回答
0
import io
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfparser import PDFParser, PDFDocument

def extract_text_from_pdf(pdf_path):
    '''
    Iterator: extract the plain text from pdf-files with pdfminer3k

    pdf_path: path to pdf-file to be extracted
    return: iterator of string of extracted text (by page)
    '''
    # pdfminer.six-version can be found at:
    # https://www.blog.pythonlibrary.org/2018/05/03/exporting-data-from-pdfs-with-python/
    with open(pdf_path, 'rb') as fp:
        parser = PDFParser(fp)
        doc = PDFDocument()
        parser.set_document(doc)
        doc.set_parser(parser)
        doc.initialize('')
        for page in doc.get_pages(): # pdfminer.six: PDFPage.get_pages(fh, caching=True, check_extractable=True):
            rsrcmgr = PDFResourceManager()
            fake_file_handle = io.StringIO()
            device = TextConverter(rsrcmgr, fake_file_handle, laparams=LAParams())
            interpreter = PDFPageInterpreter(rsrcmgr, device)
            interpreter.process_page(page)

            text = fake_file_handle.getvalue()
            yield text

            # close open handles
            device.close()
            fake_file_handle.close()

maxPages = 1
for i, t in enumerate(extract_text_from_pdf(fPath)):
    if i<maxPages:
        print(f"Page {i}:\n{t}")
    else:
        print(f"Page {i} skipped!")
于 2020-11-24T09:40:18.270 回答