SkillsGrove
SkillsGrove/Bürodokumente/openJiuwen-ai/jiuwenswarm/docx-pro

docx-pro

Installation

$npx skills add https://github.com/openJiuwen-ai/jiuwenswarm --skill docx-pro
$git clone --depth 1 https://github.com/openJiuwen-ai/jiuwenswarm.git /tmp/jiuwenswarm && mkdir -p ~/.claude/skills && cp -r /tmp/jiuwenswarm/jiuwenswarm/resources/agent/workspace/skills/workswarm-builtin/references/capabilities/docx-pro ~/.claude/skills/docx-pro
Bitte installiere diesen Agent Skill für mich: https://github.com/openJiuwen-ai/jiuwenswarm/tree/bbee2b7de4c5b9d64793030da112b584fd5c460b/jiuwenswarm/resources/agent/workspace/skills/workswarm-builtin/references/capabilities/docx-pro – lies zuerst seine SKILL.md und seine Skripte und installiere ihn erst dann in meinen Skills-Ordner, wenn du bestätigt hast, dass er sicher ist.

Zusammenfassung

Originalbeschreibung · Apache-2.0Professional Word (.docx) document generation, faithful text replacement, Markdown round-trip conversion, document inspection, TOC insertion and watermarking. Use when the user asks to create rich-formatted Word documents (cover page, TOC, multi-level headings, styled tables with merged cells, images, code blocks, quotes, headers/footers, page numbers, watermarks), replace or batch-replace text in existing docx files while preserving every other byte and all formatting (handles text split across runs, tables, headers/footers), convert Markdown to docx or docx to Markdown, inspect document structure, insert TOC fields, or add text watermarks to existing docx files. Triggers on docx-pro, 生成 Word 文档, 生成报告文档, 富格式文档, Word 替换, Word 文本替换, Word 保真替换, 批量替换, 合同替换, 替换甲方, Markdown 转 Word, Word 转 Markdown, md 转 docx, docx 转 md, 插入目录, 添加水印, 文档结构分析, 专业排版.
Die folgende SKILL.md ist aus openJiuwen-ai/jiuwenswarm (Commit bbee2b7) unter der Lizenz Apache-2.0 wiedergegeben. Der Text ist unverändert; lediglich die Metadaten im Dateikopf sind weggelassen, Bilder werden als Links angezeigt und sehr lange Dateien werden gekürzt. Copyright owner or entity authorized by. Lizenz: LICENSE. Es wird nur die Anleitungsdatei angezeigt; etwaige Skripte finden Sie im Original-Repository.

docx-pro:专业 Word 文档操作技能

基于 python-docx 的 docx 进阶工具集,提供 7 个子命令,覆盖「生成 → 转换 → 编辑 → 检查 → 后处理」完整链路。

STOP:执行前检查

  1. 依赖:运行 python scripts/setup_check.py,确认 python-docx 已安装(缺则 pip install python-docx)。
  2. 路径:确认输入文件存在、输出路径不会覆盖任何输入文件。
  3. 控制台编码:本技能脚本已内置 GBK 控制台兜底(errors=replace),直接用 python scripts/docx_pro.py ... 即可;若仍见乱码,是显示问题,不影响文件内容。
  4. 替换前先预览:用 replace --dry-run 确认命中次数与预期一致,再实际替换。

子命令速查

脚本统一入口:scripts/docx_pro.py(以下 PY 代表 python)。

1. create — JSON 大纲 → 富格式 docx

PY scripts/docx_pro.py create -j outline.json -o 报告.docx --theme business

可选参数:--header "页眉文字"、--footer center|X/Y、--toc true、--cover true、--watermark "草稿"。

三套内置主题:

  • business 商务蓝(默认):深蓝封面标题、深蓝表头白字、浅蓝斑马纹
  • academic 学术黑白:衬线正文、黑底表头、Times New Roman
  • minimal 极简:无底色表头(粗下边框)、大量留白

支持的块类型(outline.sections 内元素):

type 关键字段
heading level(1-4), text
para text(支持 粗体 代码 删除 链接 行内语法), indent, align
bullets items: ["文本" 或 {text, bold_prefix, indent}], number(编号列表)
table header[], rows[](单元格可为 {t, cs, rs} 支持合并), widths[], zebra, align
kv items: {键: 值} —— 自动渲染两列规格表
image path(相对 JSON 所在目录), width_cm, caption
code text(多行代码,等宽字体+灰底)
quote text(左边框引用块)
divider / pagebreak 无字段

大纲顶层字段:title, subtitle, meta[], cover, toc, header, footer(center|"X/Y"), theme, watermark。

2. from-md — Markdown → docx

PY scripts/docx_pro.py from-md 输入.md -o 输出.docx --theme academic --toc --cover

支持:YAML frontmatter(title/subtitle/theme/toc/cover/header/footer)、标题层级、行内格式(粗体/斜体/行内代码/删除线/链接)、无序/有序/嵌套列表、表格、围栏代码块、引用块、图片、分隔线。列表项 **加粗前缀** 正文 自动渲染为彩色加粗前缀样式。

3. to-md — docx → Markdown

PY scripts/docx_pro.py to-md 输入.docx -o 输出.md --images

--images 时导出文档内图片到 输出名_images/ 目录。注意:定位是内容提取转换而非像素级还原——样式细节(颜色/主题/水印)会丢失,正文、表格、列表、代码块、引用、图片可完整还原。

4. replace — docx 保真文本替换(合同/公文等格式敏感文档首选)

PY scripts/docx_pro.py replace 输入.docx -o 输出.docx -f "甲方:A 公司" -t "甲方:B公司"
PY scripts/docx_pro.py replace 输入.docx -o 输出.docx --map pairs.json --dry-run
  • 跨 run 定位:Word 常因拼写检查、协同编辑把一段连续文字拆成多个 run(w:r/w:t 节点),朴素字符串替换会大量漏换。本命令把同一段落内相邻 run 的文本拼接后定位目标串——替换文本写入首个命中节点,其余被覆盖节点文本清空,run 结构与全部格式属性零改动。
  • 字节级保真:zip 包内仅含目标文本的 XML 部件被修改,其余条目(样式表、主题、编号、字体表、图片……)按原始字节复制;命中部件也只改被替换节点的文本内容,保留原 XML 声明、属性顺序、命名空间前缀。
  • 覆盖范围:默认 --scope all = 正文(含表格单元格、文本框、超链接内文本)+ 页眉 + 页脚 + 脚注/尾注/批注;--scope body 仅正文。
  • 批量替换:--map pairs.json([["旧","新"], ...] 或 {"旧": "新"},也支持 [{"find":..,"to":..}]),按顺序链式执行;或多次 -f/-t 成对使用。
  • 预览:--dry-run 只统计各部件命中次数,不写出文件。
  • 自动验证:替换后逐 zip 条目字节比对、报告跨 run 命中数、残留计数,并用 python-docx 复检段落数/表格数与可打开性。

安全边界:匹配不跨段落、不跨表格单元格(相邻文本节点之间出现段落结束、制表符、换行、域代码即视为不连续,这正是"不该替换的不替换"的保证);域代码(w:instrText)与修订删除文本(w:delText)不参与匹配。跨格式 run 替换时,替换文本沿用首个命中 run 的格式。

5. inspect — 查看文档结构

PY scripts/docx_pro.py inspect 输入.docx [--json]

输出页面尺寸、边距、非空段落数、表格数与形状、内联图片数、完整标题树。--json 供程序化消费。

6. toc — 为已有 docx 插入目录域

PY scripts/docx_pro.py toc 输入.docx -o 带目录.docx --levels 1-3

插入 TOC 域到文档开头;在 Word 中打开后需按 F9(或右键→更新域)生成实际目录(要求文档标题使用 Heading 样式,本技能 create 生成的文档天然满足)。

