skills-docx

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

DOCX creation, editing, and analysis

DOCX 创建、编辑与分析

Overview

概述

A user may ask you to create, edit, or analyze the contents of a .docx file. A .docx file is essentially a ZIP archive containing XML files and other resources that you can read or edit. You have different tools and workflows available for different tasks.
用户可能会要求你创建、编辑或分析 .docx 文件的内容。.docx 文件本质上是一个包含XML文件和其他资源的ZIP压缩包,你可以读取或编辑这些内容。针对不同任务,你可以使用不同的工具和工作流。

Workflow Decision Tree

工作流决策树

Reading/Analyzing Content

读取/分析内容

Use "Text extraction" or "Raw XML access" sections below
使用下方的「文本提取」或「原始XML访问」模块

Creating New Document

创建新文档

Use "Creating a new Word document" workflow
使用「创建新Word文档」工作流

Editing Existing Document

编辑现有文档

  • Your own document + simple changes Use "Basic OOXML editing" workflow
  • Someone else's document Use "Redlining workflow" (recommended default)
  • Legal, academic, business, or government docs Use "Redlining workflow" (required)
  • 你自己创建的文档 + 简单修改 使用「基础OOXML编辑」工作流
  • 其他人创建的文档 使用**「修订标注工作流」**(推荐默认选项)
  • 法律、学术、商业或政府文档 必须使用**「修订标注工作流」**

Reading and analyzing content

读取与分析内容

Text extraction

文本提取

If you just need to read the text contents of a document, you should convert the document to markdown using pandoc. Pandoc provides excellent support for preserving document structure and can show tracked changes:
bash
undefined
如果你只需要读取文档的文本内容,应该使用pandoc将文档转换为markdown格式。Pandoc对保留文档结构有极佳的支持,还可以显示修订记录:
bash
undefined

Convert document to markdown with tracked changes

将文档转换为带修订记录的markdown格式

pandoc --track-changes=all path-to-file.docx -o output.md
pandoc --track-changes=all path-to-file.docx -o output.md

Options: --track-changes=accept/reject/all

可选参数:--track-changes=accept/reject/all

undefined
undefined

Raw XML access

原始XML访问

You need raw XML access for: comments, complex formatting, document structure, embedded media, and metadata. For any of these features, you'll need to unpack a document and read its raw XML contents.
以下场景需要你访问原始XML:评论、复杂格式、文档结构、嵌入媒体、元数据。要使用这些功能,你需要解压文档并读取其原始XML内容。

Unpacking a file

解压文件

python ooxml/scripts/unpack.py <office_file> <output_directory>
python ooxml/scripts/unpack.py <office_file> <output_directory>

Key file structures

核心文件结构

  • word/document.xml
    - Main document contents
  • word/comments.xml
    - Comments referenced in document.xml
  • word/media/
    - Embedded images and media files
  • Tracked changes use
    <w:ins>
    (insertions) and
    <w:del>
    (deletions) tags
  • word/document.xml
    - 主文档内容
  • word/comments.xml
    - document.xml中引用的评论内容
  • word/media/
    - 嵌入的图片和媒体文件
  • 修订记录使用
    <w:ins>
    (插入内容)和
    <w:del>
    (删除内容)标签

Creating a new Word document

创建新Word文档

When creating a new Word document from scratch, use docx-js, which allows you to create Word documents using JavaScript/TypeScript.
从零创建新Word文档时,请使用docx-js,它支持你使用JavaScript/TypeScript创建Word文档。

Workflow

工作流

  1. MANDATORY - READ ENTIRE FILE: Read
    docx-js.md
    (~500 lines) completely from start to finish. NEVER set any range limits when reading this file. Read the full file content for detailed syntax, critical formatting rules, and best practices before proceeding with document creation.
  2. Create a JavaScript/TypeScript file using Document, Paragraph, TextRun components (You can assume all dependencies are installed, but if not, refer to the dependencies section below)
  3. Export as .docx using Packer.toBuffer()
  1. 强制要求 - 完整阅读文件:从头到尾完整阅读
    docx-js.md
    (约500行)。读取该文件时绝对不要设置任何范围限制。 先完整阅读文件内容,了解详细语法、关键格式规则和最佳实践,再开始创建文档。
  2. 使用Document、Paragraph、TextRun组件创建JavaScript/TypeScript文件(你可以假设所有依赖都已安装,若未安装可参考下方依赖部分)
  3. 使用Packer.toBuffer()导出为.docx格式

Editing an existing Word document

编辑现有Word文档

