提取pdf内容,生成合理段落的text文件
import io
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import PDFPageAggregator
from pdfminer.layout import LAParams, LTText, LTChar, LTAnno
import re
def convert2text(fname, pages=None):
with open(fname, 'rb') as fp:
parser = PDFParser(fp) # 用文件对象来创建一个pdf文档分析器
doc: PDFDocument = PDFDocument(parser) # 创建pdf文档
rsrcmgr = PDFResourceManager() # 创建PDF,资源管理器,来共享资源
# 创建一个PDF设备对象
laparams = LAParams()
device = PDFPageAggregator(rsrcmgr, laparams=laparams)
# 创建一个PDF解释其对象
interpreter = PDFPageInterpreter(rsrcmgr, device)
# 循环遍历列表,每次处理一个page内容
# doc.get_pages() 获取page列表
r_page_num = "^[-—][\d]+[-—]$" # 页码匹配
doc_text = ''
for page in PDFPage.create_pages(doc):
interpreter.process_page(page)
layout = device.get_result()
"""解析页面内容,一个字母一个字母的解析"""
# bbox:
# x0:从页面左侧到框左边缘的距离。
# y0:从页面底部到框的下边缘的距离。
# x1:从页面左侧到方框右边缘的距离。
# y1:从页面底部到框的上边缘的距离
words_result = []
for textbox in layout:
if isinstance(textbox, LTText):
for line in textbox:
char_list = []
for char in line:
# If the char is a line-break or an empty space, the word is complete
if isinstance(char, LTAnno) or char.get_text() == ' ':
pass
elif isinstance(char, LTChar):
char_dict = {
"char": char.get_text(),
"location": {
"top": char.bbox[3],
"left": char.bbox[0],
"width": char.width,
"height": char.height,
}
}
char_list.append(char_dict)
# print("坐标 x:", char.bbox[0], "y:", char.bbox[3], " ||| ", char.get_text())
line_dict = {
"words": line.get_text().strip('\n').replace(' ', ''),
"location": {
"top": line.bbox[3],
"left": line.bbox[0],
"width": line.width,
"height": line.height, },
"chars": char_list,
}
words_result.append(line_dict)
# 计算页最小左边坐标
min_pos_x = None
for line_guise in words_result:
cur_pos_x = line_guise['location']['left']
if min_pos_x is None:
min_pos_x = int(cur_pos_x)
min_pos_x = int(cur_pos_x if cur_pos_x < min_pos_x else min_pos_x)
# 重新拼接文本
section = ''
back_pos_top = None
cur_page_is_new_section = False
line_num = -1
for line_guise in words_result:
line_num += 1
cur_pos_x = int(line_guise['location']['left'])
cur_pos_top = int(line_guise['location']['top'])
cur_line_word = line_guise['words']
# 判断是否页码 -1-
if (line_num == (len(words_result) - 1)) and re.match(r_page_num, cur_line_word):
continue
if back_pos_top is None:
section = cur_line_word
if cur_pos_x > min_pos_x + 1:
cur_page_is_new_section = True
elif cur_pos_top == back_pos_top:
section += ' ' + cur_line_word
else:
if cur_pos_x > min_pos_x + 1:
section += '\n ' + cur_line_word
else:
section += cur_line_word
back_pos_top = cur_pos_top
# print(section)
if cur_page_is_new_section:
doc_text += '\n ' + section
else:
doc_text += section
return doc_text
path='xxx.pdf'
context = convert2text(path)
with open('xxx.txt', 'w', encoding='utf-8') as f:
f.write(context)
网友评论