当前位置: 首页 > Skill >

Document Sanitizer

Document Sanitizer

Document Sanitizer

批量使用关键词和正则规则对docx/xlsx文件进行脱敏,支持一键可逆恢复,替换敏感词(公司名、个人信息等)。

1.5.1 2026.11.27 110
获取Skill文件查看使用说明
作 者:江川 许可证: 更新时间:2026.11.27
本文目录展开

document-sanitizer

技能说明 / Skill Description

概述 / Overview

本技能用于批量对 Word (.docx) 和 Excel (.xlsx) 文件执行脱敏处理,支持关键字精确替换和正则表达式动态替换,并提供一键恢复功能。

This skill batch-desensitizes Word (.docx) and Excel (.xlsx) files using keyword exact-match and regex dynamic replacement, with one-click reversible restoration.

核心特性 / Key Features

特性 说明 / Description
关键字精确替换 / Exact Match 配置关键字对,替换为带 [] 标记的占位符(如"白云"→[黑水])
正则动态替换 / Regex Dynamic 匹配手机号、身份证号、邮箱等,自动生成占位符如 [RED_手机号_1]
文件名脱敏 / Filename Sanitization 默认开启,输出文件自动使用脱敏后的文件名
统一脱敏记录 / Unified Record 所有映射累积到 _sanitize_record.json,无论文档如何修改都能恢复
一键恢复 / One-Click Restore 读取记录反向替换,还原文件名和内容,校验残留占位符
旧格式自动转换 / Legacy Format Conversion 检测到 .doc/.xls 时提示自动转换为 .docx/.xlsx

前置依赖 / Prerequisites

Python 包依赖 / Python Dependencies

pip install python-docx openpyxl

可选依赖(旧格式转换)/ Optional (Legacy Format Conversion)

pip install xlrd pywin32
  • .doc → .docx 转换需要 Windows + Microsoft Word(使用 Word COM 自动化)
  • .xls → .xlsx 转换使用 xlrd + openpyxl(跨平台)

使用方法 / Usage

1. 准备配置文件 / Prepare Config File

在工作目录下创建 _sanitize_config.json:

Create _sanitize_config.json in your workspace:

{
  "exact_rules": [
    {"pattern": "白云", "replacement": "黑水"},
    {"pattern": "南方", "replacement": "北风"},
    {"pattern": "广州", "replacement": "镇北"}
  ],
  "regex_rules": [
    {"pattern": "1[3-9]\d{9}", "label": "手机号"},
    {"pattern": "\d{6}(?:19|20)\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])\d{3}[\dXx]", "label": "身份证号"},
    {"pattern": "[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}", "label": "邮箱"}
  ]
}

2. 执行脱敏 / Run Sanitization

# 基本脱敏(默认脱敏文件名)
python <skill_dir>/scripts/sanitize.py sanitize <工作目录>

# 不脱敏文件名
python <skill_dir>/scripts/sanitize.py sanitize <工作目录> --no-rename

# 自动转换 .doc/.xls 旧格式文件
python <skill_dir>/scripts/sanitize.py sanitize <工作目录> --auto-convert

3. 恢复文档 / Restore Documents

python <skill_dir>/scripts/sanitize.py restore <工作目录>

命令行参数 / CLI Arguments

参数 / Argument 说明 / Description
sanitize <workspace> 执行脱敏 / Run sanitization
restore <workspace> 恢复文档 / Restore documents
--no-rename 不对文件名脱敏 / Skip filename sanitization
--auto-convert 自动转换 .doc/.xls,无需确认 / Auto-convert legacy formats

输出目录 / Output Directories

目录 / Directory 说明 / Description
_sanitized_output/ 脱敏后的文件 / Sanitized files
_restored_output/ 恢复后的文件 / Restored files
_sanitize_record.json 统一脱敏记录(映射累积)/ Unified record (accumulative mapping)
_sanitize_config.json 脱敏规则配置 / Sanitization rules config

脱敏记录结构 / Record Structure