7. watermark — 为已有 docx 添加文字水印

PY scripts/docx_pro.py watermark 输入.docx -o 加水印.docx -t "DRAFT" --color C0C0C0 --opacity 0.45

斜向 315° 文字水印,全部页面生效(页眉 VML 实现,兼容 Word/WPS)。

CHECKPOINT:完成后的验证

  1. 用 inspect 检查生成文档:标题树、表格数、图片数与预期一致。
  2. create 含表格时,确认表格首行是表头内容(数据未被写入表头位置)。
  3. toc/watermark/replace 后处理命令的输出路径与输入不同(不覆盖原文件)。
  4. replace 后确认:命中次数与预期一致(先 --dry-run 后实跑)、"其余 zip 条目字节级一致: True"、段落数/表格数不变、残留计数为 0(若新文本本身包含旧文本,残留>0 属正常)。
  5. 中文字符在 Windows 控制台显示乱码时,检查文件本身(read_file / inspect)确认内容正确。

大纲 JSON 完整示例

{
  "title": "产品评测报告",
  "subtitle": "2026 年度智能硬件横评",
  "meta": ["评测机构:极客实验室", "发布日期:2026-09-04"],
  "cover": true, "toc": true,
  "header": "极客实验室 · 产品评测", "footer": "X/Y",
  "theme": "business", "watermark": "内部资料",
  "sections": [
    {"type": "heading", "level": 1, "text": "一、评测概述"},
    {"type": "para", "text": "本报告覆盖 **12 款** 设备,原始数据见 `data/raw.csv`。"},
    {"type": "bullets", "items": [
      {"text": "共 12 款设备", "bold_prefix": "设备数量:"},
      {"text": "子项目 A", "indent": 1}
    ]},
    {"type": "table", "header": ["设备", "得分"], "zebra": true,
     "rows": [["设备 A", "94.5"], [{"t": "合并单元格", "cs": 2}]]},
    {"type": "kv", "items": {"总测试项": "348"}},
    {"type": "image", "path": "chart.png", "width_cm": 12, "caption": "图 1 示例"},
    {"type": "code", "text": "print('hello')"},
    {"type": "quote", "text": "结论仅供参考。"},
    {"type": "divider"}, {"type": "pagebreak"}
  ]
}

批量替换映射示例(pairs.json)

[
  ["甲方:A 公司", "甲方:B公司"],
  ["乙方:C 公司", "乙方:D公司"],
  {"find": "2025 年度", "to": "2026 年度"}
]

边界与已知限制

  • to-md 是内容级转换:主题配色、水印、封面版式等样式信息不参与回转。
  • TOC 域需在 Word/WPS 中手动更新(F9)后才显示实际目录,这是域机制决定的行为。
  • 水印为整页文字水印(斜向 315°);不支持图片水印。
  • create 的表格支持横向(cs)/纵向(rs)合并,但 to-md 回转时合并单元格会退化为普通单元格。
  • replace 不跨段落/跨单元格匹配;不处理域代码(w:instrText)与修订删除文本(w:delText);假定正文命名空间前缀为 w:(Word 标准写法);--dry-run 的统计基于原文(前一对替换产生的新文本不计入后一对的预览)。
  • 依赖:python-docx(必需,replace 的复检环节也用到);Pillow 仅当需要处理特殊格式图片时可能用到,非必需。

Directory layout

docx-pro/
├── SKILL.md
└── scripts/
    ├── docx_pro.py     # 统一 CLI 入口(7 个子命令)
    ├── docx_replace.py # 保真替换引擎(跨 run 定位 / 字节级保真 / 自动验证)
    ├── renderer.py     # 主题系统 + 块渲染引擎
    ├── md_parser.py    # Markdown -> 大纲 解析器
    ├── md_export.py    # docx -> Markdown 导出器
    └── setup_check.py  # 依赖自检
