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) | Uint8Array | Node.js 文件 I/O |
Packer.toBlob(doc) | Blob | 浏览器下载 |
Packer.toBase64String(doc) | string | Data 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: [
// ...
],
});
可用预设:
| 预设 | 尺寸 |
|---|---|
WIDE | 13.33" × 7.5"(宽屏) |
STANDARD_4X3 | 10" × 7.5"(标准) |
WIDESCREEN_16X10 | 10" × 6.25" |
WIDESCREEN_16X9 | 13.33" × 7.5" |