我试图使用Python提取包含在这个PDF文件中的文本。
我正在使用PyPDF2包(版本1.27.2),并有以下脚本:
import PyPDF2
with open("sample.pdf", "rb") as pdf_file:
read_pdf = PyPDF2.PdfFileReader(pdf_file)
number_of_pages = read_pdf.getNumPages()
page = read_pdf.pages[0]
page_content = page.extractText()
print(page_content)
当我运行代码时,我得到以下输出,这与PDF文档中包含的输出不同:
! " # $ % # $ % &% $ &' ( ) * % + , - % . / 0 1 ' * 2 3% 4
5
' % 1 $ # 2 6 % 3/ % 7 / ) ) / 8 % &) / 2 6 % 8 # 3" % 3" * % 31 3/ 9 # &)
%
如何提取PDF文档中的文本?
目的:从PDF中提取文本
所需工具:
Poppler for windows: windows中pdftotext文件的包装器
对于anaanaconda: conda install -c conda-forge
pdftotext实用程序转换PDF到文本。
步骤:
安装荡漾。windows操作系统:在env路径下增加“xxx/bin/”
PIP安装pdftotext
import pdftotext
# Load your PDF
with open("Target.pdf", "rb") as f:
pdf = pdftotext.PDF(f)
# Save all text to a txt file.
with open('output.txt', 'w') as f:
f.write("\n\n".join(pdf))
如何从PDF文件中提取文本?
首先要了解的是PDF格式。它有一个用英文编写的公共规范,请参阅ISO 32000-2:2017,并阅读超过700页的PDF 1.7规范。当然,你至少需要阅读维基百科关于PDF的页面
一旦你理解了PDF格式的细节,提取文本或多或少是容易的(但是出现在图形或图像中的文本呢?它的数字1)?不要指望在几周内单独编写一个完美的软件文本提取器....
在Linux上,你也可以使用pdf2text,你可以从你的Python代码中弹出。
一般来说,从PDF文件中提取文本是一个定义不清的问题。对于人类读者来说,一些文本可以由不同的点制成(图形),或者一张照片等等。
谷歌搜索引擎能够从PDF中提取文本,但据传需要超过5亿行的源代码。你有必要的资源(人力和预算)来发展一个竞争对手吗?
一种可能是将PDF打印到一些虚拟打印机(例如使用GhostScript或Firefox),然后使用OCR技术提取文本。
相反,我建议处理生成PDF文件的数据表示,例如原始的LaTeX代码(或Lout代码)或OOXML代码。
在所有情况下,您都需要为至少几个人年的软件开发预算。
在2020年,上述解决方案并不适用于我正在使用的特定pdf。下面是诀窍。我用的是Windows 10和Python 3.8
测试pdf文件:https://drive.google.com/file/d/1aUfQAlvq5hA9kz2c9CyJADiY3KpY3-Vn/view?usp=sharing
#pip install pdfminer.six
import io
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
def convert_pdf_to_txt(path):
'''Convert pdf content from a file path to text
:path the file path
'''
rsrcmgr = PDFResourceManager()
codec = 'utf-8'
laparams = LAParams()
with io.StringIO() as retstr:
with TextConverter(rsrcmgr, retstr, codec=codec,
laparams=laparams) as device:
with open(path, 'rb') as fp:
interpreter = PDFPageInterpreter(rsrcmgr, device)
password = ""
maxpages = 0
caching = True
pagenos = set()
for page in PDFPage.get_pages(fp,
pagenos,
maxpages=maxpages,
password=password,
caching=caching,
check_extractable=True):
interpreter.process_page(page)
return retstr.getvalue()
if __name__ == "__main__":
print(convert_pdf_to_txt('C:\\Path\\To\\Test_PDF.pdf'))
我有一个比OCR更好的工作,并保持页面对齐,同时从PDF中提取文本。应该有帮助:
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from io import StringIO
def convert_pdf_to_txt(path):
rsrcmgr = PDFResourceManager()
retstr = StringIO()
codec = 'utf-8'
laparams = LAParams()
device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
fp = open(path, 'rb')
interpreter = PDFPageInterpreter(rsrcmgr, device)
password = ""
maxpages = 0
caching = True
pagenos=set()
for page in PDFPage.get_pages(fp, pagenos, maxpages=maxpages, password=password,caching=caching, check_extractable=True):
interpreter.process_page(page)
text = retstr.getvalue()
fp.close()
device.close()
retstr.close()
return text
text= convert_pdf_to_txt('test.pdf')
print(text)