12.18 截取pdf
This commit is contained in:
parent
cb153889a4
commit
6fb4f49b13
@ -118,7 +118,7 @@ def merge_selected_pdfs(output_folder, truncate_files, output_path, base_file_na
|
||||
required_suffixes = [
|
||||
f'{base_file_name}_before.pdf',
|
||||
f'{base_file_name}_notice.pdf',
|
||||
f'{base_file_name}_tobidders_notice_table.pdf'
|
||||
f'{base_file_name}_tobidders_notice_table.pdf' #tobidders_notice tobidders_notice_table
|
||||
]
|
||||
optional_suffixes = []
|
||||
elif mode == 'goods':
|
||||
|
@ -34,20 +34,18 @@ def truncate_pdf_multiple(pdf_path, output_folder, logger,mode='goods',selection
|
||||
return ["", ""] if selection == 4 else [""]
|
||||
# 设置模式相关的参数和函数
|
||||
if mode == 'goods':
|
||||
logger.info("call 货物标截取pdf")
|
||||
truncate_function = truncate_pdf_main_goods
|
||||
merge_mode = 'goods'
|
||||
|
||||
# 根据 'goods' 模式定义异常处理的逻辑
|
||||
else: # mode == 'engineering'
|
||||
logger.info("call 工程标标截取pdf")
|
||||
truncate_function = truncate_pdf_main_engineering
|
||||
merge_mode = 'engineering'
|
||||
|
||||
mode_flag = 1
|
||||
num_selections = len(selections)
|
||||
if num_selections < 5:
|
||||
mode_flag = 2
|
||||
|
||||
res = check_pdf_pages(pdf_path, mode_flag, logger)
|
||||
res = check_pdf_pages(pdf_path, logger)
|
||||
if res is not None:
|
||||
return res # 返回包含空字符串的列表
|
||||
|
||||
@ -63,7 +61,8 @@ def truncate_pdf_multiple(pdf_path, output_folder, logger,mode='goods',selection
|
||||
pdf_path,
|
||||
output_folder,
|
||||
selection,
|
||||
logger
|
||||
logger,
|
||||
"default"
|
||||
# 如果 'goods' 模式需要额外参数,如 output_suffix,可以在这里处理
|
||||
)
|
||||
for selection in selections
|
||||
@ -115,8 +114,9 @@ if __name__ == "__main__":
|
||||
# pdf_path = r"C:\Users\Administrator\Desktop\招标文件\招标02.pdf"
|
||||
# input_path=r"C:\Users\Administrator\Desktop\招标文件\招标test文件夹\zbtest8.pdf"
|
||||
output_folder = r"C:\Users\Administrator\Desktop\fsdownload\91399aa4-1ee8-447d-a05b-03cd8d15ced5\tmp"
|
||||
# selections = [1, 4] # 仅处理 selection 4、1
|
||||
selections=[5]
|
||||
selections = [1, 4] # 仅处理 selection 4、1
|
||||
# selections=[5]
|
||||
#engineering
|
||||
files=truncate_pdf_multiple(pdf_path,output_folder,logger,'engineering',selections)
|
||||
print(files)
|
||||
# selection = 1 # 例如:1 - 招标公告, 2 - 评标办法, 3 -资格审查条件 4-投标人须知前附表+正文 5-无效标
|
||||
|
@ -3,9 +3,11 @@ import concurrent.futures
|
||||
import os
|
||||
|
||||
import regex
|
||||
from PyPDF2 import PdfReader
|
||||
from PyPDF2 import PdfReader, PdfWriter
|
||||
|
||||
from flask_app.general.clean_pdf import extract_common_header, clean_page_content
|
||||
from flask_app.general.format_change import docx2pdf
|
||||
|
||||
|
||||
def get_start_and_common_header(input_path,end_page):
|
||||
common_header = extract_common_header(input_path)
|
||||
@ -32,26 +34,78 @@ def get_start_and_common_header(input_path,end_page):
|
||||
return common_header, last_begin_index
|
||||
return common_header, last_begin_index
|
||||
|
||||
def check_pdf_pages(pdf_path, mode,logger):
|
||||
def check_pdf_pages(pdf_path, logger):
|
||||
try:
|
||||
reader = PdfReader(pdf_path)
|
||||
num_pages = len(reader.pages)
|
||||
logger.info(f"PDF '{pdf_path}' 的总页数为: {num_pages}")
|
||||
if num_pages <= 50:
|
||||
logger.info("PDF页数小于或等于50页,跳过切分逻辑。")
|
||||
if mode==1:
|
||||
return ['', '', '', '', '', '', '']
|
||||
else:
|
||||
return ['','','','']
|
||||
return ['', '', '', '', '', '', '']
|
||||
# 若页数大于50页,返回None表示继续处理
|
||||
return None
|
||||
except Exception as e:
|
||||
logger.error(f"无法读取 PDF 页数: {e}")
|
||||
# 返回空列表意味着无法执行后续处理逻辑
|
||||
if mode == 1:
|
||||
return ['', '', '', '', '', '', '']
|
||||
else:
|
||||
return ['', '', '', '']
|
||||
return ['', '', '', '', '', '', '']
|
||||
|
||||
|
||||
def save_extracted_pages(pdf_path,output_folder,start_page, end_page, output_suffix,common_header):
|
||||
try:
|
||||
# 检查 start_page 和 end_page 是否为 None
|
||||
if start_page is None or end_page is None:
|
||||
print("Error: start_page 或 end_page 为 None")
|
||||
return ""
|
||||
pdf_document = PdfReader(pdf_path)
|
||||
total_pages = len(pdf_document.pages)
|
||||
base_file_name = os.path.splitext(os.path.basename(pdf_path))[0]
|
||||
output_pdf_path = os.path.join(output_folder, f"{base_file_name}_{output_suffix}.pdf")
|
||||
|
||||
if start_page < 0 or end_page >= total_pages or start_page > end_page:
|
||||
print(f"无效的页面范围: {start_page} 到 {end_page}")
|
||||
return ""
|
||||
|
||||
if output_suffix == 'notice' and start_page > 0:
|
||||
before_pdf_path = os.path.join(output_folder, f"{base_file_name}_before.pdf")
|
||||
before_doc = PdfWriter()
|
||||
toc_page = -1
|
||||
# 查找目录页
|
||||
for page_num in range(min(start_page, total_pages)):
|
||||
page_text = pdf_document.pages[page_num].extract_text()
|
||||
cleaned_text = clean_page_content(page_text, common_header)
|
||||
if regex.search(r'目\s*录', cleaned_text, regex.MULTILINE):
|
||||
toc_page = page_num
|
||||
break
|
||||
|
||||
# 确定截取的页数
|
||||
pages_to_extract = toc_page + 1 if toc_page != -1 else start_page
|
||||
# 提取页面
|
||||
for page_num in range(pages_to_extract):
|
||||
before_doc.add_page(pdf_document.pages[page_num])
|
||||
print(before_pdf_path)
|
||||
with open(before_pdf_path, 'wb') as f_before:
|
||||
before_doc.write(f_before)
|
||||
|
||||
output_doc = PdfWriter()
|
||||
for page_num in range(start_page, end_page + 1):
|
||||
output_doc.add_page(pdf_document.pages[page_num])
|
||||
with open(output_pdf_path, 'wb') as f:
|
||||
output_doc.write(f)
|
||||
print(f"{output_suffix} 已截取并保存页面从 {start_page} 到 {end_page} 为 {output_pdf_path}")
|
||||
return output_pdf_path
|
||||
except Exception as e:
|
||||
print(f"Error in save_extracted_pages: {e}")
|
||||
return "" # 返回空字符串
|
||||
|
||||
def is_pdf_or_doc(filename):
|
||||
# 判断文件是否为PDF或Word文档
|
||||
return filename.lower().endswith(('.pdf', '.doc', '.docx'))
|
||||
|
||||
|
||||
def convert_to_pdf(file_path):
|
||||
# 假设 docx2pdf 函数已经被定义,这里仅根据文件扩展名来决定是否需要转换
|
||||
if file_path.lower().endswith(('.doc', '.docx')):
|
||||
return docx2pdf(file_path)
|
||||
return file_path
|
||||
|
||||
|
||||
|
@ -5,66 +5,9 @@ import time
|
||||
from PyPDF2 import PdfReader, PdfWriter
|
||||
from flask_app.general.clean_pdf import clean_page_content
|
||||
|
||||
from flask_app.general.截取pdf通用函数 import get_start_and_common_header
|
||||
from flask_app.general.截取pdf通用函数 import get_start_and_common_header,save_extracted_pages
|
||||
from flask_app.general.通用功能函数 import get_global_logger
|
||||
|
||||
|
||||
def save_pages_to_new_pdf(pdf_path, output_folder, output_suffix, start_page, end_page, common_header):
|
||||
try:
|
||||
# 获取文件基本名称
|
||||
base_file_name = os.path.splitext(os.path.basename(pdf_path))[0]
|
||||
|
||||
# 构建主要输出文件路径
|
||||
output_pdf_path = os.path.join(output_folder, f"{base_file_name}_{output_suffix}.pdf")
|
||||
# 读取PDF文件
|
||||
pdf_document = PdfReader(pdf_path)
|
||||
total_pages = len(pdf_document.pages)
|
||||
|
||||
# 检查起始和结束页码是否有效
|
||||
if start_page < 0 or end_page >= total_pages or start_page > end_page:
|
||||
print(f"无效的页面范围: {start_page} 到 {end_page}")
|
||||
return ""
|
||||
|
||||
# 如果 output_suffix 是 'notice',保存 start_page 之前的页面(若遇到'目录',则截取到'目录')
|
||||
if output_suffix == 'notice' and start_page > 0:
|
||||
before_pdf_path = os.path.join(output_folder, f"{base_file_name}_before.pdf")
|
||||
before_doc = PdfWriter()
|
||||
toc_page = -1
|
||||
# 查找目录页
|
||||
for page_num in range(min(start_page, total_pages)):
|
||||
page_text = pdf_document.pages[page_num].extract_text()
|
||||
cleaned_text = clean_page_content(page_text, common_header)
|
||||
if regex.search(r'目\s*录', cleaned_text, regex.MULTILINE):
|
||||
toc_page = page_num
|
||||
break
|
||||
|
||||
# 确定截取的页数
|
||||
pages_to_extract = toc_page + 1 if toc_page != -1 else start_page
|
||||
# 提取页面
|
||||
for page_num in range(pages_to_extract):
|
||||
before_doc.add_page(pdf_document.pages[page_num])
|
||||
print(before_pdf_path)
|
||||
with open(before_pdf_path, 'wb') as f_before:
|
||||
before_doc.write(f_before)
|
||||
|
||||
# print(f"已保存页面从 0 到 {pages_to_extract} 为 {before_pdf_path}")
|
||||
|
||||
# 提取指定范围的页面
|
||||
output_doc = PdfWriter()
|
||||
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:
|
||||
output_doc.write(f_output)
|
||||
print(f"{output_suffix} 已截取并保存页面从 {start_page + 1} 到 {end_page + 1} 为 {output_pdf_path}")
|
||||
return output_pdf_path
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error in save_pages_to_new_pdf: {e}")
|
||||
return "" # 返回空字符串
|
||||
|
||||
|
||||
def extract_pages_tobidders_notice(pdf_path, output_folder, begin_pattern, begin_page, common_header,
|
||||
is_secondary_match):
|
||||
pdf_document = PdfReader(pdf_path)
|
||||
@ -75,6 +18,8 @@ def extract_pages_tobidders_notice(pdf_path, output_folder, begin_pattern, begin
|
||||
mid_page = None
|
||||
end_page = None
|
||||
chapter_type = None # 用于存储“章”或“部分”
|
||||
combined_mid_pattern= None
|
||||
end_pattern = None
|
||||
|
||||
for i, page in enumerate(pdf_document.pages):
|
||||
text = page.extract_text() or ""
|
||||
@ -87,7 +32,7 @@ def extract_pages_tobidders_notice(pdf_path, output_folder, begin_pattern, begin
|
||||
match = regex.search(begin_pattern, cleaned_text)
|
||||
if match and i > begin_page:
|
||||
start_page = i
|
||||
matched_text = match.group(0) # 获取整个匹配的文本
|
||||
matched_text: str = match.group(0) # 获取整个匹配的文本
|
||||
if '章' in matched_text:
|
||||
chapter_type = '章'
|
||||
elif '部分' in matched_text:
|
||||
@ -175,14 +120,12 @@ def extract_pages_tobidders_notice(pdf_path, output_folder, begin_pattern, begin
|
||||
print(f"first: tobidders_notice 未找到起始或结束页在文件 {pdf_path} 中!尝试备用提取策略。")
|
||||
return "", ""
|
||||
|
||||
path1 = save_pages_to_new_pdf(pdf_path, output_folder, "tobidders_notice_table", start_page, mid_page,
|
||||
common_header)
|
||||
path2 = save_pages_to_new_pdf(pdf_path, output_folder, "tobidders_notice", mid_page, end_page, common_header)
|
||||
path1 = save_extracted_pages(pdf_path, output_folder, start_page, mid_page, "tobidders_notice_table", common_header)
|
||||
path2 = save_extracted_pages(pdf_path, output_folder, mid_page, end_page, "tobidders_notice", common_header)
|
||||
return path1, path2
|
||||
|
||||
|
||||
def extract_pages(pdf_path, output_folder, begin_pattern, begin_page, end_pattern, output_suffix, common_header,
|
||||
is_secondary_match=False):
|
||||
def extract_pages(pdf_path, output_folder, begin_pattern, begin_page, end_pattern, output_suffix, common_header):
|
||||
# 打开PDF文件
|
||||
pdf_document = PdfReader(pdf_path)
|
||||
start_page = None
|
||||
@ -194,7 +137,6 @@ def extract_pages(pdf_path, output_folder, begin_pattern, begin_page, end_patter
|
||||
text = page.extract_text()
|
||||
if text:
|
||||
cleaned_text = clean_page_content(text, common_header)
|
||||
# if is_secondary_match and regex.search(exclusion_pattern, cleaned_text): # 跳过投标人须知正文中的"投标文件的组成"
|
||||
if regex.search(exclusion_pattern, cleaned_text):
|
||||
continue
|
||||
if regex.search(begin_pattern, cleaned_text) and i >= begin_page:
|
||||
@ -215,7 +157,7 @@ def extract_pages(pdf_path, output_folder, begin_pattern, begin_page, end_patter
|
||||
print(f"{output_suffix} first: 未找到起始或结束页在文件 {pdf_path} 中!")
|
||||
return [""]
|
||||
else:
|
||||
return [save_pages_to_new_pdf(pdf_path, output_folder, output_suffix, start_page, end_page, common_header)]
|
||||
return [save_extracted_pages(pdf_path, output_folder, start_page, end_page, output_suffix, common_header)]
|
||||
|
||||
|
||||
def extract_pages_twice(pdf_path, output_folder, output_suffix, common_header, last_begin_index):
|
||||
@ -277,7 +219,7 @@ def extract_pages_twice(pdf_path, output_folder, output_suffix, common_header, l
|
||||
print(f"{output_suffix} twice: 未找到起始或结束页在文件 {pdf_path} 中!")
|
||||
return []
|
||||
else:
|
||||
return [save_pages_to_new_pdf(pdf_path, output_folder, output_suffix, start_page, end_page, common_header)]
|
||||
return [save_extracted_pages(pdf_path, output_folder, start_page, end_page, output_suffix, common_header)]
|
||||
elif output_suffix == "invalid":
|
||||
pdf_document = PdfReader(pdf_path)
|
||||
total_pages = len(pdf_document.pages)
|
||||
@ -285,7 +227,7 @@ def extract_pages_twice(pdf_path, output_folder, output_suffix, common_header, l
|
||||
total = int(total_pages * 2 / 3)
|
||||
start_page = last_begin_index
|
||||
end_page = min(90, total)
|
||||
return [save_pages_to_new_pdf(pdf_path, output_folder, output_suffix, start_page, end_page, common_header)]
|
||||
return [save_extracted_pages(pdf_path, output_folder, start_page, end_page, output_suffix, common_header)]
|
||||
else:
|
||||
print(f"{output_suffix} twice: 未定义的输出后缀。")
|
||||
return []
|
||||
@ -294,174 +236,224 @@ def extract_pages_twice(pdf_path, output_folder, output_suffix, common_header, l
|
||||
return []
|
||||
|
||||
|
||||
def truncate_pdf_main_engineering(input_path, output_folder, selection,logger):
|
||||
if os.path.isdir(input_path):
|
||||
generated_files = []
|
||||
for file_name in os.listdir(input_path):
|
||||
if file_name.endswith('.pdf'):
|
||||
file_path = os.path.join(input_path, file_name)
|
||||
files = truncate_pdf_main_engineering(file_path, output_folder, selection)
|
||||
if files:
|
||||
generated_files.extend(files)
|
||||
return generated_files
|
||||
elif os.path.isfile(input_path) and input_path.endswith('.pdf'):
|
||||
# base_file_name = os.path.splitext(os.path.basename(input_path))[0]
|
||||
common_header, last_begin_index = get_start_and_common_header(input_path,20)
|
||||
# print(last_begin_index)
|
||||
if selection == 1:
|
||||
# Selection 1: 招标公告
|
||||
pattern_pairs = [
|
||||
(
|
||||
regex.compile(
|
||||
r'^第[一二三四五六七八九十百千]+(?:章|部分).*?(?:公告|邀请书|邀请函|邀请).*|^第一卷|^投标邀请书|^投标邀请函|^投标邀请'),
|
||||
regex.compile(r'^第[一二三四五六七八九十百千]+(?:章|部分)\s*[\u4e00-\u9fff]+', regex.MULTILINE)
|
||||
),
|
||||
(
|
||||
regex.compile(
|
||||
r'.*(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)(?:招标公告|磋商公告|谈判公告|邀请书|邀请函|投标邀请|磋商邀请|谈判邀请)[\))]?\s*$',
|
||||
regex.MULTILINE),
|
||||
regex.compile(
|
||||
r'.*(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)(?:投标人?|磋商|供应商|谈判供应商|磋商供应商)须知(?:前附表)?\s*$',
|
||||
regex.MULTILINE
|
||||
)
|
||||
)
|
||||
]
|
||||
output_suffix = "notice"
|
||||
elif selection == 2:
|
||||
# Selection 2: 评标办法
|
||||
pattern_pairs = [
|
||||
(
|
||||
regex.compile(r'^第[一二三四五六七八九十]+(?:章|部分)\s*(?=.*(?:磋商|谈判|评标|评定|评审))(?=.*(?:办法|方法))',regex.MULTILINE),
|
||||
# Alternative begin pattern
|
||||
regex.compile(r'^第[一二三四五六七八九1-9]+(?:章|部分)\s*[\u4e00-\u9fff]+|[::]清标报告\s*$|(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)评标(方法|办法)正文\s*$|(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)(?:评标|定标)详细程序\s*$',regex.MULTILINE)
|
||||
# Alternative end pattern
|
||||
),
|
||||
(
|
||||
regex.compile(
|
||||
r'(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)' # 确保前面不是“见”
|
||||
r'第[一二三四五六七八九十1-9]+(?:章|部分)\s*' # 匹配“第X章”或“第X部分”
|
||||
r'[\u4e00-\u9fff、()()]*?' # 匹配允许的字符(中文、顿号、括号)
|
||||
r'(?=.*(?:磋商|谈判|评标|评定|评审))' # 确保包含“磋商”、“谈判”、“评标”、“评定”或“评审”
|
||||
r'(?=.*(?:办法|方法))' # 确保包含“办法”或“方法”
|
||||
r'[\u4e00-\u9fff、()()]*\s*$' # 继续匹配允许的字符直到行尾
|
||||
r'|\s*评标(办法|方法)前附表\s*$', # 或匹配“评标办法前附表”或“评标方法前附表”
|
||||
regex.MULTILINE
|
||||
),
|
||||
regex.compile(r'第[一二三四五六七八九十1-9]+(?:章|部分)\s*[\u4e00-\u9fff、()()]+\s*$', regex.MULTILINE)
|
||||
)
|
||||
]
|
||||
output_suffix = "evaluation_method"
|
||||
# TODO:exclusion
|
||||
elif selection == 3:
|
||||
# Selection 3: 资格审查条件
|
||||
pattern_pairs = [
|
||||
# (
|
||||
# regex.compile(r'^(?:附录(?:[一1])?[::]|附件(?:[一1])?[::]|附表(?:[一1])?[::]).*(?:资质|能力|信誉).*$|^第[一二三四五六七八九十百千]+(?:章|部分).*?(资格).*',
|
||||
# regex.MULTILINE),
|
||||
# regex.compile(
|
||||
# r'^(?:附录[一二三四五六七八九1-9]*[::]|附件[一二三四五六七八九1-9]*[::]|附表[一二三四五六七八九1-9]*[::])(?!.*(?:资质|能力|信誉)).*$'
|
||||
# r'^第[一二三四五六七八九1-9]+(?:章|部分)\s*[\u4e00-\u9fff]+', regex.MULTILINE)
|
||||
# ),
|
||||
(
|
||||
regex.compile(
|
||||
r'^(?:附录(?:[一1])?[::]|附件(?:[一1])?[::]|附表(?:[一1])?[::]).*(?:资质|能力|信誉).*$|(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)第[一二三四五六七八九1-9]+(?:章|部分)\s*[\u4e00-\u9fff、()()]*资格[\u4e00-\u9fff、()()]*\s*$',
|
||||
regex.MULTILINE),
|
||||
regex.compile(
|
||||
r'^(?:附录[一二三四五六七八九1-9]*[::]|附件[一二三四五六七八九1-9]*[::]|附表[一二三四五六七八九1-9]*[::])(?!.*(?:资质|能力|信誉)).*|'
|
||||
r'(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)第[一二三四五六七八九1-9]+(?:章|部分)\s*[\u4e00-\u9fff、()()]+\s*$', regex.MULTILINE)
|
||||
)
|
||||
]
|
||||
output_suffix = "qualification"
|
||||
elif selection == 4:
|
||||
# Selection 4: 投标人须知
|
||||
pattern_pairs = [
|
||||
(
|
||||
regex.compile(
|
||||
r'(?:第[一二三四五六七八九十百千]+(?:章|部分)\s*(?:投标人?|磋商|供应商|谈判供应商|磋商供应商)须知|(?:投标人?|磋商|供应商|谈判供应商|磋商供应商)须知前附表)\s*$',
|
||||
regex.MULTILINE),
|
||||
regex.compile(
|
||||
r'第[一二三四五六七八九十]+(?:章|部分)\s*[\u4e00-\u9fff]+|^评标(方法|办法)前附表|^附录(?:一)?[::]|^附件(?:一)?[::]|^附表(?:一)?[::]',
|
||||
regex.MULTILINE)
|
||||
),
|
||||
(
|
||||
regex.compile(
|
||||
r'.*(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)(?:投标人?|磋商|供应商|谈判供应商|磋商供应商)须知\s*$|'
|
||||
r'(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)(?:投标人?|磋商|供应商|谈判供应商|磋商供应商)须知前附表\s*$',
|
||||
regex.MULTILINE
|
||||
),
|
||||
regex.compile(
|
||||
r'第[一二三四五六七八九十]+(?:章|部分)\s*[\u4e00-\u9fff]+|^评标(方法|办法)前附表|^附录(?:一)?[::]|^附件(?:一)?[::]|^附表(?:一)?[::]',
|
||||
regex.MULTILINE)
|
||||
)
|
||||
]
|
||||
output_suffix = "tobidders_notice"
|
||||
elif selection == 5:
|
||||
# Selection 5: 无效标
|
||||
pattern_pairs = [
|
||||
(
|
||||
regex.compile(
|
||||
r'^第[一二三四五六七八九十百千]+(?:章|部分).*?(?:公告|邀请书|邀请函|邀请).*|^第一卷|^投标邀请书|^投标邀请函|^投标邀请'),
|
||||
regex.compile(r'第[一二三四五六七八九十]+章\s*合同|[::]清标报告|^第二卷', regex.MULTILINE)
|
||||
),
|
||||
(
|
||||
regex.compile(r'.*(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)(?:招标公告|磋商公告|谈判公告|邀请书|邀请函|投标邀请|磋商邀请|谈判邀请)[\))]?\s*$', regex.MULTILINE),
|
||||
regex.compile(r'第[一二三四五六七八九十]+章\s*合同|[::]清标报告|^第二卷', regex.MULTILINE)
|
||||
)
|
||||
]
|
||||
output_suffix = "invalid"
|
||||
|
||||
else:
|
||||
print("无效的选择:请选择1-5")
|
||||
return [""]
|
||||
|
||||
for idx, (begin_pattern, end_pattern) in enumerate(pattern_pairs, start=1):
|
||||
is_secondary_match = (idx == 2)
|
||||
begin_page = last_begin_index if last_begin_index != 0 else {
|
||||
1: 0, # 公告
|
||||
2: 10, # 评标
|
||||
3: 5, # 资格
|
||||
4: 3, # 前附表
|
||||
5: 0 # 无效标
|
||||
}.get(selection, 0)
|
||||
if selection == 4: # 投标人须知
|
||||
output_paths = list(
|
||||
extract_pages_tobidders_notice(input_path, output_folder, begin_pattern, begin_page, common_header,
|
||||
is_secondary_match))
|
||||
if output_paths and any(os.path.isfile(f) for f in output_paths):
|
||||
return output_paths
|
||||
def truncate_pdf_main_engineering(input_path, output_folder, selection,logger,output_suffix="default"):
|
||||
try:
|
||||
# 内嵌的处理单个文件的函数
|
||||
def process_single_file(file_path, output_folder, selection):
|
||||
try:
|
||||
# 获取起始和通用页眉
|
||||
common_header, last_begin_index = get_start_and_common_header(file_path, 20)
|
||||
if selection == 1:
|
||||
# Selection 1: 招标公告
|
||||
pattern_pairs = [
|
||||
(
|
||||
regex.compile(
|
||||
r'^第[一二三四五六七八九十百千]+(?:章|部分).*?(?:公告|邀请书|邀请函|邀请).*|^第一卷|^投标邀请书|^投标邀请函|^投标邀请'
|
||||
),
|
||||
regex.compile(
|
||||
r'^第[一二三四五六七八九十百千]+(?:章|部分)\s*[\u4e00-\u9fff]+', regex.MULTILINE
|
||||
)
|
||||
),
|
||||
(
|
||||
regex.compile(
|
||||
r'.*(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)(?:招标公告|磋商公告|谈判公告|邀请书|邀请函|投标邀请|磋商邀请|谈判邀请)[\))]?\s*$',
|
||||
regex.MULTILINE
|
||||
),
|
||||
regex.compile(
|
||||
r'.*(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)(?:投标人?|磋商|供应商|谈判供应商|磋商供应商)须知(?:前附表)?\s*$',
|
||||
regex.MULTILINE
|
||||
)
|
||||
)
|
||||
]
|
||||
output_suffix = "notice"
|
||||
elif selection == 2:
|
||||
# Selection 2: 评标办法
|
||||
pattern_pairs = [
|
||||
(
|
||||
regex.compile(
|
||||
r'^第[一二三四五六七八九十]+(?:章|部分)\s*(?=.*(?:磋商|谈判|评标|评定|评审))(?=.*(?:办法|方法))',
|
||||
regex.MULTILINE
|
||||
),
|
||||
# Alternative end pattern
|
||||
regex.compile(
|
||||
r'^第[一二三四五六七八九十1-9]+(?:章|部分)\s*[\u4e00-\u9fff]+|[::]清标报告\s*$|'
|
||||
r'(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)评标(方法|办法)正文\s*$|'
|
||||
r'(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)(?:评标|定标)详细程序\s*$',
|
||||
regex.MULTILINE
|
||||
)
|
||||
),
|
||||
(
|
||||
regex.compile(
|
||||
r'(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)'
|
||||
r'第[一二三四五六七八九十1-9]+(?:章|部分)\s*'
|
||||
r'[\u4e00-\u9fff、()()]*?'
|
||||
r'(?=.*(?:磋商|谈判|评标|评定|评审))'
|
||||
r'(?=.*(?:办法|方法))'
|
||||
r'[\u4e00-\u9fff、()()]*\s*$|'
|
||||
r'\s*评标(办法|方法)前附表\s*$',
|
||||
regex.MULTILINE
|
||||
),
|
||||
regex.compile(
|
||||
r'第[一二三四五六七八九十1-9]+(?:章|部分)\s*[\u4e00-\u9fff、()()]+\s*$',
|
||||
regex.MULTILINE
|
||||
)
|
||||
)
|
||||
]
|
||||
output_suffix = "evaluation_method"
|
||||
elif selection == 3:
|
||||
# Selection 3: 资格审查条件
|
||||
pattern_pairs = [
|
||||
(
|
||||
regex.compile(
|
||||
r'^(?:附录(?:[一1])?[::]|附件(?:[一1])?[::]|附表(?:[一1])?[::]).*(?:资质|能力|信誉).*$|'
|
||||
r'(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)第[一二三四五六七八九1-9]+(?:章|部分)\s*'
|
||||
r'[\u4e00-\u9fff、()()]*资格[\u4e00-\u9fff、()()]*\s*$',
|
||||
regex.MULTILINE
|
||||
),
|
||||
regex.compile(
|
||||
r'^(?:附录[一二三四五六七八九1-9]*[::]|附件[一二三四五六七八九1-9]*[::]|附表[一二三四五六七八九1-9]*[::])(?!.*(?:资质|能力|信誉)).*|'
|
||||
r'(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)第[一二三四五六七八九1-9]+(?:章|部分)\s*[\u4e00-\u9fff]+',
|
||||
regex.MULTILINE
|
||||
)
|
||||
)
|
||||
]
|
||||
output_suffix = "qualification"
|
||||
elif selection == 4:
|
||||
# Selection 4: 投标人须知
|
||||
pattern_pairs = [
|
||||
(
|
||||
regex.compile(
|
||||
r'(?:第[一二三四五六七八九十百千]+(?:章|部分)\s*(?:投标人?|磋商|供应商|谈判供应商|磋商供应商)须知|'
|
||||
r'(?:投标人?|磋商|供应商|谈判供应商|磋商供应商)须知前附表)\s*$',
|
||||
regex.MULTILINE
|
||||
),
|
||||
regex.compile(
|
||||
r'第[一二三四五六七八九十]+(?:章|部分)\s*[\u4e00-\u9fff]+|^评标(方法|办法)前附表|^附录(?:一)?[::]|^附件(?:一)?[::]|^附表(?:一)?[::]',
|
||||
regex.MULTILINE
|
||||
)
|
||||
),
|
||||
(
|
||||
regex.compile(
|
||||
r'.*(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)(?:投标人?|磋商|供应商|谈判供应商|磋商供应商)须知\s*$|'
|
||||
r'(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)(?:投标人?|磋商|供应商|谈判供应商|磋商供应商)须知前附表\s*$',
|
||||
regex.MULTILINE
|
||||
),
|
||||
regex.compile(
|
||||
r'第[一二三四五六七八九十]+(?:章|部分)\s*[\u4e00-\u9fff]+|^评标(方法|办法)前附表|^附录(?:一)?[::]|^附件(?:一)?[::]|^附表(?:一)?[::]',
|
||||
regex.MULTILINE
|
||||
)
|
||||
)
|
||||
]
|
||||
output_suffix = "tobidders_notice"
|
||||
elif selection == 5:
|
||||
# Selection 5: 无效标
|
||||
pattern_pairs = [
|
||||
(
|
||||
regex.compile(
|
||||
r'^第[一二三四五六七八九十百千]+(?:章|部分).*?(?:公告|邀请书|邀请函|邀请).*|^第一卷|^投标邀请书|^投标邀请函|^投标邀请'
|
||||
),
|
||||
regex.compile(
|
||||
r'第[一二三四五六七八九十]+章\s*合同|[::]清标报告|^第二卷',
|
||||
regex.MULTILINE
|
||||
)
|
||||
),
|
||||
(
|
||||
regex.compile(
|
||||
r'.*(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)(?:招标公告|磋商公告|谈判公告|邀请书|邀请函|投标邀请|磋商邀请|谈判邀请)[\))]?\s*$',
|
||||
regex.MULTILINE
|
||||
),
|
||||
regex.compile(
|
||||
r'第[一二三四五六七八九十]+章\s*合同|[::]清标报告|^第二卷',
|
||||
regex.MULTILINE
|
||||
)
|
||||
)
|
||||
]
|
||||
output_suffix = "invalid"
|
||||
else:
|
||||
# print(f"Selection {selection}: 使用组合提取函数失败。尝试下一个模式对。")
|
||||
continue
|
||||
output_paths = extract_pages(
|
||||
input_path,
|
||||
output_folder,
|
||||
begin_pattern,
|
||||
begin_page=begin_page,
|
||||
end_pattern=end_pattern,
|
||||
output_suffix=output_suffix,
|
||||
common_header=common_header,
|
||||
is_secondary_match=is_secondary_match
|
||||
)
|
||||
if output_paths and any(os.path.isfile(f) for f in output_paths):
|
||||
# print(f"Selection {selection}: 使用模式对 {idx} 成功提取。")
|
||||
return output_paths # Return immediately upon successful extraction
|
||||
else:
|
||||
# print(f"Selection {selection}: 使用模式对 {idx} 失败。尝试下一个模式对。")
|
||||
continue
|
||||
print("无效的选择:请选择1-5")
|
||||
return [""]
|
||||
|
||||
# If all pattern pairs failed, attempt a fallback
|
||||
print(f"Selection {selection}: 所有模式对都未匹配成功。尝试执行 extract_pages_twice 或返回空。")
|
||||
fallback_result = extract_pages_twice(input_path, output_folder, output_suffix, common_header, last_begin_index)
|
||||
if fallback_result and any(os.path.isfile(f) for f in fallback_result):
|
||||
return fallback_result
|
||||
for idx, (begin_pattern, end_pattern) in enumerate(pattern_pairs, start=1):
|
||||
is_secondary_match = (idx == 2)
|
||||
begin_page = last_begin_index if last_begin_index != 0 else {
|
||||
1: 0, # 公告
|
||||
2: 10, # 评标
|
||||
3: 5, # 资格
|
||||
4: 3, # 前附表
|
||||
5: 0 # 无效标
|
||||
}.get(selection, 0)
|
||||
|
||||
if selection == 4: # 投标人须知
|
||||
output_paths = list(
|
||||
extract_pages_tobidders_notice(
|
||||
file_path,
|
||||
output_folder,
|
||||
begin_pattern,
|
||||
begin_page,
|
||||
common_header,
|
||||
is_secondary_match
|
||||
)
|
||||
)
|
||||
if output_paths and any(os.path.isfile(f) for f in output_paths):
|
||||
return output_paths
|
||||
else:
|
||||
# print(f"Selection {selection}: 使用组合提取函数失败。尝试下一个模式对。")
|
||||
continue
|
||||
output_paths = extract_pages(
|
||||
file_path,
|
||||
output_folder,
|
||||
begin_pattern,
|
||||
begin_page=begin_page,
|
||||
end_pattern=end_pattern,
|
||||
output_suffix=output_suffix,
|
||||
common_header=common_header
|
||||
)
|
||||
if output_paths and any(os.path.isfile(f) for f in output_paths):
|
||||
# print(f"Selection {selection}: 使用模式对 {idx} 成功提取。")
|
||||
return output_paths # 成功提取后立即返回
|
||||
else:
|
||||
# print(f"Selection {selection}: 使用模式对 {idx} 失败。尝试下一个模式对。")
|
||||
continue
|
||||
|
||||
# 如果所有模式对都失败,尝试回退
|
||||
print(f"Selection {selection}: 所有模式对都未匹配成功。尝试执行 extract_pages_twice 或返回空。")
|
||||
fallback_result = extract_pages_twice(
|
||||
file_path,
|
||||
output_folder,
|
||||
output_suffix,
|
||||
common_header,
|
||||
last_begin_index
|
||||
)
|
||||
if fallback_result and any(os.path.isfile(f) for f in fallback_result):
|
||||
return fallback_result
|
||||
else:
|
||||
# 根据 selection 返回相应数量的空字符串
|
||||
empty_returns = ["", ""] if selection == 4 else [""]
|
||||
logger.error(f"Selection {selection}: 回退提取失败,返回空字符串。")
|
||||
return empty_returns
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error in processing file '{file_path}': {e}")
|
||||
return [""] * (2 if selection == 4 else 1)
|
||||
|
||||
# 检查是否为文件夹
|
||||
if os.path.isdir(input_path):
|
||||
generated_files = []
|
||||
for file_name in os.listdir(input_path):
|
||||
if file_name.endswith('.pdf'):
|
||||
file_path = os.path.join(input_path, file_name)
|
||||
files = process_single_file(file_path, output_folder, selection)
|
||||
if files:
|
||||
generated_files.extend(files)
|
||||
return generated_files
|
||||
elif os.path.isfile(input_path) and input_path.endswith('.pdf'):
|
||||
return process_single_file(input_path, output_folder, selection)
|
||||
else:
|
||||
# 根据 selection 返回相应数量的空字符串
|
||||
empty_returns = ["", ""] if selection == 4 else [""]
|
||||
logger.error(f"Selection {selection}: 回退提取失败,返回空字符串。")
|
||||
return empty_returns
|
||||
else:
|
||||
print("Invalid input path. It is neither a PDF file nor a directory containing PDF files.")
|
||||
return ['']
|
||||
print("提供的路径既不是文件夹也不是PDF文件。")
|
||||
return [""] * (2 if selection == 4 else 1)
|
||||
except Exception as e:
|
||||
print(f"Error in truncate_pdf_main_goods: {e}")
|
||||
return [""] * (2 if selection == 4 else 1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
logger=get_global_logger("123")
|
||||
@ -472,10 +464,6 @@ if __name__ == "__main__":
|
||||
# pdf_path = r"C:\Users\Administrator\Desktop\招标文件\招标02.pdf"
|
||||
# input_path=r"C:\Users\Administrator\Desktop\招标文件\招标test文件夹\zbtest8.pdf"
|
||||
output_folder = r"C:\Users\Administrator\Desktop\fsdownload\91399aa4-1ee8-447d-a05b-03cd8d15ced5\tmp"
|
||||
# selections = [1, 4] # 仅处理 selection 4、1
|
||||
selections=[5]
|
||||
# files=truncate_pdf_multiple(pdf_path,output_folder,logger,'engineering',selections)
|
||||
# print(files)
|
||||
# selection = 1 # 例如:1 - 招标公告, 2 - 评标办法, 3 -资格审查条件 4-投标人须知前附表+正文 5-无效标
|
||||
# generated_files = truncate_pdf_main_engineering(pdf_path, output_folder, selection,logger)
|
||||
# print(generated_files)
|
||||
|
@ -1,25 +1,12 @@
|
||||
import logging
|
||||
from PyPDF2 import PdfReader, PdfWriter
|
||||
import regex # 导入正则表达式库
|
||||
import os # 用于文件和文件夹操作
|
||||
from flask_app.general.clean_pdf import clean_page_content, extract_common_header
|
||||
from flask_app.general.format_change import docx2pdf
|
||||
from flask_app.general.merge_pdfs import merge_and_cleanup
|
||||
from flask_app.general.截取pdf通用函数 import get_start_and_common_header
|
||||
from flask_app.general.截取pdf通用函数 import get_start_and_common_header, save_extracted_pages, is_pdf_or_doc, \
|
||||
convert_to_pdf
|
||||
from flask_app.general.通用功能函数 import get_global_logger
|
||||
|
||||
|
||||
def is_pdf_or_doc(filename):
|
||||
# 判断文件是否为PDF或Word文档
|
||||
return filename.lower().endswith(('.pdf', '.doc', '.docx'))
|
||||
|
||||
|
||||
def convert_to_pdf(file_path):
|
||||
# 假设 docx2pdf 函数已经被定义,这里仅根据文件扩展名来决定是否需要转换
|
||||
if file_path.lower().endswith(('.doc', '.docx')):
|
||||
return docx2pdf(file_path)
|
||||
return file_path
|
||||
|
||||
def extract_pages_generic(pdf_document, begin_pattern, end_pattern, begin_page, common_header, exclusion_pattern=None,
|
||||
output_suffix="normal"):
|
||||
start_page = None
|
||||
@ -52,7 +39,7 @@ def extract_pages_generic(pdf_document, begin_pattern, end_pattern, begin_page,
|
||||
return start_page, end_page
|
||||
|
||||
|
||||
def extract_pages(pdf_path, output_folder, begin_pattern, begin_page, end_pattern, output_suffix):
|
||||
def extract_pages(pdf_path, output_folder, begin_pattern, begin_page, end_pattern, output_suffix,logger):
|
||||
try:
|
||||
common_header = extract_common_header(pdf_path)
|
||||
pdf_document = PdfReader(pdf_path)
|
||||
@ -71,11 +58,9 @@ def extract_pages(pdf_path, output_folder, begin_pattern, begin_page, end_patter
|
||||
# if start_page is None or end_page is None or mid_page is None:
|
||||
# print(f"first: {output_suffix} 未找到起始或结束页在文件 {pdf_path} 中!尝试备用提取策略。")
|
||||
# return extract_pages_twice_tobidders_notice(pdf_path, output_folder, output_suffix, common_header,begin_page)
|
||||
path1 = save_extracted_pages(pdf_document, start_page, mid_page, pdf_path, output_folder,
|
||||
"tobidders_notice_part1")
|
||||
path1 = save_extracted_pages(pdf_path,output_folder,start_page, mid_page, "tobidders_notice_part1",common_header)
|
||||
if mid_page != end_page:
|
||||
path2 = save_extracted_pages(pdf_document, mid_page, end_page, pdf_path, output_folder,
|
||||
"tobidders_notice_part2")
|
||||
path2 = save_extracted_pages(pdf_path,output_folder, mid_page, end_page,"tobidders_notice_part2",common_header)
|
||||
else:
|
||||
path2=path1
|
||||
return path1, path2
|
||||
@ -96,13 +81,13 @@ def extract_pages(pdf_path, output_folder, begin_pattern, begin_page, end_patter
|
||||
# 如果未匹配到结束页,默认截取到文件末尾
|
||||
end_page = total_pages
|
||||
print(f"{output_suffix}: 未找到结束页,默认截取到文件末尾。")
|
||||
return save_extracted_pages(pdf_document, start_page, end_page, pdf_path, output_folder, output_suffix)
|
||||
return save_extracted_pages(pdf_path, output_folder,start_page, end_page, output_suffix,common_header)
|
||||
if start_page is None or end_page is None:
|
||||
print(f"first: {output_suffix} 未找到起始或结束页在文件 {pdf_path} 中!尝试备用提取策略。")
|
||||
return extract_pages_twice(pdf_path, output_folder, output_suffix, common_header, begin_page)
|
||||
return extract_pages_twice(pdf_path, output_folder, output_suffix, common_header, begin_page,logger)
|
||||
elif output_suffix == "qualification1":
|
||||
truncate_pdf_main_goods(pdf_path, output_folder, 2, "qualification3") # 合并'资格审查'章节和'评标办法'章节
|
||||
return save_extracted_pages(pdf_document, start_page, end_page, pdf_path, output_folder, output_suffix)
|
||||
truncate_pdf_main_goods(pdf_path, output_folder, 2, logger,"qualification3") # 合并'资格审查'章节和'评标办法'章节
|
||||
return save_extracted_pages(pdf_path, output_folder,start_page, end_page, output_suffix,common_header)
|
||||
except Exception as e:
|
||||
print(f"Error processing {pdf_path}: {e}")
|
||||
return ""
|
||||
@ -181,7 +166,6 @@ def extract_pages_tobidders_notice(pdf_path, begin_pattern, begin_page, common_h
|
||||
start_page = None
|
||||
mid_page = None
|
||||
end_page = None
|
||||
chapter_type = None # 用于存储“章”或“部分”
|
||||
combined_mid_pattern = None # 中间页的组合模式
|
||||
pdf_document = PdfReader(pdf_path)
|
||||
for i, page in enumerate(pdf_document.pages):
|
||||
@ -197,8 +181,7 @@ def extract_pages_tobidders_notice(pdf_path, begin_pattern, begin_page, common_h
|
||||
match = regex.search(local_begin_pattern, cleaned_text)
|
||||
if match and i > begin_page:
|
||||
start_page = i
|
||||
matched_text = match.group(0) # 获取整个匹配的文本
|
||||
|
||||
matched_text:str = match.group(0) # 获取整个匹配的文本
|
||||
# 如果未提供固定的 end_pattern,则根据匹配的章节类型动态生成
|
||||
if not local_end_pattern:
|
||||
if '章' in matched_text:
|
||||
@ -426,7 +409,7 @@ def extract_pages_qualification(pdf_document, begin_page, common_header):
|
||||
return start_page, end_page
|
||||
|
||||
|
||||
def extract_pages_twice(pdf_path, output_folder, output_suffix, common_header, begin_page):
|
||||
def extract_pages_twice(pdf_path, output_folder, output_suffix, common_header, begin_page, logger):
|
||||
try:
|
||||
exclusion_pattern = regex.compile(
|
||||
r'文件的构成|文件的组成|须对应|需对应|须按照|需按照|须根据|需根据|文件组成|文件构成')
|
||||
@ -467,7 +450,7 @@ def extract_pages_twice(pdf_path, output_folder, output_suffix, common_header, b
|
||||
new_file.write(original_file.read())
|
||||
return new_file_path
|
||||
else:
|
||||
temp = truncate_pdf_main_goods(pdf_path, output_folder, 2, "qualification2")
|
||||
temp = truncate_pdf_main_goods(pdf_path, output_folder, 2,logger, "qualification2")
|
||||
if len(temp) > 0:
|
||||
return temp[0]
|
||||
else:
|
||||
@ -475,56 +458,131 @@ def extract_pages_twice(pdf_path, output_folder, output_suffix, common_header, b
|
||||
else:
|
||||
print(f"second: {output_suffix} 未找到起始或结束页在文件 {pdf_path} 中!")
|
||||
return ""
|
||||
return save_extracted_pages(pdf_document, start_page, end_page, pdf_path, output_folder, output_suffix)
|
||||
return save_extracted_pages(pdf_path, output_folder,start_page, end_page, output_suffix,common_header)
|
||||
except Exception as e:
|
||||
print(f"Error in extract_pages_twice: {e}")
|
||||
return ""
|
||||
|
||||
|
||||
def save_extracted_pages(pdf_document, start_page, end_page, pdf_path, output_folder, output_suffix):
|
||||
def truncate_pdf_main_goods(input_path, output_folder, selection,logger, output_suffix="default"):
|
||||
try:
|
||||
# 检查 start_page 和 end_page 是否为 None
|
||||
if start_page is None or end_page is None:
|
||||
print("Error: start_page 或 end_page 为 None")
|
||||
return ""
|
||||
# Function to handle processing of a single file
|
||||
def process_single_file(input_path, output_folder, selection, output_suffix):
|
||||
try:
|
||||
# 创建输出文件夹
|
||||
if not os.path.exists(output_folder):
|
||||
os.makedirs(output_folder)
|
||||
|
||||
base_file_name = os.path.splitext(os.path.basename(pdf_path))[0]
|
||||
output_pdf_path = os.path.join(output_folder, f"{base_file_name}_{output_suffix}.pdf")
|
||||
# 获取起始和通用页眉
|
||||
common_header, last_begin_index = get_start_and_common_header(input_path, 10)
|
||||
begin_page = last_begin_index if last_begin_index != 0 else {
|
||||
4: 1,
|
||||
2: 5,
|
||||
3: 5,
|
||||
1: 0,
|
||||
5: 3,
|
||||
6: 0 # Added default for selection 6 if needed
|
||||
}.get(selection, 0)
|
||||
|
||||
if start_page < 0 or end_page >= len(pdf_document.pages) or start_page > end_page:
|
||||
print(f"无效的页面范围: {start_page} 到 {end_page}")
|
||||
return ""
|
||||
# 根据选择设置对应的模式和结束模式
|
||||
if selection == 1:
|
||||
begin_pattern = regex.compile(
|
||||
r'.*(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)(?:招标公告|磋商公告|谈判公告|邀请书|邀请函|投标邀请|磋商邀请|谈判邀请)[\))]?\s*$',
|
||||
regex.MULTILINE
|
||||
)
|
||||
end_pattern = regex.compile(
|
||||
r'(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)第[一二三四五六七八九十1-9]+(?:章|部分)\s*[\u4e00-\u9fff、()()]+\s*$|'
|
||||
r'(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)(?:投标人?|磋商|供应商|谈判供应商|磋商供应商)须知前附表\s*$',
|
||||
regex.MULTILINE
|
||||
)
|
||||
local_output_suffix = "notice"
|
||||
elif selection == 2:
|
||||
begin_pattern = regex.compile(
|
||||
r'^第[一二三四五六七八九十]+(?:章|部分)\s*(?=.*(?:磋商|谈判|评标|评定|评审))(?=.*(?:办法|方法|内容))'
|
||||
)
|
||||
end_pattern = regex.compile(
|
||||
r'^第[一二三四五六七八九十百千]+(?:章|部分)\s*[\u4e00-\u9fff]+'
|
||||
)
|
||||
local_output_suffix = "evaluation_method"
|
||||
elif selection == 3:
|
||||
begin_pattern = regex.compile(
|
||||
r'^第[一二三四五六七八九十百千]+(?:章|部分).*?(资格审查).*', regex.MULTILINE
|
||||
)
|
||||
end_pattern = regex.compile(
|
||||
r'^第[一二三四五六七八九十百千]+(?:章|部分)\s*[\u4e00-\u9fff]+', regex.MULTILINE
|
||||
)
|
||||
local_output_suffix = "qualification1"
|
||||
elif selection == 4:
|
||||
begin_pattern = regex.compile(
|
||||
r'^第[一二三四五六七八九十百千]+(?:章|部分)\s*(?:投标人?|磋商|供应商|谈判供应商|磋商供应商)须知+|'
|
||||
r'(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)(?:投标人?|磋商|供应商|谈判供应商|磋商供应商)须知前附表\s*$',
|
||||
regex.MULTILINE
|
||||
)
|
||||
end_pattern = None
|
||||
local_output_suffix = "tobidders_notice"
|
||||
elif selection == 5:
|
||||
begin_pattern = regex.compile(
|
||||
r'^第[一二三四五六七八九十百千]+(?:章|部分).*?(?:服务|项目|商务|技术).*?要求|'
|
||||
r'^第[一二三四五六七八九十百千]+(?:章|部分)(?!.*说明).*(?:采购内容|采购要求|需求).*'
|
||||
)
|
||||
end_pattern = regex.compile(
|
||||
r'^第[一二三四五六七八九十百千]+(?:章|部分)\s*[\u4e00-\u9fff]+'
|
||||
)
|
||||
local_output_suffix = "procurement"
|
||||
elif selection == 6:
|
||||
begin_pattern = regex.compile(
|
||||
r'^第[一二三四五六七八九十百千]+(?:章|部分).*?(?:格式).*'
|
||||
)
|
||||
end_pattern = regex.compile(
|
||||
r'^第[一二三四五六七八九十百千]+(?:章|部分)\s*[\u4e00-\u9fff]+', regex.MULTILINE
|
||||
)
|
||||
local_output_suffix = "format"
|
||||
else:
|
||||
print("无效的选择:请选择1-6")
|
||||
return ['']
|
||||
|
||||
if output_suffix == 'notice' and start_page - 1 >= 0:
|
||||
before_pdf_path = os.path.join(output_folder, f"{base_file_name}_before.pdf")
|
||||
before_doc = PdfWriter()
|
||||
for page_num in range(0, start_page):
|
||||
before_doc.add_page(pdf_document.pages[page_num])
|
||||
with open(before_pdf_path, 'wb') as f:
|
||||
before_doc.write(f)
|
||||
# print(f"已保存页面从 0 到 {start_page - 1} 为 {before_pdf_path}")
|
||||
# 如果传入的 output_suffix 是 'default',则使用本地生成的 output_suffix
|
||||
if output_suffix == "default":
|
||||
output_suffix = local_output_suffix
|
||||
|
||||
output_doc = PdfWriter()
|
||||
for page_num in range(start_page, end_page + 1):
|
||||
output_doc.add_page(pdf_document.pages[page_num])
|
||||
with open(output_pdf_path, 'wb') as f:
|
||||
output_doc.write(f)
|
||||
print(f"{output_suffix} 已截取并保存页面从 {start_page} 到 {end_page} 为 {output_pdf_path}")
|
||||
return output_pdf_path
|
||||
except Exception as e:
|
||||
print(f"Error in save_extracted_pages: {e}")
|
||||
return "" # 返回空字符串
|
||||
# 将原先的 process_files 逻辑合并到此处
|
||||
pdf_path = convert_to_pdf(input_path)
|
||||
result = extract_pages(
|
||||
pdf_path,
|
||||
output_folder,
|
||||
begin_pattern,
|
||||
begin_page,
|
||||
end_pattern,
|
||||
output_suffix,
|
||||
logger
|
||||
)
|
||||
# 根据提取结果以及不同的 output_suffix 进行处理
|
||||
if result:
|
||||
if output_suffix == "tobidders_notice":
|
||||
# 确保返回的是元组,并将其中的 None 转换为 ""
|
||||
if isinstance(result, tuple) and len(result) == 2:
|
||||
path1, path2 = result
|
||||
return [path1 or "", path2 or ""]
|
||||
else:
|
||||
logger.error("Unexpected result format for 'tobidders_notice'")
|
||||
return ["",""]
|
||||
elif output_suffix == "qualification1":
|
||||
merge_and_cleanup(result, "qualification3")
|
||||
return [result or ""]
|
||||
else:
|
||||
return [result or ""]
|
||||
return [""] * (2 if selection == 4 else 1)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error in processing file '{input_path}': {e}")
|
||||
return [""] * (2 if selection == 4 else 1)
|
||||
|
||||
def truncate_pdf_main_goods(input_path, output_folder, selection, output_suffix="default"):
|
||||
try:
|
||||
# 检查是否为文件夹
|
||||
if os.path.isdir(input_path):
|
||||
generated_files = []
|
||||
for file_name in os.listdir(input_path):
|
||||
file_path = os.path.join(input_path, file_name)
|
||||
if is_pdf_or_doc(file_path):
|
||||
result = process_input(file_path, output_folder, selection, output_suffix)
|
||||
result = process_single_file(file_path, output_folder, selection, output_suffix)
|
||||
if isinstance(result, tuple):
|
||||
generated_files.extend([f if f else "" for f in result])
|
||||
else:
|
||||
@ -533,102 +591,17 @@ def truncate_pdf_main_goods(input_path, output_folder, selection, output_suffix=
|
||||
|
||||
# 单文件情况
|
||||
elif os.path.isfile(input_path) and is_pdf_or_doc(input_path):
|
||||
return process_input(input_path, output_folder, selection, output_suffix)
|
||||
return process_single_file(input_path, output_folder, selection, output_suffix)
|
||||
|
||||
else:
|
||||
print("提供的路径既不是文件夹也不是PDF文件。")
|
||||
return ['']
|
||||
logger.error("提供的路径既不是文件夹也不是PDF文件。")
|
||||
return [""] * (2 if selection == 4 else 1)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error in truncate_pdf_main: {e}")
|
||||
return [''] # 返回空字符串
|
||||
logger.error(f"Error in truncate_pdf_main_goods: {e}")
|
||||
return [""] * (2 if selection == 4 else 1) # 返回空字符串
|
||||
|
||||
|
||||
def process_input(input_path, output_folder, selection, output_suffix):
|
||||
try:
|
||||
# 创建输出文件夹
|
||||
if not os.path.exists(output_folder):
|
||||
os.makedirs(output_folder)
|
||||
|
||||
# 获取起始和通用页眉
|
||||
common_header, last_begin_index = get_start_and_common_header(input_path,10)
|
||||
begin_page = last_begin_index if last_begin_index != 0 else {
|
||||
4: 1,
|
||||
2: 5,
|
||||
3: 5,
|
||||
1: 0,
|
||||
5: 3
|
||||
}.get(selection, 0)
|
||||
|
||||
# 根据选择设置对应的模式和结束模式
|
||||
if selection == 1:
|
||||
begin_pattern = regex.compile(
|
||||
r'.*(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)(?:招标公告|磋商公告|谈判公告|邀请书|邀请函|投标邀请|磋商邀请|谈判邀请)[\))]?\s*$',
|
||||
regex.MULTILINE
|
||||
)
|
||||
end_pattern = regex.compile(r'(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)第[一二三四五六七八九十1-9]+(?:章|部分)\s*[\u4e00-\u9fff、()()]+\s*$|(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)(?:投标人?|磋商|供应商|谈判供应商|磋商供应商)须知前附表\s*$', regex.MULTILINE)
|
||||
local_output_suffix = "notice"
|
||||
elif selection == 2:
|
||||
begin_pattern = regex.compile(
|
||||
r'^第[一二三四五六七八九十]+(?:章|部分)\s*(?=.*(?:磋商|谈判|评标|评定|评审))(?=.*(?:办法|方法|内容))')
|
||||
end_pattern = regex.compile(r'^第[一二三四五六七八九十百千]+(?:章|部分)\s*[\u4e00-\u9fff]+')
|
||||
local_output_suffix = "evaluation_method"
|
||||
elif selection == 3:
|
||||
begin_pattern = regex.compile(r'^第[一二三四五六七八九十百千]+(?:章|部分).*?(资格审查).*', regex.MULTILINE)
|
||||
end_pattern = regex.compile(r'^第[一二三四五六七八九十百千]+(?:章|部分)\s*[\u4e00-\u9fff]+', regex.MULTILINE)
|
||||
local_output_suffix = "qualification1"
|
||||
elif selection == 4:
|
||||
begin_pattern = regex.compile(
|
||||
r'^第[一二三四五六七八九十百千]+(?:章|部分)\s*(?:投标人?|磋商|供应商|谈判供应商|磋商供应商)须知+|(?<!见\s*)(?<!与\s*)(?<!"\s*)(?<!“\s*)(?<!”\s*)(?:投标人?|磋商|供应商|谈判供应商|磋商供应商)须知前附表\s*$',
|
||||
regex.MULTILINE)
|
||||
end_pattern = None
|
||||
local_output_suffix = "tobidders_notice"
|
||||
elif selection == 5:
|
||||
begin_pattern = regex.compile(
|
||||
r'^第[一二三四五六七八九十百千]+(?:章|部分).*?(?:服务|项目|商务|技术).*?要求|^第[一二三四五六七八九十百千]+(?:章|部分)(?!.*说明).*(?:采购内容|采购要求|需求).*') # 包头中有一章'采购相关说明'
|
||||
end_pattern = regex.compile(r'^第[一二三四五六七八九十百千]+(?:章|部分)\s*[\u4e00-\u9fff]+')
|
||||
local_output_suffix = "procurement"
|
||||
|
||||
# begin_pattern = regex.compile(
|
||||
# r'^第[一二三四五六七八九十百千]+(?:章|部分).*?(?:服务|项目|商务).*?要求|'
|
||||
# r'^第[一二三四五六七八九十百千]+(?:章|部分).*?(?:采购|技术标准).*|'
|
||||
# r'^[一二三四五六七八九十百千]+、\s*采购清单', regex.MULTILINE)
|
||||
# end_pattern = regex.compile(
|
||||
# r'^第[一二三四五六七八九十百千]+(?:章|部分)\s*[\u4e00-\u9fff]+', regex.MULTILINE)
|
||||
|
||||
elif selection == 6:
|
||||
begin_pattern = regex.compile(r'^第[一二三四五六七八九十百千]+(?:章|部分).*?(?:格式).*')
|
||||
end_pattern = regex.compile(r'^第[一二三四五六七八九十百千]+(?:章|部分)\s*[\u4e00-\u9fff]+', regex.MULTILINE)
|
||||
local_output_suffix = "format"
|
||||
else:
|
||||
print("无效的选择:请选择1-5")
|
||||
return ['']
|
||||
|
||||
# 如果传入的 output_suffix 是 'default',则使用本地生成的 output_suffix
|
||||
if output_suffix == "default":
|
||||
output_suffix = local_output_suffix
|
||||
|
||||
# 将原先的 process_files 逻辑合并到此处
|
||||
pdf_path = convert_to_pdf(input_path)
|
||||
result = extract_pages(pdf_path, output_folder, begin_pattern, begin_page, end_pattern, output_suffix)
|
||||
|
||||
# 根据提取结果以及不同的 output_suffix 进行处理
|
||||
if result:
|
||||
if output_suffix == "tobidders_notice":
|
||||
# 确保返回的是元组,并将其中的 None 转换为 ""
|
||||
path1, path2 = result
|
||||
return [path1 or "", path2 or ""]
|
||||
elif output_suffix == "qualification1":
|
||||
merge_and_cleanup(result, "qualification3")
|
||||
return [result or ""]
|
||||
return [result or ""]
|
||||
|
||||
return [""]
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error in process_input: {e}")
|
||||
return ['']
|
||||
|
||||
|
||||
# TODO:交通智能系统和招标(1)(1)文件有问题 包头 绍兴 工程标中,判断是符合性审查之后,可以将它们设为同一章
|
||||
|
||||
@ -636,15 +609,12 @@ def process_input(input_path, output_folder, selection, output_suffix):
|
||||
if __name__ == "__main__":
|
||||
logger = get_global_logger("123")
|
||||
# input_path = r"C:\Users\Administrator\Desktop\new招标文件\货物标"
|
||||
pdf_path = r"C:\Users\Administrator\Desktop\招标文件-采购类\2024-贵州-贵州医科大学附属医院导视系统零星制作安装项目.pdf"
|
||||
# input_path="C:\\Users\\Administrator\\Desktop\\货物标\\zbfiles\\zbtest4_evaluation_method.pdf"
|
||||
# pdf_path = r"C:\Users\Administrator\Desktop\招标文件-采购类\2024-贵州-贵州医科大学附属医院导视系统零星制作安装项目.pdf"
|
||||
# pdf_path="C:\\Users\\Administrator\\Desktop\\货物标\\zbfiles"
|
||||
# input_path = r"C:\Users\Administrator\Desktop\货物标\zbfiles\2-招标文件(广水市教育局封闭管理).pdf"
|
||||
# pdf_path=r"C:\Users\Administrator\Desktop\货物标\zbfiles\zbtest4_evaluation_method.pdf"
|
||||
output_folder = r"C:\Users\Administrator\Desktop\fsdownload\ff2acdba-3a55-48a7-aa7a-61d9f89b909a\tmp"
|
||||
pdf_path=r"C:\Users\Administrator\Desktop\货物标\zbfiles\zbtest4_evaluation_method.pdf"
|
||||
output_folder = r"C:\Users\Administrator\Desktop\货物标\output33"
|
||||
# output_folder = r"C:\Users\Administrator\Desktop\new招标文件\output2"
|
||||
# selections = [1, 4]
|
||||
# files = truncate_pdf_multiple(pdf_path, output_folder,logger,selections)
|
||||
# print(files)
|
||||
# selection = 4 # 例如:1 - 公告, 2 - 评标办法, 3 - 资格审查后缀有qualification1或qualification2(与评标办法一致) 4.投标人须知前附表part1 投标人须知正文part2 5-采购需求
|
||||
# generated_files = truncate_pdf_main_goods(pdf_path, output_folder, selection)
|
||||
# print(generated_files)
|
||||
selection = 3 # 例如:1 - 公告, 2 - 评标办法, 3 - 资格审查后缀有qualification1或qualification2(与评标办法一致) 4.投标人须知前附表part1 投标人须知正文part2 5-采购需求
|
||||
generated_files = truncate_pdf_main_goods(pdf_path, output_folder, selection,logger)
|
||||
print(generated_files)
|
||||
|
Loading…
x
Reference in New Issue
Block a user