生成 Office Open XML 文档。

使用 JSON 或 TypeScript 创建 .docx.pptx.xlsx 文件。既适合 AI 代理,也适合传统开发工作流。
{
    "sections": [
        {
            "children": [
                { "paragraph": { "children": [{ "text": "Hello, World!", "bold": true }] } }
            ]
        }
    ]
}
JSON 与 TypeScript
使用纯 JSON 对象或 TypeScript 类创建文档。JSON 优先的设计使其成为 AI 代理和 LLM 工作流的理想选择。
丰富内容
段落、表格、图片、图表、SmartArt、数学公式、页眉、页脚等。
类型安全
完整的类型定义和自动补全,开箱即用。无需额外安装 @types 包。
跨平台
支持 Node.js 和浏览器。可导出为 Buffer、Blob、Base64、流或字符串。
符合 OOXML 规范
生成的文件完全符合 ISO/IEC 29500 Office Open XML 规范。
模块化包
按需安装 — docx、pptx、xml 或 core。

使用 JSON 或 TypeScript 构建文档

将文档定义为纯 JSON 对象——非常适合 AI 代理——或使用 TypeScript 类获得完整的 IDE 体验。两者均生成有效的 OOXML 标记。
    创建 Word 文档,支持段落、表格、图片和图表
    创建 PowerPoint 演示文稿,支持形状、动画和切换效果
    创建 Excel 电子表格,支持样式、图表和数据验证
    导出为 Buffer、Blob、Base64、流或字符串
{
    "sections": [
        {
            "children": [
                {
                    "table": {
                        "rows": [
                            { "cells": [{ "children": [{ "paragraph": "Name" }] }, { "children": [{ "paragraph": "Role" }] }] },
                            { "cells": [{ "children": [{ "paragraph": "Alice" }] }, { "children": [{ "paragraph": "Engineer" }] }] },
                            { "cells": [{ "children": [{ "paragraph": "Bob" }] }, { "children": [{ "paragraph": "Designer" }] }] }
                        ]
                    }
                }
            ]
        }
    ]
}

读取和修改现有文件

.docx.pptx.xlsx 文件解析为结构化对象进行检查,或通过替换 {{占位符}} 标记来修补模板。
    读取文档结构、样式和内容
    替换模板占位符为新内容
    解析、修改、重新导出一站式流水线
import { parseDocument, patchDocument, PatchType } from "@office-open/docx";

// 解析现有文件
const opts = parseDocument(buffer);
// opts.sections — 文档节
// opts.title, opts.creator — 核心属性

// 修补模板占位符
const result = await patchDocument({
  outputType: "nodebuffer",
  data: buffer,
  patches: {
    name: { type: PatchType.PARAGRAPH, children: [new TextRun("John")] },
  },
});

为你的项目添加文档生成能力。

Copyright © 2026