TianshangScribe
TianshangScribe
面向开发者、CLI 自动化和 AI 代理的跨平台 Office 文档处理工具。可创建、编辑、模板填充和转换 Word(.docx)、Excel(.xlsx)和 PowerPoint(.pptx)文档,支持 LaTeX 风格标记、原生 OMML 数学公式以及模板引擎({{placeholders}}、{{#each}} 循环、{{#if}} 条件)。附带一个 MCP 服务器,提供 7 个工具(create、edit、fill template、convert、extract、validate、compare),支持 stdio、SSE 和 Streamable HTTP 传输,并带有 bearer-token 认证和速率限制。
警告:API 不稳定——可能发生破坏性变更
本项目处于 1.0 之前(0.x)阶段。CLI 选项、MCP 工具签名、模板语法和输出格式尚未冻结,可能随时变更,恕不另行通知。 兼容性承诺:任何破坏性变更将至少提前一个版本在 CHANGELOG 中公布,并附有迁移指南。 生产环境使用时,请锁定特定版本,并在升级前查看 CHANGELOG。
安装
pip install tianshang-scribe
# Or from source:
git clone https://github.com/Tianshang301/TianshangScribe.git
cd TianshangScribe
pip install -e ".[dev]"Linux 部署
Docker(推荐用于通过 Streamable HTTP 运行 MCP 服务器):
git clone https://github.com/Tianshang301/TianshangScribe.git
cd TianshangScribe
docker compose up -d
# Streamable HTTP MCP Server at http://localhost:8080/mcp
# (override transport / auth / rate limits via TIANSHANG_SCRIBE_* env vars).deb 包(Debian / Ubuntu):
# Download from GitHub Releases
sudo dpkg -i tianshang-scribe_0.7.1_all.deb
tianshang-scribe --helppipx(隔离的 CLI):
pipx install tianshang-scribe
tianshang-scribe --help需要 Python 3.10+ · python-docx · openpyxl · python-pptx · typer · rich · lxml
Related MCP server: docx-forge-mcp
快速开始
# Create a Word document
tianshang-scribe -w --create -a "Hello World" -o hello.docx
# Replace text (--regex for regex mode)
tianshang-scribe input.docx -r "old" --replace-new "new" -o output.docx
# LaTeX markup with nesting
tianshang-scribe -w --create --latex-style \
-s "font=Times New Roman,size=14" \
-a "\bfseries{\itshape{bold italic}} \fontsize{24}{Heading} \color{FF0000}{red}" \
-o styled.docx
# Math formulas —auto-converted to native Word OMML
tianshang-scribe -w --create \
--math "x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}" \
--math "\sum_{i=0}^{n} i^2" \
-o formulas.docx
# Template filling (JSON / CSV / YAML →{{placeholder}})
tianshang-scribe template.docx -t data.json -o filled.docx
# Convert to PDF (office2pdf ~2MB, or LibreOffice fallback)
tianshang-scribe input.docx --topdf -o output.pdf
# MCP Server —stdio mode (Claude Code / Cursor)
python -m tianshang_scribe.mcp.server
# MCP Server —SSE mode (Dify / Coze / FastGPT)
python -m tianshang_scribe.mcp.server --transport sse --port 8080
# Excel: import CSV, sort, export JSON
tianshang-scribe -e --create --from-csv data.csv --sort "A1:A10 asc" --to-json -o out.json
# Excel: add formula, protect workbook
tianshang-scribe budget.xlsx --formula "B10 =SUM(B2:B9)" --protect "p@ss" -o protected.xlsx全局选项
参数 | 说明 |
| 输入文档路径(使用 |
| 处理 Word 文档 |
| 处理 Excel 工作簿 |
| 处理 PowerPoint 演示文稿 |
| 输出文件路径 |
| 允许覆盖已有文件 |
| 输出为 PDF |
| 从标准输入读取 |
| 写入标准输出 |
当省略 -w/-e/-p 时,将根据输入文件的扩展名推断文档类型。
操作
选项 | 说明 | 示例 |
| 创建空白文档 |
|
| 添加文本 |
|
|
|
|
| 查找并替换 |
|
| 删除内容 |
|
| 清除内容 / 格式 / 链接 |
|
| 修改内容 |
|
| 设置样式 |
|
| 模板填充 |
|
| 提取数据( |
|
| 设置属性 |
|
| 启用 LaTeX 解析 | |
| 添加数学公式(Word) |
|
| 数学解析方言(office/mathtype) |
|
| OMML 数学字体(默认 Cambria Math) |
|
| 以 MathType OLE 对象(MTEF)嵌入 |
|
| 添加标题(Word) |
|
| 正则表达式模式 | 与 |
| 合并文件 |
|
| 拆分文档(仅 Excel: |
|
| 添加批注(Word)/ 演讲者备注(PPT) |
|
| 添加表格(Word) |
|
| 添加图表(Excel) |
|
| 批处理模式 |
|
| 批处理的 glob 模式 |
|
| 计划任务 SQLite 数据库路径 |
|
| 注册计划任务 |
|
| 移除计划任务 |
|
| 列出计划任务 |
|
| 立即运行计划任务 |
|
| 运行到期的计划任务 |
|
| 在沙箱中运行脚本 |
|
| 从标准输入读取 | |
| 写入标准输出 |
Word 专用选项
选项 | 说明 | 示例 |
| 添加标题 |
|
| 添加数学公式 |
|
| 启用 LaTeX 标记 | |
| 生成目录 |
|
| 插入分节符 |
|
| 设置页眉 |
|
| 设置页脚 |
|
| 文字水印 |
|
| 转换为 Markdown |
|
| 转换为 HTML |
|
Excel 专用选项
选项 | 说明 | 示例 |
| 添加工作表 |
|
| 删除工作表 |
|
| 重命名工作表 |
|
| 设置列宽 |
|
| 设置行高 |
|
| 设置单元格公式 |
|
| 导入 CSV 数据 |
|
| 排序区域 |
|
| 添加图表 |
|
| 设置密码 |
|
| 移除密码 |
|
| 导出为 CSV | |
| 导出为 JSON | |
| 导出为 HTML |
LaTeX 风格标记
在 --add 内容中嵌入以下标记。使用 --latex-style 启用。支持嵌套。
语法 | 效果 |
| 粗体 |
| 斜体 |
| 小型大写字母 |
| 下划线 |
| 罗马体(衬线) |
| 无衬线体 |
| 等宽体 |
| 指定字体 |
| 字号(磅) |
| 颜色(十六进制) |
| 居中对齐 *— |
| 左对齐 *— |
| 右对齐 *— |
| 行距 *— |
| 缩进 *— |
| 插入标题 |
| 分页 |
| 插入图片 |
*— 段落级格式(创建新段落)。
字体配置
命令 | 效果 |
| 默认西文字体 |
| 默认中文字体 |
| 无衬线字体 |
| 中文无衬线字体 |
| 等宽字体 |
| 中文等宽字体 |
Word OOXML 原生区分 w:ascii(西文)和 w:eastAsia(中文)字体,可在混合文字中自动切换字体。
数学公式
LaTeX 数学公式可通过 --math 转换为原生 Word OMML(Office Math Markup Language)格式。该转换器是一个手写的递归下降解析器(expression → term → factor → atom),作用于嵌套的不可变令牌树(fraction、root、N-ary、sub/sup、accent、styled、delimiter 令牌),并通过一张 O(1) 命令表进行分派,该命令表包含预编译的正则表达式和零拷贝参数切片。使用 --math-font "Times New Roman" 可将公式渲染为 MathType 风格的衬线字体,而不是 Word 默认的 Cambria Math(<m:mathPr><m:mathFont>)。--math-style mathtype 可切换 LaTeX 解析方言以获得 MathType 兼容。使用 --math-mtef 可将公式作为真正的 MathType OLE 对象(MTEF 二进制格式)嵌入——这可由旧版 MathType(6.x 及更早版本)编辑,也与 --extract math 读取回的格式相同。输出在所有版本之间保持字节级稳定(受 golden-snapshot 回归测试套件保护)。
支持语法
分类 | 命令 |
分数 |
|
根式 |
|
上标/下标 |
|
求和/积分 |
|
极限 |
|
具名函数 |
|
希腊字母 |
|
符号 |
|
关系 |
|
箭头 |
|
重音 |
|
括号 |
|
数学字体 |
|
数学排版
遵循主流数学期刊标准(AMS、Elsevier、Springer):
内容 | 样式 | 示例 |
单字母变量 | 斜体 |
|
数字 | 正体 |
|
具名函数 | 正体 |
|
小写希腊字母 | 斜体 |
|
大写希腊字母 | 正体 |
|
自动识别
--add 文本中的命令即使没有 $...$ 包裹也会自动识别为数学公式:
带参数:
\frac\sqrt\sum\int\prod\lim重音:
\hat{x}\bar{x}\vec{x}等一元运算符:
\sin\cos\tan\log\ln等纯文本中的
H_{2}O和m^{2}会变成 Unicode 下标/上标(H₂O / m²)
样式语法
--style 使用逗号分隔的键值对:
--style "font=Times New Roman,size=14,bold,italic,color=FF0000,align=center"键 | 别名 | 值 | 说明 |
|
| 字体名 | 西文字体 |
|
| 字体名 | CJK 字体 |
|
| pt | 字号 |
| flag | 粗体 | |
| flag | 斜体 | |
| flag | 下划线 | |
|
|
| 十六进制颜色 |
|
|
| 对齐方式 |
布尔键(bold italic underline)只要存在即为 True。
模板填充
支持 JSON、CSV 和 YAML 数据源。替换文档中的 {{placeholder}}。嵌套对象可用点号表示法展开。循环可迭代列表值。条件语句可显示/隐藏块。
{
"name": "John Doe",
"date": "2026-07-28",
"user": { "city": "Beijing" },
"show": true,
"paid": false,
"items": [
{ "product": "Widget", "price": "10" },
{ "product": "Gadget", "price": "20" }
]
}{{name}} → John Doe
{{user.city}} → Beijing
{{#each items}} → repeats the block for each item
{{product}}: {{price}}
{{/each}}
{{#if show}} → shown only when show is truthy
Confidential content
{{/if}}
{{#if role=admin}} → shown only when role equals "admin"
Admin dashboard
{{/if}}
{{#unless paid}} → shown only when paid is falsy
Payment required
{{/unless}}Excel 功能
功能名称 | CLI 选项 |
工作表管理 |
|
列/行调整 |
|
公式 |
|
数据导入 |
|
数据导出 |
|
排序 |
|
图表 |
|
保护 |
|
PPT 功能
功能名称 | 说明 |
幻灯片管理 | 添加、删除、重新排列幻灯片( |
版式 | 按名称或索引应用幻灯片版式( |
演讲者备注 | 添加演示者备注( |
数学公式 |
|
切换效果 | 设置幻灯片切换效果——淡入、推入、擦除等( |
导出 | 将幻灯片保存为图片( |
媒体压缩 | 压缩图片( |
保护 | 设置/移除密码( |
退出码
代码 | 含义 |
| 成功 |
| 一般错误 |
| 参数错误 |
| 未实现 |
MCP 服务器
天匠Scribe 包含 MCP(Model Context Protocol)服务器——AI 智能体可以创建、编辑、填充模板、转换,以及从 Office 文档中提取数据。
Quick Connect
stdio(Claude Code, Cursor):
{"mcpServers": {"tianshang-scribe": {
"command": "python", "args": ["-m", "tianshang_scribe.mcp.server"]
}}}SSE(Dify, Coze, FastGPT):
python -m tianshang_scribe.mcp.server --transport sse --host 0.0.0.0 --port 8080{"mcpServers": {"tianshang-scribe": {
"url": "http://localhost:8080/sse", "transport": "sse"
}}}Tools (7)
Tool | Description |
| 使用结构化内容块创建 .docx / .xlsx / .pptx |
| 对现有文档执行替换、删除、修改、样式和添加操作 |
| 用数据填充 |
| 在格式之间转换(docx↔pdf/md/html,xlsx↔csv/json) |
| 提取元数据、全文或文档结构 |
| 在填充前预检模板占位符与数据是否匹配 |
| 比较两个 .docx 文件的段落级差异 |
Capabilities
Feature | Detail |
协议 | MCP 2024-11-05 · stdio + SSE · JSON-RPC 2.0 |
资源 |
|
提示词 | 5 个内置工作流模板( |
进度 | 在 PDF 转换和长操作期间发送 |
响应 | 多类型 |
模式 | 所有参数均支持 |
生产环境(仅 SSE)
# With authentication
TIANSHANG_SCRIBE_AUTH_TOKEN="secret" \
python -m tianshang_scribe.mcp.server --transport sse --host 0.0.0.0 --port 8080
# Health check
curl http://localhost:8080/health
# {"status":"ok","version":"0.7.1","uptime_seconds":3600,"active_sessions":3,"tools_available":7}
# CORS whitelist
python -m tianshang_scribe.mcp.server --transport sse --cors-origins "https://coze.com,https://dify.ai"端点:GET /health · GET /sse · POST /message?session_id=X
完整文档:docs/mcp/README.md。
python tests/integration/mcp/mcp_stdio_smoke.py # 9/9 quick tests (stdio)
python tests/integration/mcp/test_sse.py # 3/3 SSE transport tests
python tests/integration/mcp/mcp_agent_sim.py # 11-scenario Agent simulation架构
src/
└── tianshang_scribe/ # importable package (tianshang_scribe.*)
├── cli/ # Typer CLI entry
│ ├── main.py # Command parsing & dispatch
│ └── global_opts.py # File path / type inference
├── core/ # Document engine abstraction
│ ├── document.py # DocumentABC unified interface
│ ├── word_engine.py # Word engine (python-docx)
│ ├── excel_engine.py# Excel engine (openpyxl)
│ └── ppt_engine.py # PPT engine (python-pptx)
├── rendering/ # Style & formula rendering
│ ├── styles.py # TextStyle dataclass
│ ├── latex_parser.py # LaTeX markup parser
│ ├── math_omml.py # LaTeX →OMML math converter
│ └── template.py # Template filling engine
├── transform/ # Format conversion
│ └── pdf.py # PDF export (office2pdf + LibreOffice)
├── mcp/ # MCP Server (official mcp SDK 2.x)
│ ├── server.py # build_server + entry (stdio / SSE / Streamable HTTP)
│ ├── transport.py # transport wiring + ASGI middleware
│ ├── schemas.py # pydantic models + as_dict
│ ├── auth.py # Bearer token auth
│ ├── rate_limit.py # token bucket rate limiting
│ ├── metrics.py # Prometheus-style metrics
│ ├── security.py # read-only / destructive classification
│ ├── prompts.py # 5 prompt workflows
│ ├── tools/ # 7 Agent tools
│ │ ├── _registry.py # tool registry (schemas auto-derived)
│ │ ├── create.py / edit.py / template.py / convert.py
│ │ ├── validate.py / compare.py
│ └── errors.py # structured error codes + fixes
└── utils/ # Utility functions
└── file_utils.py技术栈
组件 | 技术 |
CLI | Typer + Rich |
Word | python-docx |
Excel | openpyxl |
PPT | python-pptx |
数学 | 手写递归下降解析器 → OMML XML(不可变令牌树、命令分派表) |
模板 | 自定义引擎({{placeholder}}, {{#each}}, {{#if}}) |
office2pdf(约 2MB Rust 二进制,零依赖)+ LibreOffice 后备方案 | |
质量 | pytest(936 项测试)· ruff · mypy |
构建 EXE
pip install pyinstaller
pyinstaller --onefile --name tianshang-scribe --hidden-import openpyxl.cell._writer --hidden-import openpyxl.cell.read_only --hidden-import openpyxl.styles --hidden-import openpyxl.chart --hidden-import openpyxl.comments src/tianshang_scribe/cli/main.py
# dist/tianshang-scribe.exe (~35 MB)演示
python -m demo.generate_demos
# demo/demo_word.docx —LaTeX + math + TOC + watermark
# demo/demo_excel.xlsx —CSV import + formulas + chart + protection
# demo/demo_ppt.pptx —slides + notes + transitions + math formulasCLI 合规性测试:
python demo/test_cli.py开发
git clone https://github.com/Tianshang301/TianshangScribe.git
cd TianshangScribe
pip install -e ".[dev]"
pytest tests/ -v # Run tests
ruff check src/tianshang_scribe/ tests/ # Lint
mypy src/tianshang_scribe/ # Type check许可证
Apache-2.0
Maintenance
Related MCP Servers
- AlicenseBqualityDmaintenanceA universal MCP server for document processing, conversion, and automation. Handle PDF, DOCX, HTML, Markdown, and more through a unified API and toolset.1333139MIT
- AlicenseAqualityDmaintenanceMCP server for Word document (.docx) creation and manipulation — the production-grade document automation tool for AI agents.938MIT
- AlicenseAqualityBmaintenanceMCP server for reading, writing, editing, formatting, and exporting Microsoft Office documents (Word, Excel, PowerPoint) via stdio JSON-RPC, with 47 tools and cross-platform support.47MIT
- AlicenseCqualityDmaintenanceA unified MCP server for document processing that enables creating, editing, and converting Word documents (DOCX), PDFs, Markdown, and images, with support for templates, formatting, and batch operations.100MIT
Related MCP Connectors
Generate PDF/DOCX/XLSX/PPTX from templates+JSON. Convert Office/HTML/MD to PDF. Universal templating
Use your own Word templates to convert Markdown → DOCX/PDF/HTML from any MCP-compatible AI.
Markdown in, any format out. PDFs merged, split, watermarked. Runs on our own doc engines.
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/Tianshang301/TianshangScribe'
If you have feedback or need assistance with the MCP directory API, please join our Discord server