跳到主要内容
Office Open

PPTX

快速开始

在 5 分钟内创建你的第一个 PowerPoint 演示文稿

通过创建一个简单的多幻灯片演示文稿来开始使用 @office-open/pptx。

#安装

pnpm
pnpm add @office-open/pptx

#第一步 — 创建演示文稿

JSON
{
  "title": "My First Presentation",
  "creator": "Author",
  "slides": [
    {
      "children": [
        {
          "shape": {
            "x": "1.3cm",
            "y": "1.3cm",
            "width": "23.3cm",
            "height": "9.3cm",
            "textBody": {
              "text": "Hello, PowerPoint!",
              "paragraphs": [
                {
                  "properties": { "alignment": "center" },
                  "children": [
                    {
                      "text": "My First Presentation",
                      "size": 40,
                      "bold": true,
                      "fill": "4472C4"
                    }
                  ]
                }
              ]
            }
          }
        }
      ]
    }
  ]
}

#第二步 — 添加多张幻灯片

JSON
{
  "slides": [
    {
      "children": [
        {
          "shape": {
            "x": "1.3cm",
            "y": "4.0cm",
            "width": "21.2cm",
            "height": "5.3cm",
            "textBody": {
              "text": "Quarterly Report",
              "paragraphs": [
                {
                  "properties": { "alignment": "center" },
                  "children": [{ "text": "Q4 2025 Summary", "size": 44, "bold": true }]
                },
                {
                  "properties": { "alignment": "center" },
                  "children": [
                    {
                      "text": "Prepared by the Finance Team",
                      "size": 20,
                      "fill": "808080"
                    }
                  ]
                }
              ]
            }
          }
        }
      ]
    },
    {
      "children": [
        {
          "shape": {
            "x": "1.3cm",
            "y": "1.3cm",
            "width": "23.3cm",
            "height": "2.6cm",
            "textBody": {
              "text": "Key Highlights",
              "paragraphs": [
                {
                  "children": [{ "text": "Key Highlights", "size": 28, "bold": true }]
                }
              ]
            }
          }
        },
        {
          "shape": {
            "x": "1.3cm",
            "y": "2.6cm",
            "width": "23.3cm",
            "height": "9.3cm",
            "textBody": {
              "paragraphs": [
                {
                  "properties": { "bullet": { "type": "char", "char": "●" } },
                  "children": [
                    {
                      "text": "Revenue grew by 15% year-over-year",
                      "size": 18
                    }
                  ]
                },
                {
                  "properties": { "bullet": { "type": "char", "char": "●" } },
                  "children": [
                    {
                      "text": "Customer satisfaction reached 94%",
                      "size": 18
                    }
                  ]
                },
                {
                  "properties": { "bullet": { "type": "char", "char": "●" } },
                  "children": [{ "text": "Expanded into 3 new markets", "size": 18 }]
                }
              ]
            }
          }
        }
      ]
    }
  ]
}

#第三步 — 导出文件

#Node.js — 写入文件

ts
import fs from "node:fs";
import { generatePresentation } from "@office-open/pptx";

const buffer = await generatePresentation({/* 选项 */});
fs.writeFileSync("presentation.pptx", buffer);
console.log("Created presentation.pptx");

#浏览器 — 下载

ts
import { generatePresentation } from "@office-open/pptx";

const blob = await generatePresentation({/* 选项 */}, { type: "blob" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "presentation.pptx";
a.click();
URL.revokeObjectURL(url);

#第四步 — 运行

将代码保存到文件(例如 demo.ts)并运行:

bash
npx tsx demo.ts