Skip to content
Automation
Skill

/wps-template-engine

文档模板批量填充。在Word模板里写{{姓名}}{{金额}}这样的变量标记, 配合Excel/CSV数据源一键批量生成。支持条件判断、循环、金额大写转换,比邮件合并更强大。 用于帮助用户构建文档模板体系。当用户提到模板、变量、占位符、批量填充时触发。 Document template engine with {{variable}} placeholders and batch rendering.

From plugin
bwkyd-wps-skills
842 skills
Install
$ npx -y skills add Bwkyd/wps-skills --skill wps-template-engine --agent claude-code

How it fires

How this skill gets triggered: by you, by Claude, or both.

  • Fires itselfAuto-invocation. Claude auto-loads it when your prompt matches the work.Auto-invocation is when the right skill fires by itself at the right moment, driven by a FLOW.md router and a hook, instead of you invoking it by name. It is the difference between a skill being installed and a skill actually getting used.Read the full definition →
  • You can call itInvoke it directly when you want it.
  • Slash command/wps-template-engine

Context preview

The summary Claude sees to decide when to auto-load this skill.

文档模板批量填充。在Word模板里写{{姓名}}{{金额}}这样的变量标记, 配合Excel/CSV数据源一键批量生成。支持条件判断、循环、金额大写转换,比邮件合并更强大。 用于帮助用户构建文档模板体系。当用户提到模板、变量、占位符、批量填充时触发。 Document template engine with {{variable}} placeholders and batch rendering.

SKILL.md

wps-template-engine.SKILL.md
name: wps-template-engine
description: |
  文档模板批量填充。在Word模板里写{{姓名}}{{金额}}这样的变量标记,
  配合Excel/CSV数据源一键批量生成。支持条件判断、循环、金额大写转换,比邮件合并更强大。
  用于帮助用户构建文档模板体系。当用户提到模板、变量、占位符、批量填充时触发。
  Document template engine with {{variable}} placeholders and batch rendering.
license: MIT
user-invocable: true
argument-hint: '[模板需求描述]'
allowed-tools: 'Read, Grep, Glob, Bash, Write, Edit'
metadata:
  author: BWKYD
  title: 模板批量填充
  description_zh: 在Word模板中设置变量,配合Excel数据批量生成文档
  tags:
    - 模板
    - 自动化
    - python-docx
    - 变量
    - WPS
  version: 1.0.1
  license: MIT

文档模板引擎

创建带 `{{变量}}` 的 Word 模板 → 从数据源自动填充 → 批量生成文档。

> 邮件合并的**升级版**:支持条件逻辑、循环、表格动态行、嵌套变量。

When to Use

  • 需要创建可复用的文档模板
  • 需要从数据源批量填充文档
  • 需要比邮件合并更强大的模板功能
  • 用户说"帮我做一个模板""文档自动化"

When NOT to Use

  • 简单的邮件合并(无高级功能) → 使用 `wps-mail-merge`
  • 一次性文档生成 → 使用 `wps-docx-writer`

模板语法

基础变量

{{变量名}}           → 替换为数据值
{{公司名称}}         → "XX科技有限公司"
{{合同金额}}         → "100,000.00"

日期/格式化

{{今日日期}}         → 自动填充当前日期
{{日期|YYYY年M月D日}} → 格式化日期
{{金额|大写}}        → 自动转金额大写
{{金额|千分位}}      → 添加千位分隔符

条件逻辑

