docx-template-filling
Robustly fill Word (.docx) templates with JSON data. Handles split placeholders, nested tables, headers/footers, and conditional logic like IF/END_IF blocks. Use this skill whenever you need to generate professional documents from templates.
- License 许可
- MIT
- 最近更新
- 2026-10-02
- Stars
- ★ 93
- Forks
- 8
01 前言 Front Matter
- name
- docx-template-filling
- description
- Robustly fill Word (.docx) templates with JSON data. Handles split placeholders, nested tables, headers/footers, and conditional logic like IF/END_IF blocks. Use this skill whenever you need to generate professional documents from templates.
03 SKILL.md 正文
docx-template-filling
Overview
This skill provides a robust framework for filling Microsoft Word templates using the python-docx library. It specifically addresses the "Split Placeholder" problem and supports conditional content blocks.
Core Principles
- Paragraph-Level Processing: Always modify text at the paragraph level (
para.text) rather than the run level (run.text) to avoid issues where Word splits placeholders like{{NAME}}into multiple runs (e.g.,{{NAandME}}). - Formatting Preservation: When replacing text in a paragraph, clear all runs except the first one. Update the first run's text to the new content to preserve the paragraph's initial formatting.
- Comprehensive Traversal: Process the main document body, tables (including nested ones), and headers/footers of all sections.
Handling Conditional Sections
Conditional sections are marked with {{IF_CONDITION}}...{{END_IF_CONDITION}}.
- If condition is True: Remove the markers but keep the content in between.
- If condition is False: Remove the entire block including markers and content.
Implementation Pattern for Conditionals
def process_conditionals(doc, condition_key, is_enabled):
start_marker = f"{{{{IF_{condition_key}}}}}"
end_marker = f"{{{{END_IF_{condition_key}}}}}"
# Track paragraphs to delete if condition is False
paras_to_delete = []
in_block = False
for para in doc.paragraphs:
if start_marker in para.text:
in_block = True
# Remove marker from this paragraph
para.text = para.text.replace(start_marker, "")
if in_block and not is_enabled:
paras_to_delete.append(para)
if end_marker in para.text:
in_block = False
# Remove marker from this paragraph
para.text = para.text.replace(end_marker, "")
Handling Split Placeholders
Use a regex-based replacement strategy at the paragraph level.
import re
def replace_placeholders(para, data):
text = para.text
pattern = r"\{\{([A-Z_0-9]+)\}\}"
matches = re.findall(pattern, text)
if not matches:
return False
new_text = text
for key in matches:
placeholder = f"{{{{{key}}}}}"
if key in data:
new_text = new_text.replace(placeholder, str(data[key]))
if new_text != text:
# Preserve first run formatting
if para.runs:
para.runs[0].text = new_text
for run in para.runs[1:]:
run.text = ""
else:
para.text = new_text
return True
return False
Traversal Logic
Ensure all parts of the document are reached:
doc.paragraphsdoc.tables(recurse intocell.paragraphsandcell.tables)doc.sections(iteratesection.header.paragraphsandsection.footer.paragraphs)
04 同仓库更多 Skills
object_counter
Count occurrences of an object in the image using computer vision algorithm.
travel-planning-workflow
Invoke this skill when performing travel planning. It defines a structured workflow for generating itineraries. Following this workflow is essential to ensure the final plan satisfies user requirements.
bundled-dataset-search-tool
invoke this skill when you need to perform database search for travel planning. This skill provides some useful pre-packaged tools to look up accommodations, attractions, cities, driving distance, flights, and restaurants from the bundled dataset.
glm-output
Output-processing guidance for GLM tasks. Especially useful after glm-basics and glm-calibration when you need verifier-matching metrics from output.nc and a final /root/metrics.json.
glm-calibration
Calibration guidance for GLM tasks. Often most effective after glm-basics has clarified the setup; glm-output is the companion skill for exact final metric computation.
glm-basics
A good starting point for GLM calibration tasks. Use to inspect glm3.nml, confirm how GLM runs, and identify the relevant files before moving on to calibration and output evaluation.