{
  "version": 2,
  "created_at": "2026-04-08 16:02:07",
  "last_updated": "2026-04-08 16:02:07",
  "mapping": {
    "黑水": "白云",
    "[RED_手机号_1]": "13828417396"
  },
  "filename_mapping": {
    "黑水物流文档.docx": "白云物流文档.docx"
  },
  "runs": [
    {"timestamp": "...", "files_processed": ["..."]}
  ]
}

核心原理 / Core Principle:只要 mapping(脱敏值→原始值)完整,无论文档经过多少修改,都可以用它来反向替换恢复。

As long as the mapping (sanitized value → original value) is complete, documents can be restored regardless of modifications.

示例 / Examples

示例 1:基本脱敏流程 / Example 1: Basic Sanitization

# 1. 创建配置文件 _sanitize_config.json
# 2. 执行脱敏
python sanitize.py sanitize ./my-docs

# 输出:
# [RENAME] 白云物流文档.docx → 黑水物流文档.docx
# [1/1] 白云物流文档.docx [OK]
# 脱敏输出目录: ./my-docs/_sanitized_output

示例 2:脱敏后恢复 / Example 2: Sanitize then Restore

# 脱敏
python sanitize.py sanitize ./my-docs

# 恢复(可多次执行,对修改后的文档也有效)
python sanitize.py restore ./my-docs

# 输出:
# [RENAME] 黑水物流文档.docx → 白云物流文档.docx
# [OK] 无残留占位符

示例 3:处理包含旧格式文件的目录 / Example 3: Directory with Legacy Files

# 自动转换 .doc/.xls 并脱敏
python sanitize.py sanitize ./my-docs --auto-convert

技术要点 / Technical Notes

  1. Word run 拆分 / Run Splitting: Word 会将文本拆分到多个 <w:r> 元素中,脚本先合并所有 w:t 文本再替换再写回,确保跨 run 的关键字也能正确匹配

    Word splits text across multiple <w:r> elements. The script merges all w:t text first, applies replacements, then writes back — ensuring cross-run keywords are correctly matched.

  2. 反向替换顺序 / Reverse Order: 恢复时按 key 长度降序替换,避免短 key 误匹配长 key 的子串

    Restoration sorts keys by length descending to prevent short keys from partially matching longer keys.

  3. 脱敏范围 / Scope: 仅支持 .docx/.xlsx 格式。.doc/.xls 旧格式需先转换(可自动完成)

    Only .docx/.xlsx are supported. Legacy .doc/.xls must be converted first (auto-conversion available).

  4. 原始文件安全 / Original Safety: 原始文件不会被修改,所有操作在输出目录中进行

    Original files are never modified — all operations happen in output directories.

错误处理 / Error Handling

错误 / Error 解决方法 / Solution
"未找到配置文件" / Config not found 在工作目录创建 _sanitize_config.json
"python-docx import error" 运行 / Run: pip install python-docx
"openpyxl import error" 运行 / Run: pip install openpyxl
".doc 转换失败" / .doc conversion failed 确保 Windows + Word 已安装 / Ensure Word is installed
"残留占位符" / Residual placeholders 检查脱敏记录是否完整 / Check record completeness

推荐文章

更多
PHP时间戳与日期字符串相互转换方法详解

本文介绍PHP中时间数值(Unix时间戳)与日期字符串之间的相互转换方法。通过strtotime和date函数,实现将当前时间、指定日期转换为时间戳,以及将时间戳格式化为可读的日期时间字符串,并提供相关在线计算工具推荐。

2025.11.30 1,034
PHP日期转时间戳时区偏差怎么解决

在 PHP 中将日期字符串转换为时间戳时,若未显式指定时区,默认使用脚本配置的 date.timezone 或系统时区。当服务器时区与业务逻辑预期(如 UTC )不一致时,会产生偏差。解决核心是确保输入字符串携带明确时区信息,或在转换前统一设置时区上下文。

2026.10.24 1,031
PHP跨域配置:怎么安全地允许指定域名而非使用通配符

在 PHP 中安全地允许指定域名跨域,核心是解析请求头中的 Origin ,验证其是否在白名单内,然后动态设置 Access-Control-Allow-Origin 响应头。不要使用通配符 * ,也不要硬编码单一域名。

2025.03.29 1,018
JavaScript中利用replace结合正则实现replaceAll效果

