Template Engine

作者 claude-office-skills9c4c7d5cd281MIT499 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫8 個月前更新

Auto-fill document templates with data - mail merge for any format

僅含說明Documents & Office
AI 產生的概覽

以資料填入文件範本,支援 Word、Excel 與 PowerPoint 檔案的合併列印。

功能
此技能說明以範本為基礎的文件產生方式:範本中含有預留位置,再以提供的資料填入。內容涵蓋 Jinja2 風格的取代、迴圈、條件與篩選器語法,並提供透過 docxtpl 處理 Word、透過 openpyxl 處理 Excel 的範例。它也說明從 CSV 檔案批次合併列印以及條件式內容呈現,最終產出填妥的文件。
適用情境
適用於信件或合約的合併列印、依資料產生個人化報告、以範本製作證書,或以使用者資料自動填寫表單。
執行需求
僅為說明內容,未附指令碼。文件中的範例需要 Python 以及 python-docx、openpyxl、python-pptx、reportlab 與 jinja2,並提及 office-mcp 伺服器的 fill_docx_template 工具。

Template Engine Skill

Overview

This skill enables template-based document generation - define templates with placeholders, then automatically fill them with data. Works with Word, Excel, PowerPoint, and more.

How to Use

  1. Describe what you want to accomplish
  2. Provide any required input data or files
  3. I'll execute the appropriate operations

Example prompts:

  • "Mail merge for bulk letters/contracts"
  • "Generate personalized reports from data"
  • "Create certificates from templates"
  • "Auto-fill forms with user data"

Domain Knowledge

Template Syntax (Jinja2-based)

{{ variable }}           - Simple substitution{% for item in list %}   - Loop{% if condition %}       - Conditional{{ date | format_date }} - Filter

Word Template Example

python
from docxtpl import DocxTemplate
# Create template with placeholders:# Dear {{ name }},# Thank you for your order #{{ order_id }}...
def fill_template(template_path: str, data: dict, output_path: str):    doc = DocxTemplate(template_path)    doc.render(data)    doc.save(output_path)    return output_path
# Usagefill_template(    "templates/order_confirmation.docx",    {        "name": "John Smith",        "order_id": "ORD-12345",        "items": [            {"name": "Product A", "qty": 2, "price": 29.99},            {"name": "Product B", "qty": 1, "price": 49.99}        ],        "total": 109.97    },    "output/confirmation_john.docx")

Excel Template

python
from openpyxl import load_workbookimport re
def fill_excel_template(template_path: str, data: dict, output_path: str):    wb = load_workbook(template_path)    ws = wb.active        # Find and replace placeholders like {{name}}    for row in ws.iter_rows():        for cell in row:            if cell.value and isinstance(cell.value, str):                for key, value in data.items():                    placeholder = "{{" + key + "}}"                    if placeholder in cell.value:                        cell.value = cell.value.replace(placeholder, str(value))        wb.save(output_path)    return output_path

Bulk Generation (Mail Merge)

python
import csvfrom pathlib import Path
def mail_merge(template_path: str, data_csv: str, output_dir: str):    """Generate documents for each row in CSV."""        Path(output_dir).mkdir(exist_ok=True)        with open(data_csv) as f:        reader = csv.DictReader(f)                for i, row in enumerate(reader):            output_path = f"{output_dir}/document_{i+1}.docx"            fill_template(template_path, row, output_path)            print(f"Generated: {output_path}")
# Usage with contacts.csv:# name,email,company# John,[email protected],Acme# Jane,[email protected],Corp
mail_merge(    "templates/welcome_letter.docx",    "data/contacts.csv",    "output/letters")

Advanced: Conditional Content

python
from docxtpl import DocxTemplate
# Template with conditionals:# {% if vip %}# Thank you for being a VIP member!# {% else %}# Thank you for your purchase.# {% endif %}
doc = DocxTemplate("template.docx")doc.render({    "name": "John",    "vip": True,    "discount": 20})doc.save("output.docx")

Best Practices

  1. Use clear placeholder naming ({{client_name}})
  2. Validate data before rendering
  3. Handle missing data gracefully
  4. Keep templates version-controlled

Installation

bash
# Install required dependenciespip install python-docx openpyxl python-pptx reportlab jinja2

Resources

來源與署名

來源:claude-office-skills/skills位於template-engine提交9c4c7d5

授權條款: MIT

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架