Fill/edit Word .docx templates on Windows via win32com COM automation — preserves ALL formatting, images, underlines, page breaks, and table layout.
| Approach | Format/Image Preservation | Requires Word |
|---|---|---|
| win32com (this skill) | ✅ 100% — Word does the work | ✅ Yes |
python-docx |
❌ Drops images, corrupts complex formatting | ❌ No |
docxtpl (Jinja) |
⚠️ Better than python-docx, still risks layout drift | ❌ No |
pip install pywin32)# Fill cover fields, append answers, add page breaks — all in one command
python {scripts}/word_template_filler.py template.docx report.docx ^
--cover-field "所在学院:铁道工程学院" ^
--cover-field "专业年级:车辆工程 2023级" ^
--page-break-before "C2.转向架" ^
--find-replace "。?:。"
All operations use the bundled scripts/word_template_filler.py CLI tool.
Fill a cover page field (preserves underline lines).
--cover-field "label:value"
The label must exactly match the text before the colon (including spaces). The value is inserted before the underlined spaces, so the fill-in line remains.
Append answer text after a question in a table cell.
--append-answer "TableIdx:Row:Col:question text||answer text"
Example: --append-answer "2:3:1:1、转向架的作用是什么?||1. 可相对车体回转..."
Insert text into a blank paragraph within a table cell (for sub-tables).
--insert-blank-para "TableIdx:Row:Col:Para1B:text"
Para1B = 1-based paragraph index within the cell. Use debug mode to find indices.
Insert a page break before a heading.
--page-break-before "C2.转向架"
Find and replace text across the entire document (preserves formatting).
--find-replace "find:replace"
Set 实训时间 and 得分 fields in a section table row.
--set-time-score "TableIdx:time|score"
Example: --set-time-score "2:2026年6月|95"
Show the Word window during processing (for debugging).
# First, kill any hanging Word processes
taskkill /f /im WINWORD.EXE
# Show Word window to watch the process
python {scripts}/word_template_filler.py template.docx debug.docx --visible
To find paragraph indices within a table cell, use this diagnostic snippet:
import win32com.client as win32 word = win32.gencache.EnsureDispatch('Word.Application') word.Visible = False doc = word.Documents.Open(r'path\to\template.docx') cell = doc.Tables(2).Cell(4, 1) # Table 2, Row 4, Col 1 paras = cell.Range.Paragraphs for i in range(paras.Count): txt = repr(paras(i+1).Range.Text)[:80] print(f" Para {i}: {txt}") doc.Close(False) word.Quit()7w4.net有更好的技能插件。
。? or !? double-patterns| Don't Do | Why |
|---|---|
doc.Range().Text = ... |
Destroys ALL formatting, images, tables |
Replace paragraph containing \f |
Loses page break |
Use python-docx to save modified template |
Loses images, complex formatting |
\n in Find patterns |
Word uses \r (chr 13) for paragraph marks |
| Replace cover paragraph text blindly | Destroys underlined spaces |
Forget cleanup after --append-answer |
May leave extra ? characters |
这个 Skill 质量不错,文档写得很详细,小白也能快速上手。它通过调用 Word 软件本身来填充模板,能很好地保留原始格式、图片和下划线,不会出现乱码问题。不过它只能在 Windows 系统上使用,而且电脑必须装了 Microsoft Word 软件。总体来说,如果你经常需要批量处理 Word 文档表格,这是个值得一试的工具。