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.merge_pdfs import merge_and_cleanup from flask_app.general.截取pdf通用函数 import get_start_and_common_header, save_extracted_pages, is_pdf_or_doc, \ convert_to_pdf, get_invalid_file, extract_pages_tobidders_notice, extract_pages_generic from flask_app.general.通用功能函数 import get_global_logger 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) exclusion_pattern = None if output_suffix == "tobidders_notice": exclusion_pattern = regex.compile( r'文件的构成|文件的组成|文件构成|文件组成|文件的编制|文件编制') start_page, mid_page, end_page = extract_pages_tobidders_notice( pdf_path, begin_pattern, begin_page, common_header, exclusion_pattern ) if not start_page or not mid_page or not end_page: print(f"三次提取tobidders_notice均失败!!{pdf_path}") return "", "" 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_path,output_folder, mid_page, end_page,"tobidders_notice_part2",common_header) else: path2=path1 return path1, path2 else: # 原有的处理逻辑保持不变 if output_suffix == "qualification1" or output_suffix == "procurement" or output_suffix == "evaluation_method": exclusion_pattern = regex.compile( r'文件的构成|文件的组成|文件构成|文件组成|文件的编制|文件编制') start_page, end_page = extract_pages_generic(pdf_document, begin_pattern, end_pattern, begin_page, common_header, exclusion_pattern, output_suffix) 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,logger) elif output_suffix == "qualification1": 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 "" def get_patterns_for_procurement(): begin_pattern = regex.compile( r'(? start_page: end_page = i # print(f"找到结束页 (附件类): {end_page}") break # 找到结束页后退出循环 else: print(f"当前页面匹配附件结束模式但包含 include_keywords,继续查找。页面: {i}") # 确定结束页 - 章节标题 elif start_page is not None and end_pattern_chapter.search(cleaned_text): if i > start_page: end_page = i print(f"找到结束页 (章节标题): {end_page}") break # 找到结束页后退出循环 return start_page, end_page def extract_pages_twice(pdf_path, output_folder, output_suffix, common_header, begin_page, logger): try: exclusion_pattern = regex.compile( r'文件的构成|文件的组成|须对应|需对应|须按照|需按照|须根据|需根据|文件组成|文件构成|文件的编制|文件编制') pdf_document = PdfReader(pdf_path) patterns = None start_page = None end_page = None if output_suffix == "procurement": patterns = [get_patterns_for_procurement()] elif output_suffix == "evaluation_method" or output_suffix == "qualification2" or output_suffix == "qualification3": patterns = [get_patterns_for_evaluation_method()] elif output_suffix == "notice": patterns = [get_patterns_for_notice()] elif output_suffix == "qualification1": start_page, end_page = extract_pages_qualification(pdf_document, begin_page, common_header) if patterns: for pattern_pair in patterns: start_page, end_page = extract_pages_generic(pdf_document, pattern_pair[0], pattern_pair[1], begin_page, common_header, exclusion_pattern, output_suffix) if start_page is not None and end_page is not None: break if start_page is None or end_page is None: if output_suffix == "qualification1": # print(f"second: {output_suffix} 未找到起始或结束页在文件 {pdf_path} 中!") print("第三次尝试资格审查:尝试提取评分办法章节...") base_file_name = os.path.splitext(os.path.basename(pdf_path))[0] evaluation_method_file = os.path.join(output_folder, f"{base_file_name}_evaluation_method.pdf") if os.path.isfile(evaluation_method_file): print(f"找到评分办法章节文件: {evaluation_method_file},生成新文件。") # 获取文件路径和文件名 new_file_name = f"{base_file_name}_qualification2.pdf" new_file_path = os.path.join(output_folder, new_file_name) # 复制文件 with open(evaluation_method_file, 'rb') as original_file: with open(new_file_path, 'wb') as new_file: new_file.write(original_file.read()) return new_file_path else: temp = truncate_pdf_main_goods(pdf_path, output_folder, 2,logger, "qualification2") if len(temp) > 0: return temp[0] else: return "" else: print(f"second: {output_suffix} 未找到起始或结束页在文件 {pdf_path} 中!") return "" 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 truncate_pdf_main_goods(input_path, output_folder, selection,logger, output_suffix="default"): try: # 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) # 获取起始和通用页眉 pdf_path = convert_to_pdf(input_path) 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: 0, 2: 5, 3: 5, 1: 0, 5: 3, 6: 0 # Added default for selection 6 if needed }.get(selection, 0) # 根据选择设置对应的模式和结束模式 if selection == 1: begin_pattern = regex.compile( r'.*(?