JavaScript原生仅支持replace()进行单次或全局替换,缺乏类似Java的replaceAll()便捷方法。本文总结四种在JS中实现全量替换的方案:使用带g标志的正则、带gm标志的正则、扩展String原型以及split后join。通过对比代码示例,展示各方法的实现细节与适用场景,帮助开发者根据需求选择最优解。

2025.04.29 1,017
OpenAI的Jalapeno芯片未落地,教你分辨300兆瓦算力消息

OpenAI计划在SB Energy租赁的数据中心部署约300兆瓦自研Jalapeno推理芯片,但目前双方仍处于洽谈阶段,交易细节与上线时间表均未公布。本文带你按报道原文核实项目所处阶段,厘清芯片发布、电力功率与实际推理算力的边界,并将融资意向与签约、开工、投运节点分开追踪,避免误将规划容量计入现有算力。

2026.12.02 1,013
PHP实现基础文件上传:HTML表单与$_FILES处理流程

在 HTML 表单中设置 enctype="multipart/form-data" ,并在 PHP 后端通过 $_FILES 数组接收文件,调用 move_uploaded_file() 将临时文件移动到目标目录即可完成上传。

2026.09.07 1,013
Skill 文件
  • SKILL.md
    • document-sanitizer
    • 技能说明 / Skill Description
      • 概述 / Overview
      • 核心特性 / Key Features
    • 前置依赖 / Prerequisites
      • Python 包依赖 / Python Dependencies
      • 可选依赖(旧格式转换)/ Optional (Legacy Format Conversion)
    • 使用方法 / Usage
      • 1. 准备配置文件 / Prepare Config File
      • 2. 执行脱敏 / Run Sanitization
      • 3. 恢复文档 / Restore Documents
    • 命令行参数 / CLI Arguments
    • 输出目录 / Output Directories
    • 脱敏记录结构 / Record Structure
    • 示例 / Examples
      • 示例 1:基本脱敏流程 / Example 1: Basic Sanitization
      • 示例 2:脱敏后恢复 / Example 2: Sanitize then Restore
      • 示例 3:处理包含旧格式文件的目录 / Example 3: Directory with Legacy Files
    • 技术要点 / Technical Notes
    • 错误处理 / Error Handling
  • 项目或源文件

相关 Skill

更多
service-qa-coach service-qa-coach Review support quality for chat, email, phone, social DM, or marketplace service teams. Use when a team needs a QA scorecard, failure-mode analysis, coaching plan, calibration checklist, or service-improvement brief without live ticketing, CRM, or contact-center system access.
高中数学作业批改助手 高中数学作业批改助手 >图片识别 直接读取学生手写作答照片,识别公式、符号、步骤 🧮 自动推导 根据题目自动生成参考答案和评分细则,无需教师预设 📊 逐步给分 每个关键步骤独立评分,体现过程分 📝 结构化报告 输出含识别内容、参考答案、逐步评分、改进建议的完整报告 🗂 批量批改 支持一次性批改多道题,最终汇总得分表
Vibration Analysis Vibration Analysis 分析来自CMS趋势、RMS/峰值、频谱及SCADA报警的风机传动链(主轴、齿轮箱、发电机)振动数据。
Traditional Bone Tool Making Basics Traditional Bone Tool Making Basics Use when you need custom, durable, repairable tools like awls, needles, scrapers, harpoons, or fishing hooks made from local animal bones or antlers and comm...
音频混音工具 音频混音工具 名称:Sound Mixing Tool(标识 sound-mixing-tool) 短简介(≤60字):音乐/音效响度精确对齐 + 融合混音工作流:用 ffmpeg 两段式 loudnorm 把多组素材 LUFS 偏差压到 <0.10,再最小干预融合、导出可进引擎的独立分轨。 标签/关键词:音频混音 响度匹配 LUFS ffmpeg 游戏音频 音效设计 Wwise FMOD 分轨导出 后期处理 触发词:混音 · 响度匹配 · LUFS 对齐 · MIX 版本 · 音乐音效分轨 · 融合 · 不打架 · 不能盖住 适用场景 :游戏音频集成、影视短视频配乐、播客后期