PPTX

@office-open/pptx

使用声明式 TypeScript API 生成 .pptx 演示文稿

安装

pnpm add @office-open/pptx

快速开始

import {
    Presentation,
    Packer,
    Slide,
    Shape,
    TextBody,
    Paragraph,
    TextRun,
} from "@office-open/pptx";

const presentation = new Presentation({
    slides: [
        new Slide({
            children: [
                new Shape({
                    body: new TextBody({
                        children: [
                            new Paragraph({
                                children: [new TextRun("Hello World")],
                            }),
                        ],
                    }),
                }),
            ],
        }),
    ],
});

const buffer = await Packer.toBuffer(presentation);

主要组件

组件说明
Presentation演示文稿根容器,包含幻灯片和属性
Slide单张幻灯片,包含形状、媒体和布局
Shape矩形、圆角矩形和自定义几何形状
GroupShape分组形状,支持集体变换
TextBody, Paragraph, TextRun形状内的富文本内容
Table, TableRow, TableCell表格,支持合并单元格和样式
Picture幻灯片上的嵌入图片
ChartFrame柱状图、折线图、饼图、面积图和散点图
SmartArtFrame预定义 SmartArt 图示
VideoFrame, AudioFrame嵌入视频和音频
LineShape, ConnectorShape元素之间的线条和连接符
Transition幻灯片切换效果
Animation形状进入、强调和退出动画
HeaderFooter幻灯片页脚,支持日期、编号和文本

导出格式

方法返回值适用场景
Packer.toBuffer(doc)Uint8ArrayNode.js 文件 I/O
Packer.toBlob(doc)Blob浏览器下载
Packer.toBase64String(doc)stringData URL、API 载荷
Packer.toString(doc)string调试、检查
Packer.toStream(doc)ReadableStream流式传输大文件

幻灯片尺寸预设

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

const presentation = new Presentation({
    slideSize: SlideSizePreset.WIDESCREEN_16X9,
    slides: [
        // ...
    ],
});

可用预设:

预设尺寸
WIDE13.33" × 7.5"(宽屏)
STANDARD_4X310" × 7.5"(标准)
WIDESCREEN_16X1010" × 6.25"
WIDESCREEN_16X913.33" × 7.5"
Copyright © 2026