Python - PyPDF2错过了大量的文本。 Windows上的任何替代方案?

问题描述 投票:-1回答:1

我试图用PyPDF2解析一个pdf文件,但我只检索了大约10%的文本。对于剩余的90%,pyPDF2仅带回换行...有点令人沮丧。

你知道在Windows上运行Python的任何替代方案吗?我听说过pdftotext,但似乎我无法安装它,因为我的电脑不能在Linux上运行。

任何的想法?

import PyPDF2

filename = 'Doc.pdf'
pdf_file = PyPDF2.PdfFileReader(open(filename, 'rb'))

print(pdf_file.getPage(0).extractText())
python parsing pdf pypdf
1个回答
0
投票

试试PyMuPDF。以下示例只打印出找到的文本。该库还允许您获取文本的位置,如果这将有助于您。

#!python3.6
import json

import fitz  # http://pymupdf.readthedocs.io/en/latest/


pdf = fitz.open('2018-04-17-CP-Chiffre-d-affaires-T1-2018.pdf')
for page_index in range(pdf.pageCount):
    text = json.loads(pdf.getPageText(page_index, output='json'))
    for block in text['blocks']:
        if 'lines' not in block:
            # Skip blocks without text
            continue
        for line in block['lines']:
            for span in line['spans']:
                print(span['text'].encode('utf-8'))
pdf.close()
© www.soinside.com 2019 - 2024. All rights reserved.