我使用的是Python 3.4,需要从PDF中提取所有文本,然后使用它进行文本处理。
我看到的所有答案都建议使用Python2.7。
我需要一些Python 3.4的东西。
邦森
发布于 2015-09-19 22:24:21
你需要安装PyPDF2模块才能在Python3.4中处理PDF。PyPDF2无法提取图像、图表或其他媒体,但它可以提取文本并将其作为Python字符串返回。要安装它,请从命令行运行pip install PyPDF2
。此模块名称区分大小写,因此请确保键入'y‘为小写,所有其他字符均为大写。
>>> import PyPDF2
>>> pdfFileObj = open('my_file.pdf','rb') #'rb' for read binary mode
>>> pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
>>> pdfReader.numPages
56
>>> pageObj = pdfReader.getPage(9) #'9' is the page number
>>> pageObj.extractText()
最后一条语句返回'my_file.pdf‘文档第9页中可用的所有文本。
发布于 2018-01-05 03:18:05
其他地方也推荐使用pdfminer.six ( https://github.com/pdfminer/pdfminer.six )来支持Python3,但我个人不能保证,因为它在安装MacOS时失败了。(这方面有一个悬而未决的问题,而且似乎是最近出现的问题,因此可能会有一个快速的解决方案。)
发布于 2019-12-29 07:45:11
slate3k很适合于提取文本。我使用Python3.7.3测试了几个PDF文件,例如,它比PyPDF2准确得多。它是slate的一个分支,是PDFMiner的包装器。下面是我使用的代码:
import slate3k as slate
with open('Sample.pdf', 'rb') as f:
doc = slate.PDF(f)
doc
#prints the full document as a list of strings
#each element of the list is a page in the document
doc[0]
#prints the first page of the document
这条评论归功于GitHub:https://github.com/mstamy2/PyPDF2/issues/437#issuecomment-400491342
https://stackoverflow.com/questions/32667398
复制相似问题