77 lines
2.9 KiB
Python
77 lines
2.9 KiB
Python
import PyPDF2
|
||
import re # 导入正则表达式库
|
||
|
||
from PyPDF2 import PdfReader
|
||
|
||
|
||
def clean_page_content(text, common_header):
|
||
# 首先删除抬头公共部分
|
||
if common_header: # 确保有公共抬头才进行替换
|
||
for header_line in common_header.split('\n'):
|
||
if header_line.strip(): # 只处理非空行
|
||
# 替换首次出现的完整行
|
||
text = re.sub(r'^' + re.escape(header_line.strip()) + r'\n?', '', text, count=1)
|
||
|
||
# 删除页码 eg:89/129 这个代码分三步走可以把89/129完全删除
|
||
text = re.sub(r'^\s*\d+\s*(?=\D)', '', text) # 删除开头的页码,仅当紧跟非数字字符时
|
||
text = re.sub(r'\s+\d+\s*$', '', text) # 删除结尾的页码
|
||
text = re.sub(r'\s*\/\s*\d+\s*', '', text) # 删除形如 /129 的页码
|
||
|
||
return text
|
||
def extract_common_header(pdf_path):
|
||
pdf_document = PdfReader(pdf_path)
|
||
headers = []
|
||
total_pages = len(pdf_document.pages)
|
||
|
||
# 确定要读取的页数和起始页
|
||
if total_pages == 2:
|
||
pages_to_read = 2
|
||
start_page = 0
|
||
else:
|
||
pages_to_read = 3
|
||
middle_page = total_pages // 2
|
||
start_page = max(0, middle_page - 1)
|
||
|
||
for i in range(start_page, min(start_page + pages_to_read, total_pages)):
|
||
page = pdf_document.pages[i]
|
||
text = page.extract_text() or ""
|
||
if text:
|
||
# 只取每页的前三行
|
||
first_lines = text.strip().split('\n')[:3]
|
||
headers.append(first_lines)
|
||
|
||
if len(headers) < 2:
|
||
return "" # 如果没有足够的页来比较,返回空字符串
|
||
|
||
# 寻找每一行中的公共部分
|
||
common_headers = []
|
||
for lines in zip(*headers):
|
||
# 在每一行中寻找公共单词
|
||
common_line = set(lines[0].split()).intersection(*[set(line.split()) for line in lines[1:]])
|
||
if common_line:
|
||
common_headers.append(' '.join(common_line))
|
||
|
||
return '\n'.join(common_headers)
|
||
|
||
def extract_text_by_page(file_path):
|
||
common_header = extract_common_header(file_path)
|
||
result = ""
|
||
with open(file_path, 'rb') as file:
|
||
reader = PyPDF2.PdfReader(file)
|
||
num_pages = len(reader.pages)
|
||
# print(f"Total pages: {num_pages}")
|
||
for page_num in range(num_pages):
|
||
page = reader.pages[page_num]
|
||
text = page.extract_text()
|
||
if text:
|
||
cleaned_text = clean_page_content(text,common_header)
|
||
print(cleaned_text)
|
||
result += cleaned_text
|
||
# print(f"Page {page_num + 1} Content:\n{cleaned_text}")
|
||
else:
|
||
print(f"Page {page_num + 1} is empty or text could not be extracted.")
|
||
return result
|
||
if __name__ == '__main__':
|
||
file_path = "C:\\Users\\Administrator\\Desktop\\货物标\\output4\\招招招标文件(一中多媒体报告厅教学设备)_tobidders_notice_part2.pdf"
|
||
res=extract_text_by_page(file_path)
|
||
# print(res) |