You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python实现PDF关键词高亮及非交互式复选框缺失检测问题咨询

PDF文本扫描高亮工具的复选框检测需求

我正在开发一款程序,用于扫描从网站下载的可选中文本的PDF文件,高亮标记特定异常内容。目前已经实现了针对指定「合规关键词(good words)」和「违规关键词(bad words)」的识别高亮功能,当前卡在非交互型复选框的缺失检测需求上——这类复选框不属于PDF表单中的可交互字段。
示例截图

已实现功能代码

import os
import fitz

source_folder = r"C:\Users\Sserb\Desktop\Test Files"
list_files = os.listdir(source_folder)

good_terms = ["trend", "decrease", "increase"]
bad_terms = ["school", "academic", "homework"]  # 每个PDF文件必须包含的词汇(无需每页都出现)

pdf_files = [x for x in list_files if x.endswith(".pdf")]

highlight_summary = []
good_term_summary = []

for file_name in pdf_files:
    # 读取PDF文件
    full_filename = os.path.join(source_folder, file_name)
    doc = fitz.open(full_filename)

    good_terms_not_found = good_terms.copy()

    list_hl_pages = []
    for page_num, page in enumerate(doc, 1):

        # 搜索关键词
        for text in bad_terms:
            text_instances = page.search_for(text)

            # 添加高亮标注
            for inst in text_instances:
                highlight = page.addHighlightAnnot(inst)
                highlight.update()
                if page_num not in list_hl_pages:
                    list_hl_pages.append(page_num)

        # 搜索合规关键词:所有合规关键词必须在PDF中出现
        words_found = []
        for good_word in good_terms_not_found:
            text_instances = page.search_for(good_word)
            if text_instances:
                words_found.append(good_word)

        for word in words_found:
            good_terms_not_found.remove(word)

    highlight_summary.append([file_name, list_hl_pages.copy()])
    if good_terms_not_found:
        good_term_summary.append([file_name, good_terms_not_found.copy()])

    # 输出处理结果
    if list_hl_pages:
        out_file = file_name.replace(".pdf", "-errors.pdf")
        doc.save(os.path.join(source_folder, "output", out_file), garbage=4, deflate=True, clean=True)
    else:
        doc.close()

#print(highlight_summary)
print(good_term_summary)
output_folder=r"C:\Users\Sserb\Desktop\Test Files\output"
new = os.path.join(output_folder,'outputfile.txt')
file = open(new, 'w')
value = str(good_term_summary) + '\n'
file.write(value)
file.close()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 17:18:01