template-engine by claude-office-skills/skills
npx skills add https://github.com/claude-office-skills/skills --skill template-engine此技能支持基于模板的文档生成——使用占位符定义模板,然后自动用数据填充它们。适用于 Word、Excel、PowerPoint 等格式。
示例提示:
{{ variable }} - 简单替换
{% for item in list %} - 循环
{% if condition %} - 条件判断
{{ date | format_date }} - 过滤器
from docxtpl import DocxTemplate
# 创建包含占位符的模板:
# 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
# 用法
fill_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"
)
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
from openpyxl import load_workbook
import re
def fill_excel_template(template_path: str, data: dict, output_path: str):
wb = load_workbook(template_path)
ws = wb.active
# 查找并替换像 {{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
import csv
from pathlib import Path
def mail_merge(template_path: str, data_csv: str, output_dir: str):
"""为 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}")
# 使用 contacts.csv 的示例:
# name,email,company
# John,john@example.com,Acme
# Jane,jane@example.com,Corp
mail_merge(
"templates/welcome_letter.docx",
"data/contacts.csv",
"output/letters"
)
from docxtpl import DocxTemplate
# 包含条件判断的模板:
# {% 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")
# 安装所需的依赖项
pip install python-docx openpyxl python-pptx reportlab jinja2
每周安装次数
21
仓库
GitHub 星标数
5
首次出现
1 天前
安全审计
已安装于
claude-code19
opencode4
gemini-cli4
github-copilot4
codex4
amp4
This skill enables template-based document generation - define templates with placeholders, then automatically fill them with data. Works with Word, Excel, PowerPoint, and more.
Example prompts:
{{ variable }} - Simple substitution
{% for item in list %} - Loop
{% if condition %} - Conditional
{{ date | format_date }} - Filter
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
# Usage
fill_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"
)
from openpyxl import load_workbook
import 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
import csv
from 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,john@example.com,Acme
# Jane,jane@example.com,Corp
mail_merge(
"templates/welcome_letter.docx",
"data/contacts.csv",
"output/letters"
)
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")
# Install required dependencies
pip install python-docx openpyxl python-pptx reportlab jinja2
Weekly Installs
21
Repository
GitHub Stars
5
First Seen
1 day ago
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
claude-code19
opencode4
gemini-cli4
github-copilot4
codex4
amp4
GitHub Actions 官方文档查询助手 - 精准解答 CI/CD 工作流问题
22,500 周安装