SuperAPI AI Directory

lark-workflow-doc-template-engine

Office & productivity

View repository

Updated 10-16 08:52

About this skill

Document Template Engine Workflow

CRITICAL — Before starting, you MUST first use the Read tool to read lark-shared/SKILL.md, which contains authentication, permission handling, and security rules.

Applicable Scenarios

  • "Create documents using the XX template" / "Generate reports in batches based on a template"
  • "Fill the data from the Base into the document template" / "Data-driven document generation"
  • "Generate contracts/notifications/reports in batches" / "Template engine"
  • "Generate a document for each row of this table" / "mail merge"
  • "Fill documents with data from a Sheet" / "Doc template engine"

Connects Feishu's "data layer" (Base/Sheets) and "document layer" (Docs) to enable data-driven automated document generation. Typical scenarios: customer reports, project notifications, contract drafts, and other documents that need to be generated in batches.

Prerequisites

Supports user identity. Before execution, ensure authorization has been granted:

lark-cli auth login --domain doc,base,drive

Workflow

用户指定模板 ──► docs +fetch(读取模板内容)
                       │
                       ▼
              识别变量占位符 {{xxx}}
                       │
                       ▼
用户指定数据源 ──► base +record-list / sheets
                       │
                       ▼
               字段 ↔ 变量 映射
                       │
                       ▼
              ┌────────┼────────┐
              ▼        ▼        ▼
          单条生成  批量生成  预览模式
                       │
                       ▼
          docs +create(生成文档) / 预览输出

Step 1: Read the template document

Refer to lark-doc/SKILL.md。

# 读取模板文档内容
lark-cli docs +fetch --doc "<模板doc_token>" --format markdown

Identify variable placeholders from the template content. Supported placeholder formats:

  • {{变量名}} — Standard format (recommended)
  • {变量名} — Simplified format
  • 【变量名】 — Chinese format

Example template:

# {{项目名称}} 项目周报

## 基本信息
- 项目经理:{{负责人}}
- 报告周期:{{开始日期}} ~ {{结束日期}}
- 项目状态:{{状态}}

## 本周进展
{{本周完成内容}}

## 下周计划
{{下周计划内容}}

Step 2: Obtain the data source

Method A — Base:

Refer to lark-base/SKILL.md。

# 获取表字段结构
lark-cli base +field-list --base-token "<base_token>" --table-id "<table_id>" --format json

# 获取记录数据
lark-cli base +record-list --base-token "<base_token>" --table-id "<table_id>" \
  --page-size 100 --format json

# 按关键词搜索特定记录(v1.0.8+,适合快速定位模板对应的数据)
lark-cli base +record-search --base-token "<base_token>" --table-id "<table_id>" \
  --query "<关键词>" --format json

Method B — Sheets:

Refer to lark-sheets/SKILL.md。

# 读取电子表格数据(第一行为表头)
lark-cli sheets spreadsheets values get --params '{"spreadsheetToken":"<token>","range":"Sheet1!A1:Z100"}' --format json

Method C — Data provided directly by the user (single record):

The user provides variable values verbally or in JSON form, and you proceed directly to Step 4.

Step 3: Variable mapping

Display the mapping relationship between template variables and data source fields for the user to confirm:

## 变量映射确认

| 模板变量 | 数据源字段 | 示例值 |
|---------|-----------|--------|
| {{项目名称}} | 字段"项目名" | XX 系统开发 |
| {{负责人}} | 字段"PM" | 张三 |
| {{状态}} | 字段"当前状态" | 进行中 |
| {{本周完成内容}} | ⚠️ 未匹配 | — |

⚠️ 有 1 个变量未自动匹配。请指定:
- {{本周完成内容}} 对应哪个字段?(或手动输入固定值)

Automatic mapping rules:

  1. Exact match: variable name == field name
  2. Fuzzy match: variable name contains field name or field name contains variable name
  3. Unmatched: mark with ⚠️ and wait for the user to specify

Step 4: Generate documents

⚠️ Security rule: Before batch generation, the user must confirm the mapping and the number of documents to generate.

Single generation:

# 替换变量后创建文档
lark-cli docs +create --title "<替换后的标题>" --markdown "<替换后的内容>"

Batch generation:

# 逐条创建(串行,每条间隔 1 秒)
# 对数据源中的每一行:
#   1. 用该行数据替换模板中的所有变量
#   2. 创建文档
lark-cli docs +create --title "<按行替换的标题>" --markdown "<按行替换的内容>"

Batch security:

  • Maximum per batch: 20 records(to avoid creating too many documents)
  • If more than 20 records, prompt the user to execute in batches
  • Provide a preview mode (only display the substitution results, do not create documents)

Presentation generation (v1.0.9+, optional):

When the user needs to generate a PPT instead of a document:

# 一步创建演示文稿(v1.0.9+)
lark-cli slides +create --slides "<Markdown 内容>"

Tip:slides +create Returns the presentation URL, which can be linked directly in the report.

Writing images to Sheets (v1.0.9+, optional):

When the template needs to insert images into Sheets:

# 向电子表格写入图片(v1.0.7+)
lark-cli sheets +write-image --spreadsheet "<token>" --range "<A1>" --image "<image_path>"

Create document shortcut archiving (v1.0.10+, optional):

When documents generated in batches need to be archived centrally in the same directory:

# 为生成的文档创建快捷方式到指定目录(v1.0.10+)
lark-cli drive +create-shortcut --file-token "<doc_token>" --parent-token "<archive_folder_token>"

Tip:+create-shortcut You can create reference links in an archive directory without moving the original documents, making it convenient to centrally manage batch-generated documents.

Preview mode:

## 预览:第 1 条文档

# XX 系统开发 项目周报

## 基本信息
- 项目经理:张三
- 报告周期:2026-03-25 ~ 2026-03-31
...

---
确认生成?(共 {N} 条,输入"确认"执行)

Step 5: Output execution report

## 文档生成报告

### 模板信息
- **模板文档**:[{标题}]({链接})
- **数据源**:{Base/Sheets 名称}(共 {N} 条记录)
- **变量**:{M} 个

### 生成结果
| # | 文档标题 | 状态 | 链接 |
|---|---------|------|------|
| 1 | XX 系统开发 项目周报 | ✅ 已创建 | [查看]({链接}) |
| 2 | YY 平台优化 项目周报 | ✅ 已创建 | [查看]({链接}) |
| 3 | ZZ 数据迁移 项目周报 | ✅ 已创建 | [查看]({链接}) |

共生成 **{N}** 篇文档。

Fault tolerance mechanism

Exception scenario Handling method
No placeholders in the template Prompt "No variable placeholders detected" and guide the user to add them to the template
Variable does not match any field Mark with ⚠️, wait for the user to specify manually or use a fixed value
Missing field in a row of the data source Replace the missing field with "—", do not block
Batch exceeds 20 items Prompt to execute in batches
Document creation failed Record the failed items and continue creating the remaining documents
No permission for the template document Prompt the user to check the document sharing settings

Permission table

Step Command Required scope Mandatory
Read template docs +fetch docx:document:readonly ✅ Mandatory
Base data base +field-list / +record-list / +record-search bitable:bitable:readonly Optional (for Base data sources)
Sheets data sheets values get / +write-image sheets:spreadsheet:readonly Optional (for Sheets data sources)
Generate document docs +create docx:document:write ✅ Mandatory
Generate presentation slides +create slides:presentation:write Optional (in PPT mode)

Reference

  • lark-shared — Authentication, permissions (required reading)
  • lark-doc — +fetch、+create Detailed usage
  • lark-base — +field-list、+record-list、+record-search Detailed usage
  • lark-sheets — Spreadsheet reading,+write-image Detailed usage
  • lark-slides — +create Presentation creation