PPTX

快速开始

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

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

安装

pnpm add @office-open/pptx

第一步 — 创建演示文稿

{
  "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!",
              "children": [
                {
                  "properties": { "alignment": "CENTER" },
                  "children": [
                    {
                      "text": "My First Presentation",
                      "size": 40,
                      "bold": true,
                      "fill": "4472C4"
                    }
                  ]
                }
              ]
            }
          }
        }
      ]
    }
  ]
}

第二步 — 添加多张幻灯片

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

const buffer = await generatePresentation({
  slides: [
    // 标题幻灯片
    {
      children: [
        {
          shape: {
            x: "1.3cm",
            y: "4.0cm",
            width: "23.3cm",
            height: "5.3cm",
            textBody: {
              text: "Quarterly Report",
              children: [
                {
                  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: "1.3cm",
            textBody: {
              text: "Key Highlights",
              children: [
                {
                  children: [{ text: "Key Highlights", size: 28, bold: true }],
                },
              ],
            },
          },
        },
        {
          shape: {
            x: "1.3cm",
            y: "4.0cm",
            width: "23.3cm",
            height: "9.3cm",
            textBody: {
              children: [
                {
                  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 — 写入文件

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");

浏览器 — 下载

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)并运行:

npx tsx demo.ts
Copyright © 2026