editing-word-documents
Reads, creates, edits, and formats Word documents (.docx files), including tracked changes, comments, and template-based generation. Activates when the user works with .docx files or requests document authoring, redlining, or text extraction from Word documents.
What this skill does
# Word Document (.docx) Guide
This guide covers creating, editing, and analyzing Word documents. For JavaScript-based creation, see [docx-js.md](references/docx-js.md). For OOXML technical details and tracked changes, see [ooxml.md](references/ooxml.md).
## Workflows Overview
| Task | Approach | Reference |
|------|----------|-----------|
| Read/analyze document | pandoc or raw XML | This file |
| Create new document | docx-js (JavaScript) | docx-js.md |
| Edit existing document | Python Document library | ooxml.md |
| Add tracked changes | Python Document library | ooxml.md |
## Reading & Analysis
### Convert to Markdown (Quick Read)
```bash
pandoc document.docx -o output.md
```
### Extract Text with Python
```python
from docx import Document
doc = Document("document.docx")
for para in doc.paragraphs:
print(para.text)
```
### Access Raw XML (Detailed Analysis)
```bash
# Unpack the .docx file
unzip document.docx -d unpacked/
# Main document content
cat unpacked/word/document.xml
# Styles
cat unpacked/word/styles.xml
# Comments
cat unpacked/word/comments.xml
```
### Visual Analysis
Convert to PDF then to images for visual inspection:
```bash
# Using LibreOffice
libreoffice --headless --convert-to pdf document.docx
# Convert PDF to images
pdftoppm -png -r 200 document.pdf page
```
## Creating Documents (docx-js)
For new documents, use the docx-js library. **Read [docx-js.md](references/docx-js.md) fully before starting.**
### Basic Example
```javascript
const { Document, Packer, Paragraph, TextRun, HeadingLevel } = require("docx");
const fs = require("fs");
const doc = new Document({
sections: [{
properties: {},
children: [
new Paragraph({
text: "Document Title",
heading: HeadingLevel.HEADING_1
}),
new Paragraph({
children: [
new TextRun("Normal text with "),
new TextRun({ text: "bold", bold: true }),
new TextRun(" formatting.")
]
})
]
}]
});
Packer.toBuffer(doc).then(buffer => {
fs.writeFileSync("output.docx", buffer);
});
```
## Editing Documents
For editing existing documents, use the Python approach with direct XML manipulation.
### Simple Text Replacement
```python
from docx import Document
doc = Document("template.docx")
for para in doc.paragraphs:
if "{{NAME}}" in para.text:
para.text = para.text.replace("{{NAME}}", "John Doe")
doc.save("filled.docx")
```
### Advanced Editing (Preserve Formatting)
For edits that preserve formatting, work with XML directly:
```python
# Unpack, modify XML, repack
import zipfile
import xml.etree.ElementTree as ET
# Extract
with zipfile.ZipFile("document.docx", "r") as zip_ref:
zip_ref.extractall("unpacked")
# Parse and modify
tree = ET.parse("unpacked/word/document.xml")
root = tree.getroot()
# ... make changes ...
tree.write("unpacked/word/document.xml", xml_declaration=True)
# Repack
with zipfile.ZipFile("modified.docx", "w") as zipf:
for root, dirs, files in os.walk("unpacked"):
for file in files:
file_path = os.path.join(root, file)
arcname = os.path.relpath(file_path, "unpacked")
zipf.write(file_path, arcname)
```
## Tracked Changes (Redlining)
For professional document editing with tracked changes:
1. **Convert to markdown** to understand content
2. **Identify changes** in batches of 3-10 edits
3. **Apply to XML** preserving original run attributes
**Critical guideline**: Only mark text that actually changes. Repeating unchanged text makes edits harder to review.
See [ooxml.md](references/ooxml.md) for detailed tracked changes implementation.
### Tracked Change Example
```xml
<!-- Original: "The quick brown fox" -->
<!-- After deletion of "quick ": -->
<w:p>
<w:r>
<w:t>The </w:t>
</w:r>
<w:del w:author="Editor" w:date="2024-01-15T10:30:00Z">
<w:r>
<w:delText>quick </w:delText>
</w:r>
</w:del>
<w:r>
<w:t>brown fox</w:t>
</w:r>
</w:p>
```
## Adding Comments
```python
from docx import Document
from docx.oxml.ns import qn
from docx.oxml import OxmlElement
doc = Document("document.docx")
# Add comment to first paragraph
para = doc.paragraphs[0]
comment = OxmlElement("w:commentReference")
comment.set(qn("w:id"), "1")
para._p.append(comment)
# Add comment content to comments.xml
# (requires direct XML manipulation)
doc.save("commented.docx")
```
## Document Properties
```python
from docx import Document
doc = Document("document.docx")
# Read properties
core_props = doc.core_properties
print(f"Title: {core_props.title}")
print(f"Author: {core_props.author}")
print(f"Created: {core_props.created}")
# Set properties
core_props.title = "New Title"
core_props.author = "New Author"
doc.save("updated.docx")
```
## Dependencies
```bash
# Python
pip install python-docx lxml
# JavaScript
npm install docx
# CLI tools
brew install pandoc libreoffice # macOS
apt-get install pandoc libreoffice # Ubuntu
```
## Best Practices
1. **Always read reference files** (docx-js.md and ooxml.md) before complex operations
2. **Batch edits logically** - group related changes together
3. **Preserve formatting** - work at the run level, not paragraph level
4. **Validate output** - open in Word to verify no corruption
5. **Keep backups** - OOXML manipulation can corrupt files if done incorrectly
Related in Writing & Docs
jax-development
IncludedUse this skill when the user is writing, debugging, profiling, refactoring, reviewing, benchmarking, parallelising, exporting, or explaining JAX code, or when they mention JAX, jax.numpy, jit, grad, value_and_grad, vmap, scan, lax, random keys, pytrees, jax.Array, sharding, Mesh, PartitionSpec, NamedSharding, pmap, shard_map, Pallas, XLA, StableHLO, checkify, profiler, or the JAX repo. It helps turn NumPy or PyTorch-style code into pure functional JAX, fix tracer/control-flow/shape/PRNG bugs, remove recompiles and host-device syncs, choose transforms and sharding strategies, inspect jaxpr/lowering/IR, and benchmark compiled code correctly.
nature-article-writer
IncludedDrafts, rewrites, diagnostically critiques, and style-calibrates primary research manuscripts for Nature and Nature Portfolio journals. Use when the user wants a Nature-style title, summary paragraph or abstract, introduction, results, discussion, methods, figure legends, presubmission enquiry, cover letter, reviewer response, or when a scientific draft sounds generic, jargon-heavy, structurally weak, or AI-ish and needs precise, broad-reader-friendly prose without inventing data, analyses, or references. Best for primary research articles and letters rather than reviews or press releases unless explicitly adapting one.
deckrd
IncludedDocument-driven framework that derives requirements, specifications, implementation plans, and executable tasks from goals through structured AI dialogue. Use when user says "write requirements", "create spec", "plan implementation", "derive tasks", "structure this feature", "break down into tasks", or "document this module". Also use for reverse engineering existing code into docs (/deckrd rev). Do NOT use for direct code writing — use /deckrd-coder after tasks are generated. Do NOT use when the user only wants to run or fix existing code without planning.
clinical-decision-support
IncludedGenerate professional clinical decision support (CDS) documents for pharmaceutical and clinical research settings, including patient cohort analyses (biomarker-stratified with outcomes) and treatment recommendation reports (evidence-based guidelines with decision algorithms). Supports GRADE evidence grading, statistical analysis (hazard ratios, survival curves, waterfall plots), biomarker integration, and regulatory compliance. Outputs publication-ready LaTeX/PDF format optimized for drug development, clinical research, and evidence synthesis.
handling-sf-data
IncludedSalesforce data operations with 130-point scoring. Use this skill to create, update, delete, bulk import/export, generate test data, and clean up org records using sf CLI and anonymous Apex. TRIGGER when: user creates test data, performs bulk import/export, uses sf data CLI commands, needs data factory patterns for Apex tests, or needs to seed/clean records in a Salesforce org. DO NOT TRIGGER when: SOQL query writing only (use querying-soql), Apex test execution (use running-apex-tests), or metadata deployment (use deploying-metadata).
accelint-ac-to-playwright
IncludedConvert and validate acceptance criteria for Playwright test automation. Use when user asks to (1) review/evaluate/check if AC are ready for automation, (2) assess if AC can be converted as-is, (3) validate AC quality for Playwright, (4) turn AC into tests, (5) generate tests from acceptance criteria, (6) convert .md bullets or .feature Gherkin files to Playwright specs, (7) create test automation from requirements. Handles both bullet-style markdown and Gherkin syntax with JSON test plan generation and validation.