You need to enable JavaScript to run this app. Python如何实现按指定文本字符串将大PDF分割为独立小PDF文件
优惠活动
大模型
产品
解决方案
定价
更多

Python如何实现按指定文本字符串将大PDF分割为独立小PDF文件

Python实现按指定文本拆分PDF的可行方案

核心实现逻辑

整体流程和你现有的单页拆分逻辑是打通的,只需要在逐页处理的环节增加文本匹配环节、调整拆分触发条件即可,不需要推翻原有代码逻辑。

方案1:基于PyPDF2的整页级拆分(适配现有代码)

这个方案完全复用你已经在用的PyPDF2依赖,不需要额外安装第三方库,拆分粒度为整页。

实现原理

  • 遍历每一页PDF时,调用PyPDF2自带的extract_text()方法提取当前页的纯文本内容
  • 检查提取的文本是否包含目标字符串(如"07.5")
  • 当检测到命中目标串的页面时,将之前累积的所有连续页面打包导出为一个独立PDF,再将当前页作为下一个拆分文件的起始页

适配你原有代码的实现示例

from PyPDF2 import PdfFileWriter, PdfFileReader

# 可自定义配置
target_str = "07.5"
input_pdf = PdfFileReader(open(r"file.pdf", "rb"))
current_chunk_pages = []  # 暂存当前待导出分片的页面
chunk_index = 0

for page_idx in range(input_pdf.numPages):
    current_page = input_pdf.getPage(page_idx)
    page_text = current_page.extract_text()
    # 命中目标串时触发拆分
    if target_str in page_text and current_chunk_pages:
        writer = PdfFileWriter()
        for p in current_chunk_pages:
            writer.addPage(p)
        with open(f"split_part_{chunk_index}.pdf", "wb") as output_stream:
            writer.write(output_stream)
        # 重置分片列表,将当前页作为新分片的开头
        current_chunk_pages = [current_page]
        chunk_index += 1
    else:
        current_chunk_pages.append(current_page)

# 导出最后剩余的未打包分片
if current_chunk_pages:
    writer = PdfFileWriter()
    for p in current_chunk_pages:
        writer.addPage(p)
    with open(f"split_part_{chunk_index}.pdf", "wb") as output_stream:
        writer.write(output_stream)

方案局限性

仅支持整页级拆分——只要某一页出现目标串,就会从这一页的页首位置拆分,没办法在同一页内部、目标串实际所在的垂直位置完成切割。

方案2:基于PyMuPDF的页内细粒度拆分

如果需求是严格在目标字符串出现的位置前拆分页面,把同一页里目标串以上的内容归到上一个文件、以下的内容归到下一个文件,就需要使用支持文本坐标定位的PyMuPDF(fitz)库实现。

实现原理

  • 逐页加载PDF,调用库自带的search_for()方法直接检索目标字符串,拿到字符串在页面上的垂直坐标位置
  • 根据匹配到的坐标值,将当前页切割为上下两个独立的矩形页面块
  • 将上半部分的页面块归入上一个待导出分片,下半部分的页面块作为下一个分片的起始内容
  • 当触发拆分条件时,累积的分片直接导出为独立PDF文件

核心实现示例

import fitz  # PyMuPDF 库,需提前通过pip安装

target_str = "07.5"
doc = fitz.open(r"file.pdf")
current_chunk = fitz.open()  # 暂存当前分片内容
chunk_idx = 0

for page_num in range(doc.page_count):
    page = doc.load_page(page_num)
    # 检索目标字符串在页面上的位置
    match_positions = page.search_for(target_str)
    if match_positions:
        # 取第一个匹配位置的顶部垂直坐标作为拆分点
        split_y = match_positions[0].y0
        page_rect = page.rect
        # 生成上半部分切割页,归入上一个分片
        top_clip = fitz.Rect(0, 0, page_rect.width, split_y)
        top_page = doc.new_page(width=page_rect.width, height=split_y)
        top_page.show_pdf_page(top_clip, doc, page_num, clip=top_clip)
        current_chunk.insert_pdf(top_page)
        # 导出上一个分片文件
        current_chunk.save(f"split_part_{chunk_idx}.pdf")
        current_chunk.close()
        # 重置分片,生成下半部分切割页作为新分片的开头
        current_chunk = fitz.open()
        bottom_clip = fitz.Rect(0, split_y, page_rect.width, page_rect.height)
        bottom_page = doc.new_page(width=page_rect.width, height=page_rect.height - split_y)
        bottom_page.show_pdf_page(bottom_clip, doc, page_num, clip=bottom_clip)
        current_chunk.insert_pdf(bottom_page)
        chunk_idx += 1
    else:
        # 未命中目标串则将整页加入当前分片
        current_chunk.insert_pdf(doc, from_page=page_num, to_page=page_num)

# 导出最后剩余的分片内容
if not current_chunk.is_empty:
    current_chunk.save(f"split_part_{chunk_idx}.pdf")
    current_chunk.close()

注意事项

  • 扫描版PDF本质是图片内容,没有可提取的文本层,两种方案都无法直接匹配文本,需要先对PDF做OCR识别生成可检索的文本层后才能实现拆分
  • 匹配时可增加位置过滤逻辑,排除页眉、页脚、注释区域的目标串误命中,避免错误拆分
  • 如果目标字符串跨页分布,可以根据实际需求调整匹配时的边界判断逻辑

内容的提问来源于stack exchange,提问作者Sammi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.02 10:36:03