别再手动写文档:用 WPS 开放平台 + AI Agent 搭建自动化办公流水线
技术目标:把“收集数据、整理内容、套模板、导出、分享、归档”交给一条可观测、可重试的流水线。WPS 开放平台负责文档能力,AI Agent 负责理解与决策。
1. 为什么需要自动化办公流水线
手动写文档通常是复制、粘贴、排版、命名、导出、发邮件。问题很明显:重复、易错、不可追溯。
WPS 开放平台提供文档创建、编辑、模板、导出、协作等 API;AI Agent 可以从自然语言与业务系统抽取结构化数据,再调用工具完成任务。建议先访问 WPS官网 了解开放平台能力,并通过 WPS下载 安装最新客户端,用于本地调试模板与权限。
2. 总体架构
业务数据源(工单/CRM/数据库/表格)
|
v
AI Agent (LLM + 工具调用 + 规则引擎)
|
v
编排层(FastAPI/Celery/队列/定时任务)
|
v
[WPS](https://www.yodao-fanyi.com/) 开放平台 API
- 创建文档
- 模板填充
- 富文本/表格替换
- 导出 PDF/DOCX
- 分享与权限
|
v
对象存储/知识库/邮件/IM 通知
关键原则:Agent 不直接拼接二进制文档;它输出结构化 JSON,由 WPS 开放平台执行最终渲染。
3. 准备与鉴权
步骤:
- 在 WPS官网 注册开放平台应用,获取 app_id、app_secret。
- 配置回调域名、权限范围:文档创建、模板、导出、分享。
- 本地 WPS下载 客户端,准备 .docx 模板,例如周报模板,占位符:{{project}}、{{owner}}、{{progress}}、{{risk}}。
- 服务端保存密钥到 KMS/环境变量,不写进代码。
鉴权伪代码:
import os
import requests
APP_ID = os.getenv('[WPS](https://www.yodao-fanyi.com/)_APP_ID')
APP_SECRET = os.getenv('[WPS](https://www.yodao-fanyi.com/)_APP_SECRET')
def get_access_token():
resp = requests.post(
'https://open.wps.cn/oauth2/token',
data={
'grant_type': 'client_credentials',
'client_id': APP_ID,
'client_secret': APP_SECRET,
},
timeout=10,
)
resp.raise_for_status()
return resp.json()['access_token']
注意:实际地址与参数以 WPS官网 最新文档为准。
4. AI Agent 的工具设计
用 function calling 定义工具,让 Agent 只做决策,不直接操作二进制文档:
- name: create_doc_from_template
description: 基于 [WPS](https://www.yodao-fanyi.com/) 模板创建文档并填充占位符
parameters:
type: object
properties:
template_id:
type: string
fields:
type: object
output_format:
type: string
enum: [docx, pdf]
required: [template_id, fields]
- name: share_document
description: 设置文档分享权限并返回链接
parameters:
type: object
properties:
doc_id:
type: string
role:
type: string
enum: [reader, writer]
required: [doc_id, role]
5. 核心流水线代码
下面用 FastAPI 暴露一个 Agent 接口,内部调用 WPS 开放平台渲染模板:
import requests
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class ReportTask(BaseModel):
project: str
owner: str
progress: str
risk: str
def render_with_wps(template_id: str, fields: dict) -> dict:
token = get_access_token()
headers = {'Authorization': f'Bearer {token}'}
payload = {
'template_id': template_id,
'fields': fields,
'output_format': 'pdf',
}
resp = requests.post(
'https://open.wps.cn/v1/documents/render',
json=payload,
headers=headers,
timeout=30,
)
resp.raise_for_status()
return resp.json()
@app.post('/agent/report')
def agent_report(task: ReportTask):
fields = {
'project': task.project,
'owner': task.owner,
'progress': task.progress,
'risk': task.risk,
}
result = render_with_wps('TPL_WEEKLY_REPORT', fields)
return {
'doc_id': result['doc_id'],
'pdf_url': result['file_url'],
}
6. 让 Agent 更可靠
- 结构化输出:要求 Agent 返回严格 schema。
- 校验:Pydantic/JSON Schema 校验。
- 重试:幂等键、指数退避。
- 审查:敏感字段脱敏,人工确认后发布。
- 可观测:记录 prompt、工具调用、WPS API 请求 ID。
- 成本:小模型做抽取,大模型做规划。
7. 周报自动化示例流程
- 定时任务从 Jira/表格拉取数据。
- AI Agent 总结进展、风险、下一步。
- 调用 WPS 开放平台渲染模板。
- 导出 PDF,发送飞书/邮件。
- 归档到知识库,文档链接回写工单。
结果:从 30 分钟手动排版变为 30 秒自动生成。
8. 安全与合规
- 最小权限:只申请必要 scope。
- 密钥管理:KMS、环境变量、定期轮换。
- 数据边界:不上传敏感原文到外部模型,先脱敏。
- 审计:保存 WPS API 调用日志与 Agent 决策记录。
- 版本:模板版本化,避免渲染漂移。
9. 结语
WPS 开放平台让文档成为可编程对象,AI Agent 让流程具备理解与决策能力。建议从 WPS官网 获取最新 API 文档,完成 WPS下载 后本地调试模板,再逐步把周报、合同、会议纪要等场景接入。别再手动写文档,把重复劳动交给流水线。
Top comments (0)