"First, do no harm." When editing an existing document, minimize changes. Touch only what needs to change. Preserve all formatting, styles, relationships, and structure that are not directly involved in the edit.
{{name}}, $DATE$, [PLACEHOLDER])Do NOT use when: the user wants to change the look/style of the entire document (→ Scenario C) or create from scratch (→ Scenario A).
1. Preview → CLI: analyze <input.docx>
2. Analyze → Understand structure: sections, styles, headings, tables
3. Identify → Locate exact edit targets (paragraph index, table index, placeholder text)
4. Edit → Apply surgical changes via CLI or direct XML
5. Validate → CLI: validate <output.docx>
6. Diff → Compare before/after to verify only intended changes were made
{{fieldName}} → actual value)The CLI natively supports {{fieldName}} placeholders:
# Replace all {{placeholders}} from a JSON map
dotnet run ... edit input.docx --fill-placeholders data.json --output filled.docx
Where data.json:
{
"companyName": "Acme Corp",
"date": "March 21, 2026",
"amount": "$15,000.00",
"recipientName": "Jane Smith"
}
Other placeholder formats ($FIELD$, [PLACEHOLDER]) require text replacement:
dotnet run ... edit input.docx --replace "$DATE$" "March 21, 2026" --output updated.docx
When the entire search text is within a single w:r (run):
<!-- Before -->
<w:r>
<w:rPr><w:b /></w:rPr>
<w:t>{{companyName}}</w:t>
</w:r>
<!-- After — formatting preserved -->
<w:r>
<w:rPr><w:b /></w:rPr>
<w:t>Acme Corp</w:t>
</w:r>
Direct replacement. The run's w:rPr is untouched.
When the search text is split across multiple runs (common when Word applies spell-check or formatting mid-text):
<!-- "{{companyName}}" split into 3 runs -->
<w:r><w:rPr><w:b /></w:rPr><w:t>{{company</w:t></w:r>
<w:r><w:rPr><w:b /><w:i /></w:rPr><w:t>Na</w:t></w:r>
<w:r><w:rPr><w:b /></w:rPr><w:t>me}}</w:t></w:r>
Strategy:
w:rPr)Remove the text from subsequent runs (or remove the runs entirely if empty)
<!-- After -->
<w:r><w:rPr><w:b /></w:rPr><w:t>Acme Corp</w:t></w:r>
Rule: Always preserve the formatting of the first run in the match.
Tables are 0-indexed in document order:
dotnet run ... edit input.docx --table-index 0 --table-data data.json --output updated.docx
Find a table by its header row content:
dotnet run ... edit input.docx --table-match "Name,Amount,Date" --table-data data.json
{
"rows": [
["Alice Johnson", "$5,000", "2026-03-15"],
["Bob Smith", "$3,200", "2026-03-18"]
],
"appendRows": true
}
appendRows: true — add rows after existing dataappendRows: false (default) — replace all data rows (keeps header row)To modify a specific cell, locate it by row/column index:
<!-- Row 2 (0-indexed), Column 1 -->
<w:tr> <!-- tr[2] -->
<w:tc>...</w:tc>
<w:tc> <!-- tc[1] — target cell -->
<w:p>
<w:r><w:t>Old Value</w:t></w:r>
</w:p>
</w:tc>
</w:tr>
Replace the w:t content. Do NOT modify w:tcPr (cell properties) or w:tblPr (table properties).
w:trackChanges in settings)See references/track_changes_guide.md for full XML examples.
Quick reference — inserting text with tracking:
<w:ins w:id="1" w:author="MiniMaxAI" w:date="2026-03-21T10:00:00Z">
<w:r>
<w:t>New text here</w:t>
</w:r>
</w:ins>
Deleting text with tracking:
<w:del w:id="2" w:author="MiniMaxAI" w:date="2026-03-21T10:00:00Z">
<w:r>
<w:delText>Removed text</w:delText> <!-- MUST use delText, not t -->
</w:r>
</w:del>
Problem: Replacing text that spans runs by naively modifying individual runs destroys inline formatting.
Fix: Concatenate run text, find match boundaries, consolidate into the first run, remove consumed runs.
Problem: Replacing text inside a w:hyperlink element without preserving the hyperlink wrapper removes the link.
<w:hyperlink r:id="rId5">
<w:r>
<w:rPr><w:rStyle w:val="Hyperlink" /></w:rPr>
<w:t>Click here</w:t> <!-- Only replace this text -->
</w:r>
</w:hyperlink>
Fix: Only modify the w:t inside the hyperlink's run. Never remove or replace the w:hyperlink element itself.
Problem: Replacing text that is inside a w:ins or w:del element without understanding the revision context creates invalid markup.
Fix: If the target text is inside a revision mark, either:
w:ins/w:del wrapper)Problem: Adding new paragraphs without specifying a style causes them to inherit Normal, which may not match the surrounding context.
Fix: When inserting paragraphs, copy the w:pStyle from an adjacent paragraph of the same type.
Problem: Inserting a new list item breaks numbering sequence.
Fix: Ensure the new paragraph has the same w:numId and w:ilvl as adjacent list items. If continuing a sequence, set w:numPr to match.
Problem: User content contains &, <, >, ", ' — these must be escaped in XML.
Fix: Always XML-escape user-provided text before inserting into w:t elements:
& → &< → <> → >" → "' → 'Problem: Leading/trailing spaces in w:t are stripped by XML parsers.
Fix: Add xml:space="preserve" attribute:
<w:t xml:space="preserve"> text with leading space</w:t>
After editing, always compare the before and after states:
# Structural diff — shows only changed elements
dotnet run ... diff original.docx modified.docx
# Text-only diff — shows content changes
dotnet run ... diff original.docx modified.docx --text-only
Verify: