PPTX

快速开始

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

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

安装

npm install @office-open/pptx

第一步 — 创建演示文稿

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

const pres = new Presentation({
    title: "My First Presentation",
    creator: "Author",
    slides: [
        new Slide({
            children: [
                new Shape({
                    x: 1,
                    y: 1,
                    width: 8,
                    height: 1.5,
                    text: "Hello, PowerPoint!",
                    paragraphs: [
                        new Paragraph({
                            alignment: "center",
                            children: [
                                new TextRun({
                                    text: "My First Presentation",
                                    fontSize: 40,
                                    bold: true,
                                    color: "4472C4",
                                }),
                            ],
                        }),
                    ],
                }),
            ],
        }),
    ],
});

第二步 — 添加多张幻灯片

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

const pres = new Presentation({
    slides: [
        // 标题幻灯片
        new Slide({
            children: [
                new Shape({
                    x: 1,
                    y: 2,
                    width: 8,
                    height: 2,
                    text: "Quarterly Report",
                    paragraphs: [
                        new Paragraph({
                            alignment: "center",
                            children: [
                                new TextRun({ text: "Q4 2025 Summary", fontSize: 44, bold: true }),
                            ],
                        }),
                        new Paragraph({
                            alignment: "center",
                            children: [
                                new TextRun({
                                    text: "Prepared by the Finance Team",
                                    fontSize: 20,
                                    color: "808080",
                                }),
                            ],
                        }),
                    ],
                }),
            ],
        }),

        // 内容幻灯片
        new Slide({
            children: [
                new Shape({
                    x: 0.5,
                    y: 0.5,
                    width: 9,
                    height: 0.8,
                    text: "Key Highlights",
                    paragraphs: [
                        new Paragraph({
                            children: [
                                new TextRun({ text: "Key Highlights", fontSize: 28, bold: true }),
                            ],
                        }),
                    ],
                }),
                new Shape({
                    x: 0.5,
                    y: 1.5,
                    width: 9,
                    height: 4,
                    paragraphs: [
                        new Paragraph({
                            bullet: true,
                            children: [
                                new TextRun({
                                    text: "Revenue grew by 15% year-over-year",
                                    fontSize: 18,
                                }),
                            ],
                        }),
                        new Paragraph({
                            bullet: true,
                            children: [
                                new TextRun({
                                    text: "Customer satisfaction reached 94%",
                                    fontSize: 18,
                                }),
                            ],
                        }),
                        new Paragraph({
                            bullet: true,
                            children: [
                                new TextRun({ text: "Expanded into 3 new markets", fontSize: 18 }),
                            ],
                        }),
                    ],
                }),
            ],
        }),
    ],
});

第三步 — 导出文件

Node.js — 写入文件

import fs from "node:fs";

const buffer = await Packer.toBuffer(pres);
fs.writeFileSync("presentation.pptx", buffer);
console.log("Created presentation.pptx");

浏览器 — 下载

const blob = await Packer.toBlob(pres);
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