PPTX

幻灯片布局

配置幻灯片尺寸、背景和演讲者备注

控制每张幻灯片的尺寸、背景和备注。

幻灯片尺寸

generatePresentation()size 属性中设置幻灯片尺寸:

{
  "size": { "width": "33.9cm", "height": "19.1cm" },
  "slides": [
    {
      "children": [
        {
          "shape": {
            "x": "2.6cm",
            "y": "2.6cm",
            "width": "21.2cm",
            "height": "10.6cm",
            "textBody": { "text": "Widescreen content" }
          }
        }
      ]
    }
  ]
}

尺寸预设

使用字符串快捷方式设置常见宽高比:

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

generatePresentation({
  size: "16:9", // 或 "4:3"
  slides: [],
});
预设EMU 尺寸像素(96 DPI)
"16:9"12192000 x 68580001280 x 720
"4:3"9144000 x 6858000960 x 720

隐藏幻灯片

通过 hidden: true 将幻灯片排除出放映(生成 p:sld/@show="0"):

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

generatePresentation({
  slides: [
    { children: [], hidden: true }, // 放映时跳过
    { children: [] },
  ],
});

幻灯片背景

纯色

{
  "slides": [
    {
      "children": [],
      "background": { "fill": "1F4E79" }
    }
  ]
}

渐变

{
  "slides": [
    {
      "children": [],
      "background": {
        "fill": {
          "type": "gradient",
          "stops": [
            { "position": 0, "color": "0D47A1" },
            { "position": 100, "color": "1976D2" }
          ],
          "angle": 90
        }
      }
    }
  ]
}

图片背景

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

generatePresentation({
  slides: [
    {
      background: {
        fill: {
          type: "blip",
          data: new Uint8Array(fs.readFileSync("bg.png")),
          imageType: "png",
        },
      },
      children: [],
    },
  ],
});

演讲者备注

添加在演示者模式下可见的备注:

{
  "slides": [
    {
      "children": [
        {
          "shape": {
            "x": "2.6cm",
            "y": "2.6cm",
            "width": "21.2cm",
            "height": "10.6cm",
            "textBody": { "text": "Slide content" }
          }
        }
      ],
      "notes": "Remember to emphasize the key metrics in this slide."
    }
  ]
}

演示文稿元数据

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

generatePresentation({
  title: "Annual Report 2025",
  creator: "John Doe",
  description: "Yearly financial summary",
  subject: "Finance",
  keywords: "annual, report, finance",
  lastModifiedBy: "Jane Doe",
  revision: 1,
  slides: [],
});

元数据属性

属性类型说明
titlestring文档标题
creatorstring作者名称
descriptionstring文档描述
subjectstring主题
keywordsstring关键词
lastModifiedBystring最后修改者
revisionnumber修订版本号

幻灯片母版

使用 masters 属性定义幻灯片母版。每个母版可以自定义主题、背景和占位符布局:

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

generatePresentation({
  masters: [
    {
      name: "light",
      background: { fill: "FFFFFF" },
      theme: {
        name: "Light Theme",
        colors: { dark1: "333333", light1: "FFFFFF" },
      },
    },
  ],
  slides: [{ children: [] }],
});

母案子元素

使用 children 属性向母版添加装饰形状(JSON 格式):

{
  name: "branded",
  children: [
    {
      shape: {
        x: "0.0cm",
        y: "16.9cm",
        width: "25.4cm",
        height: "1.1cm",
        fill: "4472C4",
      },
    },
  ],
}

母版占位符

控制母版上显示哪些占位符及其位置:

{
  placeholders: {
    title: { x: "1.3cm", y: "0.5cm", width: "22.8cm", height: "2.1cm" },
    body: { x: "1.3cm", y: "3.2cm", width: "22.8cm", height: "13.2cm" },
    date: false,      // 隐藏日期
    footer: false,    // 隐藏页脚
    slideNumber: true, // 使用默认位置
  },
}

多母版

定义多个母版并在幻灯片中引用:

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

generatePresentation({
  masters: [
    {
      name: "light",
      theme: { name: "Light", colors: { dark1: "333333" } },
    },
    {
      name: "dark",
      background: { fill: "1B2A4A" },
      theme: { name: "Dark", colors: { dark1: "FFFFFF", light1: "1B2A4A" } },
    },
  ],
  slides: [
    { master: "light", children: [] },
    { master: "dark", children: [] },
  ],
});

幻灯片版式

使用 layout 属性指定幻灯片的版式类型:

{
  layout: "title",  // 或 "blank"、"obj"、"secHead" 等
  children: [...],
}

版式类型

类型显示名称
"blank"空白
"title"标题幻灯片
"obj"标题和内容
"secHead"节标题
"twoObj"两栏内容
"twoTxTwoObj"比较
"titleOnly"仅标题
"objTx"带标题的内容
"picTx"带标题的图片
"vertTx"竖排文本
"vertTitleAndTx"竖排标题和文本
"tx"标题和文本
"twoColTx"两栏内容
"chart"带标题的内容
"tbl"带标题的内容

主题选项

为每个母版自定义主题颜色和字体:

颜色方案

属性说明
dark1深色主色
light1浅色主色
dark2深色副色
light2浅色副色
accent1accent6强调色
hyperlink超链接颜色
followedHyperlink已访问链接颜色

字体方案

属性说明
majorFont标题字体(拉丁)
minorFont正文字体(拉丁)
majorFontAsian标题字体(东亚)
minorFontAsian正文字体(东亚)
Copyright © 2026