跳到主要内容
Office Open

PPTX

幻灯片布局

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

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

#幻灯片尺寸

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

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

#尺寸预设

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

ts
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"):

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

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

#幻灯片背景

#纯色

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

#渐变

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

#图片背景

ts
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: [],
    },
  ],
});

#演讲者备注

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

JSON
{
  "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."
    }
  ]
}

#演示文稿元数据

ts
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 属性定义幻灯片母版。每个母版可以自定义主题、背景和占位符布局:

ts
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 格式):

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

#母版占位符

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

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

generatePresentation({
  masters: [
    {
      name: "custom",
      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, // 使用默认位置
      },
    },
  ],
  slides: [],
});

#多母版

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

ts
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 属性指定幻灯片的版式类型:

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

generatePresentation({
  slides: [
    {
      layout: "title", // 或 "blank"、"object"、"sectionHeader" 等
      children: [],
    },
  ],
});

#版式类型

类型显示名称
"blank"空白
"title"标题幻灯片
"text"标题和文本
"twoColumnText"两栏内容
"titleOnly"仅标题
"object"标题和内容
"sectionHeader"节标题
"chart"图表
"table"表格
"clipArtAndText"剪贴画和文本
"pictureText"带标题的图片
"twoObjects"两栏内容
"twoTextAndTwoObjects"比较
"objectAndText"带标题的内容
"verticalText"竖排文本
"verticalTitleAndText"竖排标题和文本

#主题选项

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

#颜色方案

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

#字体方案

属性说明
majorFont标题字体 —— FontCollectionOptions(latin、eastAsia、complexScript、symbol)
minorFont正文字体 —— FontCollectionOptions(latin、eastAsia、complexScript、symbol)