Skip to main content

Overview

The DOCX skill enables comprehensive Word document manipulation including creating professional documents from scratch, editing existing files with tracked changes and comments, and extracting content. A .docx file is a ZIP archive containing XML files that can be programmatically manipulated.
Use this skill whenever working with Word documents (.docx files), including creating reports, memos, letters, templates, or any document requiring professional formatting like tables of contents, headings, page numbers, or letterheads.

Quick Reference

Reading and Converting Documents

Converting Legacy .doc Files

Legacy .doc files must be converted before editing:

Reading Content

Converting to Images

Accepting Tracked Changes

To produce a clean document with all tracked changes accepted:

Creating New Documents

Setup and Installation

Install the docx-js library globally:

Basic Document Structure

After creating a document, always validate it with python scripts/office/validate.py doc.docx. If validation fails, unpack, fix the XML, and repack.

Page Size and Orientation

CRITICAL: docx-js defaults to A4, not US Letter. Always set page size explicitly:
Common page sizes (DXA units):Landscape orientation:

Styles and Headings

Use Arial as the default font (universally supported). Override built-in heading styles:

Lists

NEVER use unicode bullets manually. Always use numbering configuration with LevelFormat.BULLET.
Each reference creates INDEPENDENT numbering. Same reference continues (1,2,3 then 4,5,6), different reference restarts (1,2,3 then 1,2,3).

Tables

CRITICAL: Tables need dual widths - set both columnWidths on the table AND width on each cell.
Width rules:
  • Always use WidthType.DXA — never WidthType.PERCENTAGE (breaks in Google Docs)
  • Table width must equal sum of columnWidths
  • Cell width must match corresponding columnWidth
  • Cell margins are internal padding - reduce content area, not add to width
  • For full-width tables: use content width (page width minus margins)

Images

Headers, Footers, and Page Numbers

Table of Contents

Editing Existing Documents

Follow all 3 steps in order:

Step 1: Unpack

Extracts XML, pretty-prints, merges adjacent runs, and converts smart quotes to XML entities so they survive editing.

Step 2: Edit XML

Edit files in unpacked/word/. Use “Claude” as the author for tracked changes and comments unless specified otherwise.
Use the Edit tool directly for string replacement. Do not write Python scripts. Scripts introduce unnecessary complexity.
CRITICAL: Use smart quotes for new content:
Adding comments:

Step 3: Pack

Validates with auto-repair, condenses XML, and creates DOCX.
Auto-repair will fix:
  • durableId >= 0x7FFFFFFF (regenerates valid ID)
  • Missing xml:space="preserve" on <w:t> with whitespace
Auto-repair won’t fix:
  • Malformed XML
  • Invalid element nesting
  • Missing relationships
  • Schema violations

XML Reference

Tracked Changes

Insertion:
Deletion:
Minimal edits - only mark what changes:
Deleting entire paragraphs:

Comments

CRITICAL: <w:commentRangeStart> and <w:commentRangeEnd> are siblings of <w:r>, never inside <w:r>.

Critical Rules

Always follow these rules when using docx-js:
  • Set page size explicitly (defaults to A4, not US Letter)
  • Never use \n - use separate Paragraph elements
  • Never use unicode bullets - use LevelFormat.BULLET
  • PageBreak must be inside a Paragraph
  • ImageRun requires type parameter
  • Always set table width with DXA, never WidthType.PERCENTAGE
  • Tables need dual widths - columnWidths AND cell width
  • Use ShadingType.CLEAR, never SOLID
  • TOC requires HeadingLevel only - no custom styles
  • Include outlineLevel for headings (required for TOC)

Dependencies

  • pandoc: Text extraction
  • docx: npm install -g docx (new documents)
  • LibreOffice: PDF conversion (auto-configured via scripts/office/soffice.py)
  • Poppler: pdftoppm for images