无法使用PyPDF2从PDF文件中获取文本

2024-09-29 21:49:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从我用PyPDF下载的PDF文件中获取文本。 这是我的代码:

if not PyPDF2.PdfFileReader('download.pdf').isEncrypted:
    PyPDF2.PdfFileReader('download.pdf').getPage(0).extractText()

这是输出:

'\n\n˘ˇ˘ˆ˙\n˝˛˚˜!\n\n\n\n#\nˇ˘ˆ˙ˆ˝˛˝\n˙˙˘ ˘ˆ"˝\n$!%˙(˝)˙*˜+,˝-.#/.(#0)0)/.1.+02345.\n˛˛ˇ/#.$/0/70/#.+322.32˙˘˛˘˘\n˛˘ 8˙˘9:˘ˆ;\n˛˘\n\n˝=\n˙˘˛\n.ˇ<9:˘ˇˇ%˘˛ˇ ˘˘<˘\n˝>"?˝˘$@<˘*ˆˆ˘˙˘A˘B˘˙˘˛ˇ!˛˘˙˘˛ˇ˘\n1C˙ˆ˘06˛˘8+˛9:˘D10+E˝ˆ˘8\n$˘˘9:˘˘1C˙ˆ˘+˘F˛˘D$1+FE˝˘˛˘˘<˘?˝\n////)*˘1˘˛ ?GG˜*HI\nD˘˙A˘E\nJ$\n˛\nDLE///M˛˝˛˙˘˛˘˛\n˛˘˛>"?\n˙˘˛\n˛\n/)M6;˝˛˙˘˛˘\n˛\n///˛\n\n'

当我打开它的文件内容时。另外,当我使用另一个程序将pdf转换成txt时,效果很好。这是一个网页上的javascript呈现的pdf,不知道这是否有什么不同。在


Tags: 文件代码文本ifpdfdownloadnotpypdf
2条回答

以下内容摘自文档(https://pythonhosted.org/PyPDF2/PageObject.html

extractText() Locate all text drawing commands, in the order they are provided in the content stream, and extract the text. 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. Returns: a unicode string object.

因此,这个函数的性能似乎取决于pdf本身。

在win7,python3.6下,我遇到了PyPDF2没有正确编码一些PDF文件的问题。我的解决办法是六号乘客.

pip install pdfminer.six

要从PDF中提取文本,可以使用类似于本文中的函数:https://stackoverflow.com/a/42154976/9524424

很适合我。。。

相关问题 更多 >

    热门问题