快速开始

安装

如何安装 office-open 包

包管理器

安装你需要的文档类型对应的包:

# pnpm
pnpm add @office-open/docx
pnpm add @office-open/pptx

# npm
npm install @office-open/docx
npm install @office-open/pptx

# yarn
yarn add @office-open/docx
yarn add @office-open/pptx

没有单一的 office-open 包——只需安装你需要的特定包。共享依赖(@office-open/core@office-open/xml)会自动引入。

浏览器

在浏览器中使用时,直接从支持 ESM 的 CDN 导入:

<script type="module">
    import { Document, Packer, Paragraph, TextRun } from "https://esm.sh/@office-open/docx";

    // ... 创建并导出你的文档
</script>

或使用打包工具(Vite、Webpack 等)——这些包以标准 ESM 模块发布,开箱即用。

TypeScript

所有包均使用 TypeScript 编写,并附带内置类型定义。无需安装额外的 @types 包。

import { Document, Packer, Paragraph, TextRun } from "@office-open/docx";

// 完整的类型支持和自动补全
const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [new TextRun({ text: "Hello, World!", bold: true })],
                }),
            ],
        },
    ],
});

环境要求

  • Node.js >= 18
  • TypeScript >= 5.0(可选,但推荐)
Copyright © 2026