Vollständigen Lizenztext ansehen (Apache-2.0)
                                 Apache License
                           Version 2.0, January 2004
                        http://www.apache.org/licenses/

   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION

   1. Definitions.

      "License" shall mean the terms and conditions for use, reproduction,
      and distribution as defined by Sections 1 through 9 of this document.

      "Licensor" shall mean the copyright owner or entity authorized by
      the copyright owner that is granting the License.

      "Legal Entity" shall mean the union of the acting entity and all
      other entities that control, are controlled by, or are under common
      control with that entity. For the purposes of this definition,
      "control" means (i) the power, direct or indirect, to cause the
      direction or management of such entity, whether by contract or
      otherwise, or (ii) ownership of fifty percent (50%) or more of the
      outstanding shares, or (iii) beneficial ownership of such entity.

      "You" (or "Your") shall mean an individual or Legal Entity
      exercising permissions granted by this License.

      "Source" form shall mean the preferred form for making modifications,
      including but not limited to software source code, documentation
      source, and configuration files.

      "Object" form shall mean any form resulting from mechanical
      transformation or translation of a Source form, including but
      not limited to compiled object code, generated documentation,
      and conversions to other media types.

      "Work" shall mean the work of authorship, whether in Source or
      Object form, made available under the License, as indicated by a
      copyright notice that is included in or attached to the work
      (an example is provided in the Appendix below).

      "Derivative Works" shall mean any work, whether in Source or Object
      form, that is based on (or derived from) the Work and for which the
      editorial revisions, annotations, elaborations, or other modifications
      represent, as a whole, an original work of authorship. For the purposes
      of this License, Derivative Works shall not include works that remain
      separable from, or merely link (or bind by name) to the interfaces of,
      the Work and Derivative Works thereof.

      "Contribution" shall mean any work of authorship, including
      the original version of the Work and any modifications or additions
      to that Work or Derivative Works thereof, that is intentionally
      submitted to Licensor for inclusion in the Work by the copyright owner
      or by an individual or Legal Entity authorized to submit on behalf of
      the copyright owner. For the purposes of this definition, "submitted"
      means any form of electronic, verbal, or written communication sent
      to the Licensor or its representatives, including but not limited to
      communication on electronic mailing lists, source code control systems,
      and issue tracking systems that are managed by, or on behalf of, the
      Licensor for the purpose of discussing and improving the Work, but
      excluding communication that is conspicuously marked or otherwise
      designated in writing by the copyright owner as "Not a Contribution."

      "Contributor" shall mean Licensor and any individual or Legal Entity
      on behalf of whom a Contribution has been received by Licensor and
      subsequently incorporated within the Work.

   2. Grant of Copyright License. Subject to the terms and conditions of
      this License, each Contributor hereby grants to You a perpetual,
      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
      copyright license to reproduce, prepare Derivative Works of,
      publicly display, publicly perform, sublicense, and distribute the
      Work and such Derivative Works in Source or Object form.

   3. Grant of Patent License. Subject to the terms and conditions of
      this License, each Contributor hereby grants to You a perpetual,
      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
      (except as stated in this section) patent license to make, have made,
      use, offer to sell, sell, import, and otherwise transfer the Work,
      where such license applies only to those patent claims licensable
      by such Contributor that are necessarily infringed by their
      Contribution(s) alone or by combination of their Contribution(s)
      with the Work to which such Contribution(s) was submitted. If You
      institute patent litigation against any entity (including a
      cross-claim or counterclaim in a lawsuit) alleging that the Work
      or a Contribution incorporated within the Work constitutes direct
      or contributory patent infringement, then any patent licenses
      granted to You under this License for that Work shall terminate
      as of the date such litigation is filed.

   4. Redistribution. You may reproduce and distribute copies of the
      Work or Derivative Works thereof in any medium, with or without
      modifications, and in Source or Object form, provided that You
      meet the following conditions:

      (a) You must give any other recipients of the Work or
          Derivative Works a copy of this License; and

      (b) You must cause any modified files to carry prominent notices
          stating that You changed the files; and

      (c) You must retain, in the Source form of any Derivative Works
          that You distribute, all copyright, patent, trademark, and
          attribution notices from the Source form of the Work,
          excluding those notices that do not pertain to any part of
          the Derivative Works; and

      (d) If the Work includes a "NOTICE" text file as part of its
          distribution, then any Derivative Works that You distribute must
          include a readable copy of the attribution notices contained
          within such NOTICE file, excluding those notices that do not
          pertain to any part of the Derivative Works, in at least one
          of the following places: within a NOTICE text file distributed
          as part of the Derivative Works; within the Source form or
          documentation, if provided along with the Derivative Works; or,
          within a display generated by the Derivative Works, if and
          wherever such third-party notices normally appear. The contents
          of the NOTICE file are for informational purposes only and
          do not modify the License. You may add Your own attribution
          notices within Derivative Works that You distribute, alongside
          or as an addendum to the NOTICE text from the Work, provided
          that such additional attribution notices cannot be construed
          as modifying the License.

      You may add Your own copyright statement to Your modifications and
      may provide additional or different license terms and conditions
      for use, reproduction, or distribution of Your modifications, or
      for any such Derivative Works as a whole, provided Your use,
      reproduction, and distribution of the Work otherwise complies with
      the conditions stated in this License.

   5. Submission of Contributions. Unless You explicitly state otherwise,
      any Contribution intentionally submitted for inclusion in the Work
      by You to the Licensor shall be under the terms and conditions of
      this License, without any additional terms or conditions.
      Notwithstanding the above, nothing herein shall supersede or modify
      the terms of any separate license agreement you may have executed
      with Licensor regarding such Contributions.

   6. Trademarks. This License does not grant permission to use the trade
      names, trademarks, service marks, or product names of the Licensor,
      except as required for reasonable and customary use in describing the
      origin of the Work and reproducing the content of the NOTICE file.

   7. Disclaimer of Warranty. Unless required by applicable law or
      agreed to in writing, Licensor provides the Work (and each
      Contributor provides its Contributions) on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
      implied, including, without limitation, any warranties or conditions
      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
      PARTICULAR PURPOSE. You are solely responsible for determining the
      appropriateness of using or redistributing the Work and assume any
      risks associated with Your exercise of permissions under this License.

   8. Limitation of Liability. In no event and under no legal theory,
      whether in tort (including negligence), contract, or otherwise,
      unless required by applicable law (such as deliberate and grossly
      negligent acts) or agreed to in writing, shall any Contributor be
      liable to You for damages, including any direct, indirect, special,
      incidental, or consequential damages of any character arising as a
      result of this License or out of the use or inability to use the
      Work (including but not limited to damages for loss of goodwill,
      work stoppage, computer failure or malfunction, or any and all
      other commercial damages or losses), even if such Contributor
      has been advised of the possibility of such damages.

   9. Accepting Warranty or Additional Liability. While redistributing
      the Work or Derivative Works thereof, You may choose to offer,
      and charge a fee for, acceptance of support, warranty, indemnity,
      or other liability obligations and/or rights consistent with this
      License. However, in accepting such obligations, You may act only
      on Your own behalf and on Your sole responsibility, not on behalf
      of any other Contributor, and only if You agree to indemnify,
      defend, and hold each Contributor harmless for any liability
      incurred by, or claims asserted against, such Contributor by reason
      of your accepting any such warranty or additional liability.

   END OF TERMS AND CONDITIONS

   APPENDIX: How to apply the Apache License to your work.

      To apply the Apache License to your work, attach the following
      boilerplate notice, with the fields enclosed by brackets "[]"
      replaced with your own identifying information. (Don't include
      the brackets!)  The text should be enclosed in the appropriate
      comment syntax for the file format. We also recommend that a
      file or class name and description of purpose be included on the
      same "printed page" as the copyright notice for easier
      identification within third-party archives.

   Copyright [yyyy] [name of copyright owner]

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.