#flask_app/工程标/截取pdf工程标版.py import regex import os 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.通用功能函数 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) exclusion_pattern = regex.compile(r'文件的构成|文件的组成|须对应|需对应|须按照|需按照|须根据|需根据|文件组成|文件构成') def run_extraction(): start_page = None mid_page = None end_page = None chapter_type = None # 用于存储“章”或“部分” for i, page in enumerate(pdf_document.pages): text = page.extract_text() or "" cleaned_text = clean_page_content(text, common_header) if exclusion_pattern and regex.search(exclusion_pattern, cleaned_text) and mid_page is not None: continue if start_page is None: match = regex.search(begin_pattern, cleaned_text) if match and i > begin_page: start_page = i matched_text = match.group(0) # 获取整个匹配的文本 if '章' in matched_text: chapter_type = '章' elif '部分' in matched_text: chapter_type = '部分' else: chapter_type = None # 未匹配到“章”或“部分” if chapter_type: # 根据 chapter_type 动态生成 end_pattern if not is_secondary_match: end_pattern = regex.compile( rf'^第[一二三四五六七八九十百千]+?(?:{chapter_type})\s*[\u4e00-\u9fff]+|' r'^评标(方法|办法)前附表|' r'^附录(?:一)?[::]|' r'^附件(?:一)?[::]|' r'^附表(?:一)?[::]', regex.MULTILINE ) else: end_pattern = regex.compile( rf'第[一二三四五六七八九十百千]+?(?:{chapter_type})\s*[\u4e00 -\u9fff]+\s*$' ) # 根据 chapter_type 动态生成 additional_mid_pattern if chapter_type == '章': additional_mid_pattern = r'^第[一二三四五六七八九十百千]+?(?:部分)' elif chapter_type == '部分': additional_mid_pattern = r'^第[一二三四五六七八九十百千]+?(?:章)' else: additional_mid_pattern = '' # 定义基础的 mid_pattern base_mid_pattern = r'^\s*(?:[((]\s*[一二12]?\s*[))]\s*[、..]*|' \ r'[一二12][、..]+|[、..]+)\s*(说\s*明|总\s*则|名\s*词\s*解\s*释)' \ r'|(? mid_page: end_page = i break return start_page, mid_page, end_page # 运行提取 start_page, mid_page, end_page = run_extraction() if start_page is None or end_page is None or mid_page is None: 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) return path1, path2 def extract_pages(pdf_path, output_folder, begin_pattern, begin_page, end_pattern, output_suffix, common_header, is_secondary_match=False): # 打开PDF文件 pdf_document = PdfReader(pdf_path) start_page = None end_page = None exclusion_pattern = regex.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_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: if start_page and (output_suffix == "notice" or output_suffix == "invalid"): pass else: start_page = i if start_page is not None and regex.search(end_pattern, cleaned_text) and not regex.search(begin_pattern,cleaned_text): condition = i > start_page if condition: is_invalid_condition = output_suffix == "invalid" and i > 30 # 这边默认无效投标至少有30页 if is_invalid_condition or output_suffix != "invalid": end_page = i break # 找到结束页后退出循环 # 移除对 extract_pages_twice 的调用 if start_page is None or end_page is None: 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)] def extract_pages_twice(pdf_path, output_folder, output_suffix, common_header, last_begin_index): """ 处理 PDF 文件,作为 truncate_pdf_main 的后备方法。 此函数将在所有模式对都失败后调用。 """ try: pdf_document = PdfReader(pdf_path) if output_suffix == "qualification": print("twice:qualificaiton!") # 动态设置 include_keys include_keys = ["资格审查", "资质审查", "符合性审查", "资格性检查", "符合性检查","资格检查","能力","信誉"] # 定义起始匹配模式,仅匹配“附录”、“附件”或“附表” begin_pattern = r'^(?:附录(?:[一1])?[::]|附件(?:[一1])?[::]|附表(?:[一1])?[::])' # 定义结束匹配模式 - 附录、附件、附表等(移除负向前瞻) end_pattern_attachment = regex.compile( r'^(?:附录[一二三四五六七八九1-9]*[::]|附件[一二三四五六七八九1-9]*[::]|附表[一二三四五六七八九1-9]*[::]).*$', regex.MULTILINE ) # 定义结束匹配模式 - 章节标题、评标附表、投标人须知等 end_pattern_chapter = regex.compile( r'第[一二三四五六七八九]+(?:章|部分)\s*[\u4e00-\u9fff、()()]+\s*$|' r'\s*评标(?:办法|方法)前附表\s*$|' r'投标人须知', regex.MULTILINE ) start_page = None end_page = None # 从指定的开始页开始遍历 for i, page in enumerate(pdf_document.pages[last_begin_index:], start=last_begin_index): text = page.extract_text() if text: cleaned_text = clean_page_content(text, common_header) # 确定起始页,需在last_begin_index之后 if any(key in cleaned_text for key in include_keys): if regex.search(begin_pattern, cleaned_text, regex.MULTILINE): if start_page is None: start_page = i # 确保起始页不小于章节的开始页码 continue # print(f"匹配到附录/附件/附表,设置起始页为: {start_page}") # 确定结束页 - 附录、附件、附表等 if start_page is not None and end_pattern_attachment.search(cleaned_text): # 额外检查当前页面是否不包含任何 include_keys if not any(key in cleaned_text for key in include_keys): if i > start_page: end_page = i # print(f"找到结束页 (附件类): {end_page}") break # 找到结束页后退出循环 else: print(f"当前页面匹配附件结束模式但包含 include_keys,继续查找。页面: {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 # 找到结束页后退出循环 if start_page is None or end_page is None: 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)] elif output_suffix == "invalid": pdf_document = PdfReader(pdf_path) total_pages = len(pdf_document.pages) # 计算总页数的三分之二 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)] else: print(f"{output_suffix} twice: 未定义的输出后缀。") return [] except Exception as e: print(f"Error in extract_pages_twice: {e}") 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'.*(?