docxtpl

Generate Word (.docx) documents from Jinja2 templates. Uses python-docx (read/write) + Jinja2 (template engine). Design templates in Microsoft Word, then populate with Python variables.

Safety Notice

This listing is from the official public ClawHub registry. Review SKILL.md and referenced scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "docxtpl" with this command: npx skills add killgfat/docxtpl

docxtpl Skill

Use a .docx file as a Jinja2 template, then fill it with Python data at runtime.

Installation

pip install docxtpl
# For subdocuments support:
pip install "docxtpl[subdoc]"

Quick Start

from docxtpl import DocxTemplate

doc = DocxTemplate("my_template.docx")
context = {"name": "Alice", "department": "Cardiology"}
doc.render(context)
doc.save("output.docx")

Workflow:

  1. Design a .docx in Word with Jinja2 tags ({{ var }}, {%p if ... %})
  2. Load the template with DocxTemplate(path)
  3. Build a Python context dict
  4. Call doc.render(context) then doc.save(path)

Tag Reference

TagScopePurpose
{{ var }}inline stringDisplay a variable
{{r var }}inline styledDisplay a RichText object
{%p ... %}paragraphControl full paragraphs (if/for)
{%tr ... %}table rowControl table rows
{%tc ... %}table columnControl table columns
{%r ... %}runControl styled runs
{%p if cond %} / {%p endif %}paragraphConditional paragraph
{%p for x in list %} / {%p endfor %}paragraphLoop over paragraphs
{% colspan N %}table cellHorizontal cell spanning
{% hm %}table cellHorizontal merge in for-loop
{% vm %}table cellVertical merge in for-loop
{% cellbg color %}table cellSet cell background color
{ #p comment #}paragraphComment
{%- -%}text mergeMerge split lines
{ _%, %_ }, { _{, }_ }escapeDisplay literal {%, %}, {{, }}

Important: Always put spaces around delimiters: {{ var }} not {{var}}.

CLI Usage

python -m docxtpl template.docx data.json output.docx
python -m docxtpl -o template.docx data.json output.docx  # overwrite

Key Classes

  • DocxTemplate(path) — Load a .docx template. Methods: .render(context), .save(path), .get_undeclared_template_variables(context=None)
  • RichText(text, ...) / R(text) — Styled text with .add() for multi-style concatenation
  • InlineImage(tpl, img_path, width, height) — Dynamic image insertion (use Mm(), Inches(), or Pt() for dimensions)
  • Listing(text) — Escaped text preserving \n/\a/\f without losing style
  • RichTextParagraph() / RP() — Paragraph-level properties (use with {{p var }})
  • Subdoctpl.new_subdoc(path) to merge existing .docx files

Media Replacement

For headers/footers where dynamic tags aren't supported:

tpl.replace_pic("dummy.jpg", "real.jpg")      # pictures
tpl.replace_media("dummy.mp3", "real.mp3")     # any media
tpl.replace_embedded("dummy.docx", "real.docx") # embedded docs
tpl.replace_zipname("path/in/zip.xlsx", "real.xlsx") # raw zip entry

Escaping

By default, no auto-escaping. Use one of:

  • RichText for safe content
  • {{ var|e }} in template
  • escape("text") in Python context
  • tpl.render(context, autoescape=True)

Tips

  • \n → newline, \a → new paragraph, \t → tab, \f → page break (in strings and RichText)
  • To render a template multiple times, call tpl.reset_replacements() before each render() if using media replacements
  • Pass a custom jinja_env to render() for custom Jinja2 filters
  • Preview get_undeclared_template_variables() to warn about missing keys

📖 Full API reference: references/official-docs.md

CLI Scripts

Convenience scripts in scripts/ (use {baseDir}/scripts/ path):

# Render with inline key=value pairs
python scripts/docxtpl-render.py template.docx out.docx name="Alice" age="30"

# Render from JSON file
python scripts/docxtpl-render-json.py template.docx data.json out.docx

# Render from JSON string
python scripts/docxtpl-render.py template.docx out.docx --json '{"name":"Bob"}'

# List missing template variables
python scripts/docxtpl-list-vars.py template.docx

# Batch render from CSV (one .docx per row, named by id column)
python scripts/docxtpl-render-batch.py patients.csv ./reports/ template.docx \
  --id-column patient_id --suffix _report --summary

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Coding

Claude Usage Checker

Check Claude Code / Claude Max usage limits. Run when user asks about usage, limits, quota, or how much Claude capacity is left.

Registry SourceRecently Updated
Coding

Software Engineer

Write production-ready code with clean architecture, proper error handling, and pragmatic trade-offs between shipping fast and building right.

Registry SourceRecently Updated
Coding

Box

Box CLI skill for working with files, folders, metadata, search, and Box AI in headless environments.

Registry SourceRecently Updated
Coding

Kimi Code CLI

使用 Kimi Code CLI 执行复杂代码任务。当用户需要: 1. 大型代码生成(完整功能模块、多文件项目) 2. 复杂代码重构(legacy 代码现代化、架构调整) 3. 深度 Bug 诊断(多文件关联错误、难以定位的问题) 4. 技术调研实现(新技术栈完整集成、PoC 开发) 5. 长时间运行的开发任务(...

Registry SourceRecently Updated