When editing an existing Word document, use the Document library (a Python library for OOXML manipulation). The library automatically handles infrastructure setup and provides methods for document manipulation. For complex scenarios, you can access the underlying DOM directly through the library.
编辑现有Word文档时,请使用Document库(用于OOXML操作的Python库)。该库会自动处理基础配置,提供文档操作的相关方法。复杂场景下,你可以通过该库直接访问底层DOM。

Workflow

工作流

  1. MANDATORY - READ ENTIRE FILE: Read
    ooxml.md
    (~600 lines) completely from start to finish. NEVER set any range limits when reading this file. Read the full file content for the Document library API and XML patterns for directly editing document files.
  2. Unpack the document:
    python ooxml/scripts/unpack.py <office_file> <output_directory>
  3. Create and run a Python script using the Document library (see "Document Library" section in ooxml.md)
  4. Pack the final document:
    python ooxml/scripts/pack.py <input_directory> <office_file>
The Document library provides both high-level methods for common operations and direct DOM access for complex scenarios.
  1. 强制要求 - 完整阅读文件:从头到尾完整阅读
    ooxml.md
    (约600行)。读取该文件时绝对不要设置任何范围限制。 完整阅读文件内容,了解Document库API以及直接编辑文档文件的XML模式。
  2. 解压文档:
    python ooxml/scripts/unpack.py <office_file> <output_directory>
  3. 使用Document库创建并运行Python脚本(参考ooxml.md中的「Document库」模块)
  4. 打包最终文档:
    python ooxml/scripts/pack.py <input_directory> <office_file>
Document库既提供了用于常见操作的高层方法,也支持复杂场景下的直接DOM访问。

Redlining workflow for document review

文档审阅的修订标注工作流

This workflow allows you to plan comprehensive tracked changes using markdown before implementing them in OOXML. CRITICAL: For complete tracked changes, you must implement ALL changes systematically.
Batching Strategy: Group related changes into batches of 3-10 changes. This makes debugging manageable while maintaining efficiency. Test each batch before moving to the next.
Principle: Minimal, Precise Edits When implementing tracked changes, only mark text that actually changes. Repeating unchanged text makes edits harder to review and appears unprofessional. Break replacements into: [unchanged text] + [deletion] + [insertion] + [unchanged text]. Preserve the original run's RSID for unchanged text by extracting the
<w:r>
element from the original and reusing it.
Example - Changing "30 days" to "60 days" in a sentence:
python
undefined
该工作流允许你先在markdown中规划完整的修订内容,再在OOXML中实现。重要提示:要生成完整的修订记录,你必须系统地实现所有变更。
分批策略:将相关的变更分为3-10个一组的批次。这既便于调试,也能保持效率。每完成一批次的修改后都要进行测试,再进行下一批次。
原则:最小化、精准编辑 实现修订记录时,仅标记实际发生变更的文本。重复未变更的文本会让编辑内容更难审阅,也显得不专业。将替换内容拆分为:[未变更文本] + [删除内容] + [插入内容] + [未变更文本]。对于未变更文本,从原文档中提取
<w:r>
元素复用,保留原有运行的RSID。
示例 - 将句子中的「30 days」修改为「60 days」:
python
undefined

BAD - Replaces entire sentence

错误示例 - 替换整个句子

'<w:del><w:r><w:delText>The term is 30 days.</w:delText></w:r></w:del><w:ins><w:r><w:t>The term is 60 days.</w:t></w:r></w:ins>'
'<w:del><w:r><w:delText>The term is 30 days.</w:delText></w:r></w:del><w:ins><w:r><w:t>The term is 60 days.</w:t></w:r></w:ins>'

GOOD - Only marks what changed, preserves original <w:r> for unchanged text

正确示例 - 仅标记变更部分,为未变更文本保留原有<w:r>

'<w:r w:rsidR="00AB12CD"><w:t>The term is </w:t></w:r><w:del><w:r><w:delText>30</w:delText></w:r></w:del><w:ins><w:r><w:t>60</w:t></w:r></w:ins><w:r w:rsidR="00AB12CD"><w:t> days.</w:t></w:r>'
undefined
'<w:r w:rsidR="00AB12CD"><w:t>The term is </w:t></w:r><w:del><w:r><w:delText>30</w:delText></w:r></w:del><w:ins><w:r><w:t>60</w:t></w:r></w:ins><w:r w:rsidR="00AB12CD"><w:t> days.</w:t></w:r>'
undefined

Tracked changes workflow