{{#if 性别=男}}先生{{/if}}
{{#if 性别=女}}女士{{/if}}
{{#if 金额>10000}}需要总经理审批{{/if}}
{{#if 部门=技术部}}技术考核标准如下...{{/if}}

循环(表格动态行)

| 序号 | 品名 | 数量 | 单价 | 金额 |
{{#each 商品列表}}
| {{序号}} | {{品名}} | {{数量}} | {{单价}} | {{小计}} |
{{/each}}
| 合计 |  |  |  | {{总金额}} |

内置变量

{{__TODAY__}}        → 当前日期 YYYY-MM-DD
{{__TODAY_CN__}}     → 当前日期 YYYY年M月D日
{{__NOW__}}          → 当前时间 HH:MM
{{__INDEX__}}        → 当前记录序号(批量生成时)
{{__TOTAL__}}        → 总记录数

工作流程

Step 1: 决定路径

用户需要什么?
│
├─ 创建新模板 → Step 2A: 设计模板
│
├─ 用已有模板填充 → Step 2B: 数据填充
│
└─ 两者都要 → 先2A再2B

Step 2A: 设计模板

1. 理解用户的文档类型和变量需求 2. 设计模板结构和变量标记 3. 生成带 `{{变量}}` 的 .docx 模板文件 4. 生成配套的数据模板(.xlsx),列名对应变量名

Step 2B: 数据填充

from docx import Document
from openpyxl import load_workbook
import re
import os
import csv
from datetime import datetime

class TemplateEngine:
    """文档模板引擎"""

    BUILTIN_VARS = {
        '__TODAY__': lambda: datetime.now().strftime('%Y-%m-%d'),
        '__TODAY_CN__': lambda: datetime.now().strftime('%Y年%m月%d日'),
        '__NOW__': lambda: datetime.now().strftime('%H:%M'),
    }

    def __init__(self, template_path):
        self.template_path = template_path
        self.vars_found = set()
        self._scan_variables()

    def _scan_variables(self):
        """扫描模板中的所有变量"""
        doc = Document(self.template_path)
        for para in doc.paragraphs:
            self.vars_found.update(re.findall(r'\{\{(.+?)\}\}', para.text))
        for table in doc.tables:
            for row in table.rows:
                for cell in row.cells:
                    for para in cell.paragraphs:
                        self.vars_found.update(
                            re.findall(r'\{\{(.+?)\}\}', para.text))

    def get_variables(self):
        """返回模板中使用的变量列表"""
        return sorted(self.vars_found)

    def render(self, data, output_path):
        """用数据渲染模板"""
        doc = Document(self.template_path)

        # 注入内置变量
        for key, func in self.BUILTIN_VARS.items():
            data.setdefault(key, func())

        # 格式化处理
        processed = {}
        for key, value in data.items():
            processed[key] = str(value) if value is not None else ''
            # 金额大写
            if key + '|大写' in str(self.vars_found):
                processed[key + '|大写'] = self._amount_to_cn(value)
            # 千分位
            if key + '|千分位' in str(self.vars_found):
                try:
                    processed[key + '|千分位'] = f'{float(value):,.2f}'
                except (ValueError, TypeError):
                    processed[key + '|千分位'] = str(value)

        # 替换段落
        for para in doc.paragraphs:
            self._replace_in_paragraph(para, processed)

        # 替换表格
        for table in doc.tables:
            for row in table.rows:
                for cell in row.cells:
                    for para in cell.paragraphs:
                        self._replace_in_paragraph(para, processed)

        doc.save(output_path)
        return os.path.abspath(output_path)

    def _replace_in_paragraph(self, para, data):
        """替换段落中的变量(保持格式)"""
        full_text = para.text
        if '{{' not in full_text:
            return

        for var_name, value in data.items():
            placeholder = '{{' + var_name + '}}'
            if placeholder in full_text:
                for run in para.runs:
                    if placeholder in run.text:
                        run.text = run.text.replace(placeholder, value)
                full_text = para.text

    def batch_render(self, records, output_dir,
                     filename_pattern='doc_{{__INDEX__}}'):
        """批量渲染"""
        os.makedirs(output_dir, exist_ok=True)
        generated = []

        for i, record in enumerate(records):
            record['__INDEX__'] = str(i + 1)
            record['__TOTAL__'] = str(len(records))

            fname = filename_pattern
            for key, value in record.items():
                fname = fname.replace('{{' + key + '}}', str(value))
            fname = re.sub(r'[\\/:*?"<>|]', '_', fname)

            output_path = os.path.join(output_dir, f'{fname}.docx')
            self.render(record, output_path)
            generated.append(output_path)

        return generated

    @staticmethod
    def _amount_to_cn(amount):
        """金额转中文大写"""
        try:
            num = float(amount)
        except (ValueError, TypeError):
            return str(amount)
        units = ['', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿']
        digits = '零壹贰叁肆伍陆柒捌玖'
        integer = int(abs(num))
        decimal = round(abs(num) - integer, 2)

        if integer == 0:
            result = '零元'
        else:
            s = str(integer)
            result = ''
            for i, d in enumerate(reversed(s)):
                if int(d) != 0:
                    result = digits[int(d)] + units[i] + result
                else:
Read more
Ships withbwkyd-wps-skills

42 个 WPS Office 办公自动化 Claude Code Skills 集合 一句话指令,让 Claude 帮你自动生成 Word / Excel / PPT / PDF 文档。

Get the whole plugin

Other skills on bwkyd-wps-skills.

wps-attendance
Skill

wps-attendance

考勤打卡统计。把考勤机导出的打卡数据丢进来,自动统计每个人的 迟到、早退、缺勤、加班时长,生成月度考勤汇总表,异常考勤自动标红。 用于帮助HR处理考勤数据。当用户提到考勤、打卡、迟到、加班统计时触发。 Attendance tracker - generates summary reports from…

wps-batch-convert
Skill

wps-batch-convert

文档批量格式转换。把一个文件夹里的Word全转文本、Excel全导出CSV、 Markdown转Word,支持docx/txt/xlsx/csv/md等格式批量互转,一次搞定。 用于帮助用户批量转换文档格式。当用户提到批量转换、格式转换、导出时触发。 Batch converts documents between…

wps-budget
Skill

wps-budget

预算表一键生成。部门年度预算、项目预算、活动经费预算,选个模板填数字就行, 小计合计公式自动写好,还有预算vs实际对比和执行率自动计算。 用于帮助财务和行政制作预算表。当用户提到预算、费用表、经费时触发。 Budget spreadsheet generator with formulas and variance…

wps-certificate
Skill

wps-certificate

证书奖状批量生成。给一份名单,批量生成荣誉证书、结业证书、聘书、感谢信, 每人一份独立文件,年终评优、培训结业、表彰活动必备工具。 用于帮助用户批量生成证书。当用户提到证书、奖状、聘书时触发。 Batch certificate/award generator from a list of recipients.

wps-chart
Skill

wps-chart

数据图表一键生成。不知道数据该用什么图表?告诉我你的数据和目的, 自动推荐最佳图表类型并生成,柱状图折线图饼图散点图都支持,还帮你调配色。 用于帮助用户制作数据可视化图表。当用户提到图表、柱状图、折线图、饼图时触发。 Chart generator - recommends best chart type and…

wps-cn-calendar
Skill

wps-cn-calendar

日历排班值班表。生成带法定节假日和调休标注的月度/年度日历表, 还能做三班倒/两班倒排班表和节假日值班安排,颜色区分不同班次一目了然。 用于帮助用户生成日历和排班表。当用户提到日历、排班、值班、节假日时触发。 Chinese calendar with holidays, shift scheduling, and…