最近在研究如何提取 pdf 和 word 中的文本信息 <br>

需求说明 <br>

同学在开发一款校园服务的小程序,名字叫做《上应小风筝》,具体见上应小风筝。现打算实现一个信息检索功能,学校官网每天都会发一些各式各样的文件,word、excel、pdf,信息量不仅大,而且种类繁杂混乱,我们希望提供信息检索服务,学生只需要提供关键字,就能找到相应的文件的内容。我们采用的方法是,先提取这些文件的内容,然后进行分词,将分词和文件及其页码形成映射,然后将当前页码的内容转换为图片展现给用户。

# 提取 PDF 的文本信息

python 有库 pdfminer3k,直接使用 pip 安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
fp = open(self._doc_path, 'rb')
parser = PDFParser(fp)
doc = PDFDocument()
parser.set_document(doc)
doc.set_parser(parser)
doc.initialize()
if not doc.is_extractable:
raise PDFTextExtractionNotAllowed
else:
rsrcmgr = PDFResourceManager()
laparams = LAParams()
device = PDFPageAggregator(rsrcmgr,laparams=laparams)
interpreter = PDFPageInterpreter(rsrcmgr, device)
# all content
content = []
for page in doc.get_pages():
interpreter.process_page(page)
layout = device.get_result()
# content in one page
content_single_page = ""
for x in layout:
if (isinstance(x, LTTextBoxHorizontal)):
content_single_page += x.get_text().strip()
content.append(content_single_page)
return content

其中 page 为页码

# 提取 WORD 的文本信息

# PDF 转图片