2024-08-29 16:37:09 +08:00
|
|
|
|
from PyPDF2 import PdfReader, PdfWriter
|
|
|
|
|
import re # 导入正则表达式库
|
|
|
|
|
import os # 用于文件和文件夹操作
|
|
|
|
|
|
|
|
|
|
def clean_page_numbers(text):
|
|
|
|
|
# 使用正则表达式删除页码
|
|
|
|
|
# 假设页码在文本的最开始,紧跟着文字且无空格分隔
|
|
|
|
|
cleaned_text = re.sub(r'^\s*\d+\s*(?=\D)', '', text) # 删除开头的页码,仅当紧跟非数字字符时
|
|
|
|
|
# 删除结尾的页码
|
|
|
|
|
cleaned_text = re.sub(r'\s+\d+\s*$', '', cleaned_text)
|
|
|
|
|
# 删除形如 /129 的页码
|
2024-09-03 09:36:18 +08:00
|
|
|
|
cleaned_text = re.sub(r'\s*\/\s*\d+\\s*', '', cleaned_text)
|
2024-08-29 16:37:09 +08:00
|
|
|
|
return cleaned_text
|
2024-09-03 09:36:18 +08:00
|
|
|
|
|
2024-08-29 16:37:09 +08:00
|
|
|
|
def extract_pages(pdf_path, output_folder, chapter_pattern, begin_page, end_phrase_pattern, output_suffix):
|
|
|
|
|
# 打开PDF文件
|
|
|
|
|
pdf_document = PdfReader(pdf_path)
|
|
|
|
|
start_page = None
|
|
|
|
|
end_page = None
|
|
|
|
|
|
|
|
|
|
qualification_pattern = re.compile(r'资格审查|资质条件|能力')
|
|
|
|
|
# 遍历文档的每一页,查找开始和结束短语的位置
|
|
|
|
|
for i in range(len(pdf_document.pages)):
|
|
|
|
|
page = pdf_document.pages[i]
|
|
|
|
|
text = page.extract_text()
|
|
|
|
|
if text:
|
|
|
|
|
cleaned_text = clean_page_numbers(text)
|
|
|
|
|
if re.search(chapter_pattern, cleaned_text) and i > begin_page:
|
|
|
|
|
if output_suffix == "invalid" and start_page: #仅当提取Invalid的时候,判断初始页码是第一个匹配到的页码,因为招标编号可能存在多个,后面的覆盖前面
|
|
|
|
|
continue
|
|
|
|
|
if output_suffix == "qualification" and not re.search(qualification_pattern, cleaned_text):
|
|
|
|
|
# 如果是资格审查条件,但当前页不包含相关词汇,则不执行任何操作
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
start_page = i
|
|
|
|
|
if start_page is not None and re.search(end_phrase_pattern, cleaned_text) and i > (start_page+1):
|
|
|
|
|
is_invalid_condition = output_suffix == "invalid" and i > 30
|
|
|
|
|
if is_invalid_condition or output_suffix != "invalid":
|
|
|
|
|
end_page = i
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
# 确保找到了起始和结束页面
|
|
|
|
|
if start_page is None or end_page is None:
|
|
|
|
|
print(f"未找到起始或结束页在文件 {pdf_path} 中!")
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
# 创建一个新的PDF文档保存截取的页面
|
|
|
|
|
base_file_name = os.path.splitext(os.path.basename(pdf_path))[0] # Get the base name without extension
|
|
|
|
|
output_pdf_path = os.path.join(output_folder, f"{base_file_name}_{output_suffix}.pdf")
|
|
|
|
|
output_doc = PdfWriter()
|
|
|
|
|
|
|
|
|
|
# 添加需要的页面,从 start_page 开始,包括 end_page
|
|
|
|
|
for page_num in range(start_page, end_page + 1):
|
|
|
|
|
output_doc.add_page(pdf_document.pages[page_num])
|
|
|
|
|
# 保存新的PDF文件
|
|
|
|
|
with open(output_pdf_path, 'wb') as f:
|
|
|
|
|
output_doc.write(f)
|
|
|
|
|
|
|
|
|
|
print(f"已截取并保存页面从 {start_page} 到 {end_page} 为 {output_pdf_path}")
|
|
|
|
|
|
|
|
|
|
return output_pdf_path
|
|
|
|
|
|
|
|
|
|
def process_input(input_path, output_folder, chapter_pattern, begin_page, end_phrases, output_suffix,selection):
|
|
|
|
|
# 确保输出文件夹存在
|
|
|
|
|
if not os.path.exists(output_folder):
|
|
|
|
|
os.makedirs(output_folder)
|
|
|
|
|
if(selection==3 or selection==4 or selection==5):
|
|
|
|
|
end_phrase_pattern = re.compile('|'.join([phrase for phrase in end_phrases]), re.MULTILINE)
|
|
|
|
|
else:
|
|
|
|
|
end_phrase_pattern = re.compile('|'.join([re.escape(phrase) for phrase in end_phrases]))
|
|
|
|
|
|
|
|
|
|
if os.path.isdir(input_path):
|
|
|
|
|
generated_files = []
|
|
|
|
|
# 遍历文件夹内的所有PDF文件
|
|
|
|
|
for file in os.listdir(input_path):
|
|
|
|
|
if file.endswith(".pdf"):
|
|
|
|
|
pdf_path = os.path.join(input_path, file)
|
|
|
|
|
output_pdf_path = extract_pages(pdf_path, output_folder, chapter_pattern, begin_page, end_phrase_pattern, output_suffix)
|
|
|
|
|
if output_pdf_path and os.path.isfile(output_pdf_path):
|
|
|
|
|
generated_files.append(output_pdf_path)
|
|
|
|
|
return generated_files
|
|
|
|
|
elif os.path.isfile(input_path) and input_path.endswith(".pdf"):
|
|
|
|
|
# 处理单个PDF文件
|
|
|
|
|
output_pdf_path = extract_pages(input_path, output_folder, chapter_pattern, begin_page, end_phrase_pattern, output_suffix)
|
|
|
|
|
if output_pdf_path and os.path.isfile(output_pdf_path):
|
|
|
|
|
return [output_pdf_path] # 以列表形式返回,以保持一致性
|
|
|
|
|
else:
|
|
|
|
|
print("提供的路径既不是文件夹也不是PDF文件。")
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def truncate_pdf_main(input_path, output_folder, selection):
|
|
|
|
|
if selection == 1:
|
|
|
|
|
# Configure patterns and phrases for "投标人须知前附表"
|
|
|
|
|
pattern = re.compile(r'第[一二三四五六七八九十]+章\s*投标人须知')
|
|
|
|
|
begin_page = 3
|
|
|
|
|
end_phrases = ["投标人须知正文"]
|
|
|
|
|
output_suffix = "tobidders_notice_table"
|
|
|
|
|
elif selection == 2:
|
|
|
|
|
# Configure patterns and phrases for "评标办法"
|
|
|
|
|
pattern = re.compile(r'第[一二三四五六七八九十]+章\s*评标办法')
|
|
|
|
|
begin_page = 10
|
|
|
|
|
end_phrases = ["评标办法正文", "评标办法"]
|
|
|
|
|
output_suffix = "evaluation_method"
|
|
|
|
|
elif selection == 3:
|
|
|
|
|
# Configure patterns and phrases for "投标人须知正文"
|
|
|
|
|
pattern = re.compile(r'投标人须知正文')
|
|
|
|
|
begin_page = 5
|
|
|
|
|
end_phrases = [
|
|
|
|
|
r'^第[一二三四五六七八九十]+章\s*评标办法',r'^评标办法前附表',r'^附录:', r'^附录一:',r'^附件:', r'^附件一:',r'^附表:', r'^附表一:',r'^附录:', r'^附录一:',r'^附件:', r'^附件一:',r'^附表:', r'^附表一:',
|
|
|
|
|
]
|
|
|
|
|
output_suffix = "tobidders_notice"
|
|
|
|
|
elif selection==4:
|
2024-09-03 09:36:18 +08:00
|
|
|
|
# 配置用于 "资格审查条件" 的正则表达式模式和短语
|
|
|
|
|
appendix_pattern = r'^附录(?:一)?[::]|^附件(?:一)?[::]|^附表(?:一)?[::]'
|
|
|
|
|
pattern = re.compile(appendix_pattern)
|
|
|
|
|
begin_page = 5
|
|
|
|
|
end_phrases = [r'评标办法正文', r'评标办法', appendix_pattern]
|
|
|
|
|
output_suffix = "qualification"
|
|
|
|
|
elif selection == 5:
|
|
|
|
|
# 配置用于 "无效标" 的正则表达式模式和短语
|
2024-08-29 16:37:09 +08:00
|
|
|
|
pattern = re.compile(r'第[一二三四五六七八九十]+章\s*招标公告|第一卷|招标编号:|招标编号:')
|
|
|
|
|
begin_page = 0
|
|
|
|
|
end_phrases = [
|
|
|
|
|
r'第[一二三四五六七八九十]+章\s*合同',
|
|
|
|
|
r':清标报告',# 添加了新的匹配项
|
|
|
|
|
r':清标报告'
|
|
|
|
|
]
|
2024-09-03 09:36:18 +08:00
|
|
|
|
output_suffix = "invalid"
|
2024-08-29 16:37:09 +08:00
|
|
|
|
else:
|
|
|
|
|
print("无效的选择")
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
# Process the selected input
|
|
|
|
|
return process_input(input_path, output_folder, pattern, begin_page, end_phrases, output_suffix,selection)
|
|
|
|
|
|
|
|
|
|
def truncate_pdf_multiple(input_path, output_folder):
|
|
|
|
|
truncate_files = []
|
2024-09-03 09:36:18 +08:00
|
|
|
|
for selection in range(1, 5):
|
2024-08-29 16:37:09 +08:00
|
|
|
|
files = truncate_pdf_main(input_path, output_folder, selection)
|
|
|
|
|
truncate_files.extend(files)
|
|
|
|
|
return truncate_files
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-09-03 09:36:18 +08:00
|
|
|
|
input_path = "C:\\Users\\Administrator\\Desktop\\招标文件\\招标03.pdf"
|
2024-08-29 16:37:09 +08:00
|
|
|
|
output_folder = "C:\\Users\\Administrator\\Desktop\\招标文件\\test"
|
2024-09-03 09:36:18 +08:00
|
|
|
|
# truncate_pdf_multiple(input_path,output_folder)
|
|
|
|
|
selection = 5 # 例如:1 - 投标人须知前附表, 2 - 评标办法, 3 - 投标人须知正文 4-资格审查条件
|
|
|
|
|
generated_files = truncate_pdf_main(input_path, output_folder, selection)
|
2024-08-29 16:37:09 +08:00
|
|
|
|
# print("生成的文件:", generated_files)
|