Optimized DOCX: Zero Format Loss in Word (.docx) Processing

Preface

Modifying Word documents within an Agent typically involves regenerating the entire file using a library or simply modifying plain text. However, styles, headers/footers, sections, numbering, revision history (track changes), and images on complex templates are easily lost or misaligned. Furthermore, handling layouts for Chinese official documents (GB/T 9704) relies solely on “Save As” or simple text replacement, which is often insufficient for stability.

Below is an introduction to the docx optimized version (coordinate @user_c66b8218/docx007-skill) on SkillHub. It treats .docx files as Open XML within a ZIP package, following a workflow of Unpack → Modify XML in-place → Repack, supported by validation and rendering tools. Its goal is to preserve the original formatting as much as possible during creation, editing, form filling, reformatting, and format conversion.

What is this

docx optimized version is a community-published (community) Agent Skill categorized under office-efficiency. It is maintained by SkillHub user user_c66b8218, with the current store version at 1.0.0. The Skill package includes a runnable Python toolchain (lxml, python-docx) and relies on local LibreOffice and poppler-utils. It does not require an API Key (requires_api_key=false).

In one sentence: It processes Microsoft Word .docx files within an Agent session using a READ → ACT → VERIFY workflow—reading structure, modifying XML within an unpacked directory or creating new documents, and then validating and rendering to PDF/PNG for visual inspection. It provides specific instructions and script support for Chinese/CJK and GB/T 9704 official document layouts.

Core Features

The following capabilities are derived from the SKILL.md and references/ documentation within the Skill package and can be selected based on task routing:

  1. Creation (Pipeline A): When no existing .docx is available, use python-docx to create a new document (see references/create.md).
  2. Filling & Editing (Pipeline B): Perform run-aware find-and-replace, placeholder filling, and insertion of paragraphs/headings on existing .docx files after unpacking (see references/edit.md).
  3. Applying Styles & Templates (Pipeline C): Reformat or apply templates, including two strategies: Overlay and Base-Replace (see references/format.md).
  4. Reading & Analysis: docx_reader.py provides structure overviews, --outline for heading trees, --preview, and --diff-against comparisons.
  5. Format Preservation: Unpacking, editing, and repacking aims to preserve styles, themes, headers/footers, multi-section layouts, numbering, revisions (track changes), fields, and embedded images. The Skill explicitly emphasizes prioritizing editing over full-package regeneration.
  6. Validation & Visualization: docx_validate.py acts as a gatekeeper for element order and other constraints; docx_render.py outputs PDFs and paginated PNGs via LibreOffice for easy verification of layouts and CJK fonts.
  7. Format Conversion: libreoffice_convert.py supports converting .doc/.odt/.rtf to .docx or PDF.
  8. Chinese & Official Documents: Support for UTF-8, w:rFonts East Asian fonts, and font size tables; for official documents, it references GB/T 9704 (e.g., FangSong_GB2312 size 3 for body text, SimHei for titles, etc., see references/cjk-guide.md).

The store-side SkillHub rating is 100000, with 1342 downloads (statistics based on SkillHub page scraping).

Installation & Activation

Environment Dependencies

Before running scripts in the Skill directory, the dependencies declared in the package must be met:

  • Python: python-docx, lxml, Pillow
  • System: libreoffice (soffice), poppler-utils (pdftoppm)
  • Fonts: It is recommended to install Noto Sans CJK / Noto Serif CJK for better CJK rendering.

After installation, you can run an environment self-check in the Skill root directory:

python3 scripts/docx_doctor.py

The output should be READY.

Installing via SkillHub CLI

If the CLI is not installed, follow the official SkillHub instructions to install it:

curl -fsSL https://skillhub-1388575217.cos.ap-guangzhou.myqcloud.com/install/install.sh | bash

Install the Skill to the current Agent’s skills directory (must use --dir, otherwise the default ./skills/ might not be loaded by the Agent). For example, in Cursor:

skillhub install docx007-skill --namespace user_c66b8218 --dir ~/.cursor/skills/

Common paths for other clients (from SkillHub installation docs): Claude Code uses ~/.claude/skills/; Codex uses ~/.codex/skills/ or .agents/skills/ in the project. Typically, a restart or reload of the Agent is required to recognize the new Skill.

Installing via Natural Language

The prompt provided on the SkillHub page is: According to https://skillhub.cn/install/skillhub.md, install @user_c66b8218/docx007-skill.

Typical Usage

The typical flow aligns with references/quickstart.md inside the Skill: First READ, then ACT, and finally VERIFY. The following commands are executed in the Skill package root directory (the path containing scripts/).

1. Read original structure

python3 scripts/docx_reader.py input.docx --outline

2. Unpack, Replace, Repack

python3 scripts/docx_unpack.py input.docx /tmp/wd
python3 scripts/docx_replace_text.py /tmp/wd --find OLD --replace NEW
python3 scripts/docx_pack.py /tmp/wd output.docx

3. Validate, Compare, Render for visual check

python3 scripts/docx_validate.py output.docx --report
python3 scripts/docx_reader.py output.docx --diff-against input.docx
python3 scripts/docx_render.py output.docx --out /tmp/rev

Template placeholder filling example (in the same unpacked directory):

python3 scripts/docx_fill_placeholders.py /tmp/wd --data '{"name":"Zhang San"}'

Legacy .doc files can be converted first:

python3 scripts/libreoffice_convert.py old.doc --to docx --outdir .

In Agent conversations, trigger words include Word, docx, document, official document, template application, report, mail merge, etc. (see Skill metadata triggers). The Agent should follow Skill requirements: any deliverable must pass docx_validate.py and --diff-against, and complete the PNG visual inspection via docx_render.py.

Applicable Scenarios & Notes

Who is it for

  • Scenarios where you need to modify contracts, reports, papers, or official documents within an Agent while preserving the original template layout.
  • When performing batch placeholder filling, run-aware replacement, or inserting headings with outline levels, and you are concerned about losing formatting when python-docx rewrites the entire document alone.
  • Developers handling Chinese/CJK or GB/T 9704 official document layouts who are willing to edit XML according to rules such as Open XML element order.

Limitations & Notes

  • Depends on local LibreOffice and poppler; the rendering pipeline may require additional configuration in headless environments (see references/troubleshooting.md for troubleshooting).
  • Open XML has strict constraints on element order, revision marks (w:ins / w:del), and units (half-points, dxa, EMU). Violations will cause docx_validate.py to fail; modifications must be made in accordance with documents like references/openxml-element-order.md.
  • The SkillHub store version is 1.0.0; the SKILL.md frontmatter in the package contains an internal version field, which should be based on the actual installed package.
  • This Skill is a tool and workflow guide; it does not replace compliance approval for confidential documents. Please operate sensitive files in an isolated environment.

Conclusion

docx optimized version treats Word as an auditable XML package and uses a script chain to fix the steps of Read → Modify → Verify → View. It is suitable for reproducible, low-format-loss .docx processing within an Agent. Official entry and installation coordinates:

  • SkillHub Page: https://www.skillhub.cn/skills/user_c66b8218/docx007-skill
  • API Coordinates: https://api.skillhub.cn/user_c66b8218/docx007-skill
  • Installation: skillhub install docx007-skill --namespace user_c66b8218 --dir <your Agent skills directory>
羽毛球分组比赛记分
小程序二维码

欢迎使用《羽毛球分组比赛记分》微信小程序

Xiaoye