| name | Word Document Handler |
| description | Comprehensive Microsoft Word (.docx) document creation, editing, and analysis with support for tracked changes, comments, formatting preservation, and text extraction |
| when_to_use | When users need to work with Word documents for: (1) Creating new documents with professional formatting, (2) Reading and analyzing existing content, (3) Modifying content while preserving formatting, (4) Working with tracked changes, or (5) Adding comments |
| version | 0.0.1 |
DOCX creation, editing, and analysis
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.
Workflow Decision Tree
Reading/Analyzing Content
Use "Text extraction" or "Raw XML access" sections below
Creating New Document
Use "Creating a new Word document" workflow
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)
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:
pandoc --track-changes=all path-to-file.docx -o output.md
Raw XML access
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.
Unpacking a file
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
Creating a new Word document
When creating a new Word document from scratch, use docx-js, which allows you to create Word documents using JavaScript/TypeScript.
Workflow
- 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.
- 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)
- Export as .docx using Packer.toBuffer()
Editing an existing Word document
When editing an existing Word document, you need to work with the raw Office Open XML (OOXML) format. This involves unpacking the .docx file, editing the XML content, and repacking it.
Workflow
- MANDATORY - READ ENTIRE FILE: Read
ooxml.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 validation rules, and patterns before proceeding.
- Unpack the document:
python ooxml/scripts/unpack.py <office_file> <output_directory>
- Edit the XML files (primarily
word/document.xml and word/comments.xml)
- CRITICAL: Validate immediately after each edit and fix any validation errors before proceeding:
python ooxml/scripts/validate.py <dir> --original <file>
- Pack the final document:
python ooxml/scripts/pack.py <input_directory> <office_file>
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.
Comprehensive tracked changes workflow
-
Get markdown representation: Convert document to markdown with tracked changes preserved:
pandoc --track-changes=all path-to-file.docx -o current.md
-
Create comprehensive revision checklist: Create a detailed checklist of ALL changes needed, with tasks listed in sequential order.
- All tasks should start as unchecked items using
[ ] format
- DO NOT use markdown line numbers - they don't map to XML structure
- DO use:
- 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")
- Example:
[ ] Section 8: Change "30 days" to "60 days" (grep: "notice period of.*days prior")
- Consider that text may be split across multiple
<w:t> elements due to formatting
- Save as
revision-checklist.md
-
Setup tracked changes infrastructure:
- Unpack the document:
python ooxml/scripts/unpack.py <office_file> <output_directory>
- Run setup script:
python skills/docx/scripts/setup_redlining.py <unpacked_directory>
- This automatically:
- Creates
word/people.xml with Claude as author (ID 0)
- Updates
[Content_Types].xml to include people.xml content type
- Updates
word/_rels/document.xml.rels to add people.xml relationship
- Adds
<w:trackRevisions/> to word/settings.xml
- Generates and adds a random 8-character hex RSID (e.g., "6CEA06C3")
- Displays the generated RSID for reference
- CRITICAL: Note the RSID displayed by the script - you MUST use this same RSID for ALL tracked changes
-
Apply changes from checklist systematically:
- MANDATORY - READ ENTIRE FILE: Read
ooxml.md (~500 lines) completely from start to finish. NEVER set any range limits when reading this file. Pay special attention to the section titled "Tracked Change Patterns".
Converting Documents to Images
To visually analyze Word documents, convert them to images using a two-step process:
-
Convert DOCX to PDF:
soffice --headless --convert-to pdf document.docx
-
Convert PDF pages to JPEG images:
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:
pdftoppm -jpeg -r 150 -f 2 -l 5 document.pdf page
Code Style Guidelines
IMPORTANT: When generating code for DOCX operations:
- Write concise code
- Avoid verbose variable names and redundant operations
- Avoid unnecessary print statements
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)