DemoMacro/office-open
关于
Create Word, Excel, and PowerPoint files (.docx, .xlsx, .pptx) from plain JSON or fully typed APIs — generate, parse, and patch. Built for AI agents, LLM tool-calling, and hand-written code alike; no Microsoft Office required, opens in every major office suite.
人工智能与机器学习可以做什么
- 📄 All-in-One — Word (.docx), Excel (.xlsx), and PowerPoint (.pptx) in one cohesive API — no server required, works offline
- 🗂 Multi-Format I/O — Parse legacy Word/Excel/PowerPoint (.doc/.xls/.ppt), RTF, ODF, and OOXML package variants through the same option models; generate RTF, ODT/ODS/ODP, and CSV/TSV
- 🤖 AI Tooling — Draft-07 JSON Schemas frozen from the TypeScript API, on-demand schema slicing for LLM context budgets (CLI + SDK tool), Vercel AI SDK tool definitions, and an installable Agent Skill
- 🧭 Core OOXML Coverage — All 2,273 elements and 1,969 attributes across the 21 OOXML Transitional schemas (WordprocessingML, PresentationML, SpreadsheetML, DrawingML, shared math, VML, and Word extensions) are implemented for both generation and parsing, verified by automated XSD coverage tooling
实测星标增长
- 24小时
- 0
- 7天
- +2
- 15天
- +2
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.zh-CN.md
原文office-open
English | 简体中文
AI 原生的 Office 文档库,面向 TypeScript 和 JavaScript。 用纯 JSON 或全类型 API 创建 Word、Excel、PowerPoint、RTF 和 OpenDocument 文件——在格式能力范围内提供生成、解析和补丁。读取旧版 .doc/.xls/.ppt 与 ODF;生成 .rtf、ODT/ODS/ODP 并转换 CSV/TSV。为 AI 智能体、LLM 工具调用与手写代码而生;无需 Microsoft Office。
文档 · Playground · AI 集成 · 性能基准 · npm
json-to-office 在生产环境使用 office-open——其质量优先的 DOCX 渲染引擎正是 @office-open/docx。
⭐ 如果 office-open 为你节省了时间,一个 star 能帮更多开发者发现它。
特性
- 📄 三格式一体 — Word (.docx)、Excel (.xlsx)、PowerPoint (.pptx) 共用一套连贯 API——无需服务器,离线可用
- 🗂 多格式读写 — 通过同一套 options 模型解析旧版 Word/Excel/PowerPoint(.doc/.xls/.ppt)、RTF、ODF 与 OOXML 包变体;支持生成 RTF、ODT/ODS/ODP 与转换 CSV/TSV
- 🤖 AI 工具链 — 由 TypeScript API 冻结的 Draft-07 JSON Schema、按需切片适配 LLM 上下文预算(CLI + SDK 工具)、Vercel AI SDK 工具定义、可安装的 Agent Skill
- 🧭 核心 OOXML 覆盖 — 21 个 OOXML Transitional schema(WordprocessingML、PresentationML、SpreadsheetML、DrawingML、共享 math、VML 与 Word 扩展)的全部 2,273 个元素与 1,969 个属性均已实现生成与解析,并由自动化 XSD 覆盖率工具验证
- 📐 符合规范 — 输出通过 OOXML Transitional XSD schema(ISO/IEC 29500)校验,并经实测可在 Microsoft Office、WPS Office、LibreOffice、Google Workspace 中打开
- 🔒 全量类型 — 每个 API 都有完整 TypeScript 定义,自动补全与类型安全全覆盖
- 🔄 解析与补丁 — 读回现有 .docx、.pptx、.xlsx 文件做往返(round-trip)工作流,或按占位符替换补丁模板
- 🧮 电子表格公式 — 解析、分析、重写并按依赖顺序重算 Excel 与 OpenFormula 表达式
- 🎨 富内容 — 段落、表格、图片、图表、SmartArt、数学公式、效果、动画等
- 🔀 跨格式复制 — 图片、形状、表格、文本可跨格式转换;各格式保留原生类型,转换复用
core共享域——无统一文档模型层 - ⚡ 高性能 — 纯字符串拼接生成 XML,无中间 AST,原生 zlib 压缩——见性能基准
- 🌐 跨平台 — Node.js、浏览器、Deno、Bun。导出 Buffer、Blob、Base64、流或字符串
性能基准
各包基准测试摘选(ops/s,越高越好;Windows 11、Node 24——相同场景在 Bun 1.4 上还能再快约 2×)。压缩率与 MS Office 默认值持平;完整方法论、压缩模式与全量数据见各包 README:
| 包 | 场景 | @office-open | 竞品 | 快 |
|---|---|---|---|---|
| @office-open/docx | 全功能文档 + 2 张图片 | 763 ops/s | docx 9.6.1 — 54.2 ops/s | 14× |
| 2,000 段落 + 20 张图片 | 104 ops/s | docx 9.6.1 — 2.85 ops/s | 37× | |
| @office-open/pptx | 50 页全样式幻灯片 | 73.8 ops/s | PptxGenJS — 0.91 ops/s | 81× |
| @office-open/xlsx | 100k 行 × 20 列(200 万单元格) | 0.89 ops/s | hucre — 0.46 ops/s | 1.9× |
| @office-open/xml | 解析复杂 OOXML | 424k ops/s | txml — 389k ops/s | 1.1× |
为什么选择 office-open?
大多数 Office 库只能生成文件。office-open 形成完整闭环:生成、解析、补丁——以一致的选项模型、通过 XSD 校验的输出和面向 AI 的 JSON Schema,统一覆盖 Word、Excel 和 PowerPoint。
| 如果你正在使用 | office-open 额外提供 |
|---|---|
| docx | 解析与模板补丁支持回写工作流,生成速度约快 14 倍 |
| ExcelJS | 同一套声明式 API 覆盖 Word 和 PowerPoint,共享绘图模型 |
| PptxGenJS | 解析与补丁、跨格式转换、XSD 校验输出 |
包
| 包 | 版本 | 说明 |
|---|---|---|
| office-open | 全家桶:全部包 + CLI + AI SDK 工具 | |
| @office-open/docx | Word 文档生成、解析与补丁 | |
| @office-open/pptx | PowerPoint 生成、解析与补丁 | |
| @office-open/xlsx | 电子表格生成、解析与补丁 | |
| @office-open/formula | 电子表格公式解析、分析、重写与求值 | |
| @office-open/doc | 旧版 Word .doc 解析 | |
| @office-open/xls | 旧版 Excel .xls 解析 | |
| @office-open/ppt | 旧版 PowerPoint .ppt 解析 | |
| @office-open/rtf | Rich Text Format 生成与解析 | |
| @office-open/odf | 共享 OpenDocument 容器与运行时 | |
| @office-open/odt | OpenDocument 文本生成与解析 | |
| @office-open/ods | OpenDocument 表格生成与解析 | |
| @office-open/odp | OpenDocument 演示生成与解析 | |
| @office-open/core | 共享 OOXML 基础设施、图表、单位换算 | |
| @office-open/xml | 底层 XML 解析与序列化 |
格式能力
| 格式族 | 解析 | 生成 | 补丁 | 说明 |
|---|---|---|---|---|
OOXML .docx/.xlsx/.pptx |
✅ | ✅ | ✅ | 双向包编解码器 |
旧版 .doc/.xls/.ppt |
✅ | ❌ | ❌ | 投影为 OOXML options 模型 |
.rtf |
✅ | ✅ | ✅ | 严格的 DocumentOptions 投影 |
ODF .odt/.ods/.odp |
✅ | ✅ | ✅ | 规范格式族读写编解码器 |
能力矩阵是契约,不代表可以虚构不支持的编辑能力:旧版格式没有写入器,RTF 会结构化拒绝无法表示的字段而不是静默丢弃,ODF 补丁会先编辑 canonical 模型再重新生成包。
快速开始
# pnpm
pnpm add office-open
# npm
npm install office-open
# yarn
yarn add office-open
# bun
bun add office-open
office-open 打包各格式包以及 CLI、JSON Schema 和 AI SDK 工具。想要更小的体积?见包一节按格式选用 @office-open/* 包。
import { generateDocumentSync } from "office-open/docx";
import { writeFileSync } from "node:fs";
// Options 是纯 JSON 对象——零类实例化
const buffer = generateDocumentSync({
sections: [
{
children: [
{ paragraph: { heading: "Heading1", children: ["文档标题"] } },
{ paragraph: { children: [{ text: "正文内容", italic: true }] } },
{
table: {
rows: [
{ cells: [{ children: [{ paragraph: "A1" }] }, { children: [{ paragraph: "B1" }] }] },
{ cells: [{ children: [{ paragraph: "A2" }] }, { children: [{ paragraph: "B2" }] }] },
],
},
},
],
},
],
});
writeFileSync("document.docx", buffer);
PowerPoint 与 Excel 结构相同——generatePresentationSync({ slides: [...] }) 与 generateWorkbookSync({ worksheets: [...] });见上方各包 README。另有按类型分发的 generateOffice() 助手与 CLI:
npx office-open xlsx input.json "output.xlsx"
解析现有文件
把现有文件读回同一套结构化 options,用于检查或往返编辑——pptx 的 parsePresentation 与 xlsx 的 parseWorkbook 与之同构:
import { parseDocument } from "office-open/docx";
const opts = await parseDocument(buffer);
// opts.sections — 文档节与内容
// opts.title, opts.creator — 核心属性
AI 集成
给 AI 智能体一等公民的 Office 文档能力——四种方式,零胶水代码:
Vercel AI SDK 工具 — 让 Claude、GPT 等模型生成经过 schema 校验重试的合法文档:
import { generateText } from "ai";
import { officeOpenTools } from "office-open/ai";
const result = await generateText({
model,
prompt: "创建一份季度报告文档",
tools: officeOpenTools, // OOXML/ODF 生成工具 + schema 查询
});
MCP server — 将文档接入 Claude Code、Cursor 或任何 MCP 客户端:
claude mcp add --transport http office-open https://www.office-open.com/mcp
Agent Skill — 可安装技能,内附 docx、pptx、xlsx 精选 API 参考:
npx skills add https://www.office-open.com
JSON Schema — 供自有工具调用使用的冻结 draft-07 schema,按需切片适配 LLM 上下文预算:
npx office-open schema index docx
npx office-open schema slice docx ParagraphOptions
详见 AI 集成指南。
版本策略
本项目遵循语义化版本。主版本号为 0(pre-1.0)期间,破坏性 API 变更以 minor 版本(0.x.0)而非 patch 发布——公开 API 预计持续演进至 1.0.0 稳定版。若需要在 minor 更新之间保持稳定,下游项目请锁定精确版本。
开发
需要 Node.js 18+ 与 pnpm 9+。
git clone https://github.com/DemoMacro/office-open.git
cd office-open
pnpm install
pnpm dev # 监听模式的开发构建
pnpm build # 构建全部包
pnpm test # 运行测试
pnpm check # Lint 与格式化
参与贡献
欢迎贡献!Fork 仓库,克隆你的 fork,并添加 upstream 远端:
git clone https://github.com/YOUR_USERNAME/office-open.git
cd office-open
git remote add upstream https://github.com/DemoMacro/office-open.git
pnpm install
然后按工作流推进:按项目标准编码,运行 pnpm build && pnpm test,使用约定式提交(feat:、fix:、docs:、refactor: 等)提交,推送到你的 fork,并向 upstream 发起 Pull Request。
支持与社区
如果 office-open 对你有用,一个 ⭐ star 能帮更多开发者发现它。
赞助
office-open 由 Wiseair-srl 支持——他们的 json-to-office 选择 @office-open/docx 作为质量优先的 DOCX 渲染引擎,在线 playground 见 docx.json-to-office.com。感谢!也想支持本项目?GitHub Sponsors。
致谢
本项目的 git 历史始于 dolanmiu/docx 的 fork。实现此后已完全重写,但 API 形态——sections、paragraphs、tables、runs——仍沿用该项目确立的模型,其设计塑造了我们的早期方向。感谢 Dolan Miu 与 docx 的贡献者给我们的起点与启发。
许可证
本项目基于 MIT 许可证开源——详见 LICENSE。
Built with ❤️ by Demo Macro
探索相关项目
加载中…