gpt4 book ai didi

python - pyPdf 无法从我的 PDF 中的某些页面中提取文本

转载 作者:太空狗 更新时间:2023-10-29 22:17:05 25 4
gpt4 key购买 nike

我正在尝试使用 pyPdf 从多页 PDF 中提取和打印页面。问题是,文本不是从某些页面中提取的。我在这里放了一个示例文件:

http://www.4shared.com/document/kmJF67E4/forms.html

如果运行以下命令,前 81 页不返回文本,而最后 11 页正确提取。谁能帮忙?

from pyPdf import PdfFileReader  
input = PdfFileReader(file("forms.pdf", "rb"))
for page in input1.pages:
print page.extractText()

最佳答案

请注意,extractText() 在正确提取文本方面仍然存在问题。来自 extractText() 的文档:

This works well for some PDF files, but poorly for others, depending on the generator used. This will be refined in the future. Do not rely on the order of text coming out of this function, as it will change if this function is made more sophisticated.

既然是你想要的文本,那么可以使用Linux命令pdftotext

要使用 Python 调用它,您可以这样做:

>>> import subprocess
>>> subprocess.call(['pdftotext', 'forms.pdf', 'output'])

文本从 forms.pdf 中提取并保存到 output

这适用于您的 PDF 文件并提取您想要的文本。

关于python - pyPdf 无法从我的 PDF 中的某些页面中提取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4203414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com