Use Scenario C when:
Do NOT use when: the user wants to edit content (→ Scenario B) or create from scratch (→ Scenario A).
1. Analyze source → CLI: analyze source.docx (list styles, fonts, structure)
2. Analyze template → CLI: analyze template.docx (list styles, fonts, structure)
3. Map styles → Create mapping plan (source style → template style)
4. Apply template → CLI: apply-template source.docx --template template.docx --output result.docx
5. Validate (XSD) → CLI: validate result.docx --xsd wml-subset.xsd
6. GATE-CHECK → CLI: validate result.docx --xsd business-rules.xsd ← MUST PASS
7. Diff verify → CLI: diff source.docx result.docx --text-only (content must be identical)
| Part | File | Description |
|---|---|---|
| Styles | word/styles.xml |
All style definitions (paragraph, character, table, numbering) |
| Theme | word/theme/theme1.xml |
Color scheme, font scheme, format scheme |
| Numbering | word/numbering.xml |
List and numbering definitions |
| Headers | word/header*.xml |
Header content and formatting |
| Footers | word/footer*.xml |
Footer content and formatting |
| Section props | w:sectPr |
Margins, page size, orientation, columns |
| Part | Reason |
|---|---|
| Document content | Paragraphs, tables, images stay from source |
| Comments | Belong to source document's review history |
| Tracked changes | Belong to source document's revision history |
| Custom XML parts | Application-specific data, not visual |
| Document properties | Title, author, dates belong to source |
| Glossary document | Template's building blocks are not transferred |
Before choosing Overlay or Base-Replace, you MUST analyze the template's internal structure. This is the #1 cause of failure when skipped.
Run $CLI analyze --input template.docx or manually inspect:
# Quick structure scan
scripts/docx_preview.sh template.docx
Identify these zones in the template:
Zone A: Front matter (cover page, declaration, abstract, TOC)
→ These are KEPT from template, never replaced
Zone B: Example/placeholder body content ("第1章 XXX", sample paragraphs)
→ This is REPLACED with user's actual content
Zone C: Back matter (appendices, acknowledgments, blank pages)
→ These are KEPT from template or removed
Zone D: Final sectPr
→ ALWAYS kept from template
Search the template's document.xml for anchor text that marks the start and end of example content:
Start anchor patterns (first paragraph of example body):
End anchor patterns (last paragraph before back matter):
The last paragraph before appendices or final sectPr
# Pseudocode for finding replacement range
for i, element in enumerate(template_body_elements):
text = get_text(element)
style = get_style(element)
if style in heading1_styles and ("第1章" in text or "Chapter 1" in text):
replace_start = i
if "参考文献" in text or "References" in text:
replace_end = i
break
CRITICAL: Verify the range by printing what's inside:
Template elements [0..replace_start-1]: front matter (KEEP)
Template elements [replace_start..replace_end]: example content (REPLACE)
Template elements [replace_end+1..end]: back matter (KEEP)
If replace_start or replace_end cannot be found, DO NOT proceed. Ask the user to identify the replacement boundaries.
Now that you know the structure:
| Observation | Decision |
|---|---|
| Template has ≤30 paragraphs, no cover/TOC | C-1: Overlay (pure style template) |
| Template has >100 paragraphs with cover/TOC/example sections | C-2: Base-Replace |
| Template paragraph count ≈ user document | C-1: Overlay (similar structure) |
| Template paragraph count >> user document (e.g., 263 vs 134) | C-2: Base-Replace |
list(body) — NOT findall('w:p') (which misses tables)template[0:replace_start] + cleaned_user_content + template[replace_end+1:]When template style names differ from source style names, a mapping is required. This step is mandatory — skipping it is the #1 cause of formatting failures in template application.
Before any template application, extract and compare styleIds from both documents:
# Extract all styleIds from source
$CLI analyze --input source.docx --styles-only
# Output example:
# Heading1 (paragraph, basedOn: Normal)
# Heading2 (paragraph, basedOn: Normal)
# Normal (paragraph)
# ListBullet (paragraph, basedOn: Normal)
# Extract all styleIds from template
$CLI analyze --input template.docx --styles-only
# Output example:
# 1 (paragraph, basedOn: a, name: "heading 1")
# 2 (paragraph, basedOn: a, name: "heading 2")
# 3 (paragraph, basedOn: a, name: "heading 3")
# a (paragraph, name: "Normal")
# a0 (character, name: "Default Paragraph Font")
Critical distinction: w:styleId vs w:name:
<!-- styleId="1" but name="heading 1" -->
<w:style w:type="paragraph" w:styleId="1">
<w:name w:val="heading 1"/>
<w:basedOn w:val="a"/>
</w:style>
The w:styleId attribute is what <w:pStyle w:val="..."/> references. The w:name attribute is the human-readable display name. They can be completely different. Many CJK templates use numeric styleIds (1, 2, 3, a, a0) instead of English names.
If source uses Heading1 and template defines Heading1 as a styleId, map directly. No action needed.
If no exact styleId match, try matching by w:name attribute:
Heading1 (name="heading 1") → Template styleId 1 (name="heading 1")Within the same type, also try matching by:
For renamed or custom styles, provide an explicit mapping:
{
"styleMap": {
"Heading1": "1",
"Heading2": "2",
"Heading3": "3",
"Heading4": "3",
"Normal": "a",
"BodyText": "a",
"ListBullet": "a",
"CompanyName": "Title",
"OldTableStyle": "TableGrid"
}
}
| Template Origin | StyleId Pattern | Example |
|---|---|---|
| Chinese Word (default) | Numeric/alphabetic | 1, 2, 3, a, a0 |
| English Word (default) | English names | Heading1, Normal, Title |
| Google Docs export | Prefixed | Subtitle, NormalWeb |
| WPS Office | Mixed | 1, Heading1, custom names |
| Academic templates | Custom | ThesisHeading1, ThesisBody |
Follow this algorithm:
List source styleIds actually used in document.xml (not all defined in styles.xml):
# Pseudocode: find all unique pStyle values in source document.xml
used_styles = set()
for p in body.iter('w:p'):
pStyle = p.find('w:pPr/w:pStyle')
if pStyle is not None:
used_styles.add(pStyle.get('val'))
For each used style, find the best match in template:
w:name value (case-insensitive)Normal or a)Validate the mapping — every source styleId must map to an existing template styleId:
✓ Heading1 → 1 (name match: "heading 1")
✓ Heading2 → 2 (name match: "heading 2")
✓ Normal → a (name match: "Normal")
✗ CustomCallout → ??? (no match found, will fallback to 'a')
Apply the mapping when copying content — update every <w:pStyle w:val="..."/>:
<!-- Source -->
<w:pPr><w:pStyle w:val="Heading1"/></w:pPr>
<!-- After mapping -->
<w:pPr><w:pStyle w:val="1"/></w:pPr>
Styles in the source document that have no match in the template are logged as warnings:
WARNING: Style 'CustomCallout' has no mapping in template. Content will fall back to 'a' (Normal).
The content is preserved; only the style reference is updated to the template's default paragraph style.
When using the template as a base document (C-2 strategy), the template's styles.xml is already in place. You must:
styles.xml — the template's styles are the authorityTableGrid but template defines it as a3 or similar, remap <w:tblStyle> toorPr inside runs may reference character styles like Hyperlink or Strong that have different IDs in the templateWhen copying content from source to template, apply these rules to EACH paragraph and run:
REMOVE from <w:rPr>:
<w:rFonts w:ascii="..." w:hAnsi="..."/> — Latin font overrides (EXCEPT: keep w:eastAsia)<w:sz>, <w:szCs> — font size (let style control)<w:color> — text color<w:highlight> — highlight color<w:shd> — shading<w:b>, <w:i> — bold/italic UNLESS the source style requires it (e.g., emphasis)<w:u> — underline<w:spacing> — character spacingKEEP in <w:rPr>:
<w:rFonts w:eastAsia="宋体"/> — CJK font declaration (MUST keep, or Chinese text renders wrong)<w:rFonts w:eastAsia="华文中宋"/> — same reason<w:drawing> — image references (handle separately via rId remapping)REMOVE from <w:pPr>:
<w:pBdr> — paragraph borders<w:shd> — paragraph shading<w:spacing> — line/paragraph spacing (let style control)<w:jc> — justification (let style control)<w:tabs> — custom tab stops<w:rPr> inside pPr — default run formatting for the paragraphKEEP in <w:pPr>:
<w:pStyle> — style reference (after mapping to template's styleId)<w:sectPr> — section properties (if intentionally inserting section breaks)<w:numPr> — numbering reference (after mapping numId to template's numbering)Table cells (<w:tc>):
Apply the same rPr/pPr cleanup to every paragraph inside every cell. Also:
<w:tcPr> structural properties (column span, row span, width)<w:tcPr><w:shd> (cell shading — let table style control)When copying parts (headers, footers, images) from the template into the source package, relationship IDs (r:id) may collide.
Problem:
rId7 → image1.pngrId7 → header1.xmlrId7 overwrites source's image referenceSolution:
document.xml.rels for all existing rId valuesrId12)rId13Update all references in copied parts to use new IDs
<!-- Template original -->
<Relationship Id="rId1" Type="...header" Target="header1.xml" />
<!-- After remapping into source package -->
<Relationship Id="rId13" Type="...header" Target="header1.xml" />
<!-- Update sectPr reference -->
<w:headerReference w:type="default" r:id="rId13" />
When the source document contains external hyperlinks (e.g., URLs in references or footnotes), these are stored as relationships in word/_rels/document.xml.rels:
<Relationship Id="rId15" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"
Target="https://example.com/paper" TargetMode="External"/>
The corresponding text in document.xml references this rId:
<w:hyperlink r:id="rId15">
<w:r><w:t>https://example.com/paper</w:t></w:r>
</w:hyperlink>
Merging steps:
<w:hyperlink r:id="..."> elementsword/_rels/footnotes.xml.rels) and endnotesCommon mistake: Copying hyperlink paragraphs without merging rels → hyperlinks silently break (clicking does nothing in Word).
After template application, the output document MUST pass business-rules.xsd validation. This is a hard gate — if it fails, the document is NOT deliverable.
| Rule | What It Validates |
|---|---|
| Template styles exist | All styles referenced by content paragraphs are defined in styles.xml |
| Margins match | Page margins match template specification |
| Fonts correct | w:docDefaults fonts match template's font scheme |
| Heading hierarchy | Heading levels are sequential (no H1 → H3 without H2) |
| Required styles present | Normal, Heading1-Heading3, TableGrid exist |
| Page size | Matches template's declared page size |
GATE-CHECK FAILED:
- Style 'CustomStyle1' referenced in paragraph 14 but not defined in styles.xml
- Margin w:left=1080 does not match template requirement 1440
Fix each failure:
styles.xml, or remap the paragraph to an existing stylew:sectPr margins to match templatew:docDefaults to match template font schemeRe-validate after every fix until gate-check passes.
Problem: Source document uses w:numId="5" in list paragraphs, but after replacing numbering.xml with the template's version, numbering ID 5 doesn't exist.
Symptom: Lists appear as plain paragraphs (no bullets/numbers).
Fix:
w:numId references in document contentnumbering.xmlProblem: Source document's styles reference theme colors (w:themeColor="accent1") that have different values in the template's theme.
Symptom: Colors change unexpectedly (usually acceptable — this IS the point of re-theming). But if a style uses w:color with both w:val and w:themeColor, the theme color wins in Word.
Fix: Review color changes. If specific colors must be preserved, use explicit w:val without w:themeColor.
Problem: Source document has multiple sections (e.g., portrait + landscape pages), but the template assumes a single section.
Symptom: All sections get the same margins/orientation, breaking landscape pages.
Fix:
w:sectPr in w:bodyw:sectPr elements (inside w:pPr) from the sourceProblem: Template specifies fonts not available on the target system.
Fix: Either embed fonts in the DOCX (word/fonts/) or use web-safe alternatives:
Problem: Template has Heading1 based on Normal, but after applying template, Normal has different properties, cascading unwanted changes to headings.
Fix: Verify the w:basedOn chain for all critical styles. Ensure base styles are also correctly transferred from template.
After template application, verify:
business-rules.xsd validation succeeds