修订记录工作流

  1. Get markdown representation: Convert document to markdown with tracked changes preserved:
    bash
    pandoc --track-changes=all path-to-file.docx -o current.md
  2. Identify and group changes: Review the document and identify ALL changes needed, organizing them into logical batches:
    Location methods (for finding changes in XML):
    • Section/heading numbers (e.g., "Section 3.2", "Article IV")
    • Paragraph identifiers if numbered
    • Grep patterns with unique surrounding text
    • Document structure (e.g., "first paragraph", "signature block")
    • DO NOT use markdown line numbers - they don't map to XML structure
    Batch organization (group 3-10 related changes per batch):
    • By section: "Batch 1: Section 2 amendments", "Batch 2: Section 5 updates"
    • By type: "Batch 1: Date corrections", "Batch 2: Party name changes"
    • By complexity: Start with simple text replacements, then tackle complex structural changes
    • Sequential: "Batch 1: Pages 1-3", "Batch 2: Pages 4-6"
  3. Read documentation and unpack:
    • MANDATORY - READ ENTIRE FILE: Read
      ooxml.md
      (~600 lines) completely from start to finish. NEVER set any range limits when reading this file. Pay special attention to the "Document Library" and "Tracked Change Patterns" sections.
    • Unpack the document:
      python ooxml/scripts/unpack.py <file.docx> <dir>
    • Note the suggested RSID: The unpack script will suggest an RSID to use for your tracked changes. Copy this RSID for use in step 4b.
  4. Implement changes in batches: Group changes logically (by section, by type, or by proximity) and implement them together in a single script. This approach:
    • Makes debugging easier (smaller batch = easier to isolate errors)
    • Allows incremental progress
    • Maintains efficiency (batch size of 3-10 changes works well)
    Suggested batch groupings:
    • By document section (e.g., "Section 3 changes", "Definitions", "Termination clause")
    • By change type (e.g., "Date changes", "Party name updates", "Legal term replacements")
    • By proximity (e.g., "Changes on pages 1-3", "Changes in first half of document")
    For each batch of related changes:
    a. Map text to XML: Grep for text in
    word/document.xml
    to verify how text is split across
    <w:r>
    elements.
    b. Create and run script: Use
    get_node
    to find nodes, implement changes, then
    doc.save()
    . See "Document Library" section in ooxml.md for patterns.
    Note: Always grep
    word/document.xml
    immediately before writing a script to get current line numbers and verify text content. Line numbers change after each script run.
  5. Pack the document: After all batches are complete, convert the unpacked directory back to .docx:
    bash
    python ooxml/scripts/pack.py unpacked reviewed-document.docx
  6. Final verification: Do a comprehensive check of the complete document:
    • Convert final document to markdown:
      bash
      pandoc --track-changes=all reviewed-document.docx -o verification.md
    • Verify ALL changes were applied correctly:
      bash
      grep "original phrase" verification.md  # Should NOT find it
      grep "replacement phrase" verification.md  # Should find it
    • Check that no unintended changes were introduced
  1. 获取markdown格式版本:将文档转换为保留修订记录的markdown格式:
    bash
    pandoc --track-changes=all path-to-file.docx -o current.md
  2. 识别并分组变更:审阅文档,识别所有需要修改的内容,将其整理为逻辑批次:
    定位方法(用于在XML中查找变更位置):
    • 章节/标题编号(例如「3.2节」、「第四条」)
    • 带编号的段落标识符
    • 包含唯一上下文文本的Grep匹配模式
    • 文档结构(例如「第一段」、「签名块」)
    • 不要使用markdown行号 - 它们和XML结构没有对应关系
    批次组织(每批次分组3-10个相关变更):
    • 按章节划分:「批次1:第2章修订」、「批次2:第5章更新」
    • 按类型划分:「批次1:日期修正」、「批次2:参与方名称变更」
    • 按复杂度划分:先处理简单的文本替换,再处理复杂的结构变更
    • 按顺序划分:「批次1:第1-3页」、「批次2:第4-6页」
  3. 阅读文档并解压文件
    • 强制要求 - 完整阅读文件:从头到尾完整阅读
      ooxml.md
      (约600行)。读取该文件时绝对不要设置任何范围限制。 特别注意「Document库」和「修订记录模式」部分。
    • 解压文档
      python ooxml/scripts/unpack.py <file.docx> <dir>
    • 记录建议的RSID:解压脚本会推荐一个用于你修订记录的RSID,复制该RSID用于步骤4b。
  4. 分批实现变更:按逻辑分组(按章节、类型或位置)将变更放在同一个脚本中实现。这种方式的优势:
    • 更易调试(批次越小越容易定位错误)
    • 支持渐进式推进
    • 保持效率(3-10个变更的批次大小体验最佳)
    建议的批次分组方式:
    • 按文档章节(例如「第3章变更」、「定义部分」、「终止条款」)
    • 按变更类型(例如「日期修改」、「参与方名称更新」、「法律术语替换」)
    • 按位置邻近度(例如「第1-3页变更」、「文档前半部分变更」)
    处理每一批相关变更时:
    a. 将文本映射到XML:在
    word/document.xml
    中grep对应文本,确认文本在
    <w:r>
    元素中的拆分方式。
    b. 创建并运行脚本:使用
    get_node
    查找节点,实现变更后执行
    doc.save()
    。参考ooxml.md中的**「Document库」**部分了解实现模式。
    注意:编写脚本前务必先grep
    word/document.xml
    获取最新行号并确认文本内容。每次运行脚本后行号都会发生变化。
  5. 打包文档:所有批次修改完成后,将解压后的目录转换回.docx格式:
    bash
    python ooxml/scripts/pack.py unpacked reviewed-document.docx
  6. 最终验证:对完整文档进行全面检查:
    • 将最终文档转换为markdown格式:
      bash
      pandoc --track-changes=all reviewed-document.docx -o verification.md
    • 验证所有变更都已正确应用:
      bash
      grep "original phrase" verification.md  # 应无匹配结果
      grep "replacement phrase" verification.md  # 应有匹配结果
    • 检查是否引入了非预期的变更

Converting Documents to Images

将文档转换为图片

To visually analyze Word documents, convert them to images using a two-step process:
  1. Convert DOCX to PDF:
    bash
    soffice --headless --convert-to pdf document.docx
  2. Convert PDF pages to JPEG images:
    bash
    pdftoppm -jpeg -r 150 document.pdf page
    This creates files like
    page-1.jpg
    ,
    page-2.jpg
    , etc.
Options:
  • -r 150
    : Sets resolution to 150 DPI (adjust for quality/size balance)
  • -jpeg
    : Output JPEG format (use
    -png
    for PNG if preferred)
  • -f N
    : First page to convert (e.g.,
    -f 2
    starts from page 2)
  • -l N
    : Last page to convert (e.g.,
    -l 5
    stops at page 5)
  • page
    : Prefix for output files
Example for specific range:
bash
pdftoppm -jpeg -r 150 -f 2 -l 5 document.pdf page  # Converts only pages 2-5
要对Word文档进行可视化分析,可通过两步流程将其转换为图片:
  1. 将DOCX转换为PDF
    bash
    soffice --headless --convert-to pdf document.docx
  2. 将PDF页面转换为JPEG图片
    bash
    pdftoppm -jpeg -r 150 document.pdf page
    执行后会生成
    page-1.jpg
    page-2.jpg
    等文件。
可选参数:
  • -r 150
    :设置分辨率为150 DPI(可调整以平衡质量和文件大小)
  • -jpeg
    :输出JPEG格式(如果需要PNG格式可使用
    -png
  • -f N
    :转换的起始页码(例如
    -f 2
    表示从第2页开始转换)
  • -l N
    :转换的结束页码(例如
    -l 5
    表示转换到第5页为止)
  • page
    :输出文件的前缀
指定页码范围的示例:
bash
pdftoppm -jpeg -r 150 -f 2 -l 5 document.pdf page  # 仅转换第2-5页

Code Style Guidelines

代码风格指南

IMPORTANT: When generating code for DOCX operations:
  • Write concise code
  • Avoid verbose variable names and redundant operations
  • Avoid unnecessary print statements
重要提示:生成DOCX操作相关代码时:
  • 代码要简洁
  • 避免过长的变量名和冗余操作
  • 避免不必要的打印语句

Dependencies

依赖

Required dependencies (install if not available):
  • pandoc:
    sudo apt-get install pandoc
    (for text extraction)
  • docx:
    npm install -g docx
    (for creating new documents)
  • LibreOffice:
    sudo apt-get install libreoffice
    (for PDF conversion)
  • Poppler:
    sudo apt-get install poppler-utils
    (for pdftoppm to convert PDF to images)
  • defusedxml:
    pip install defusedxml
    (for secure XML parsing)
所需依赖(如果不可用请安装):
  • pandoc
    sudo apt-get install pandoc
    (用于文本提取)
  • docx
    npm install -g docx
    (用于创建新文档)
  • LibreOffice
    sudo apt-get install libreoffice
    (用于PDF转换)
  • Poppler
    sudo apt-get install poppler-utils
    (用于pdftoppm工具将PDF转换为图片)
  • defusedxml
    pip install defusedxml
    (用于安全XML解析)