pptx-craft
v0.1.0
Published
Lightweight, type-safe library for creating PowerPoint (.pptx) files
Downloads
27
Maintainers
Readme
English | 中文
安装
npm install pptx-craft快速开始
import { Presentation } from 'pptx-craft';
const pptx = new Presentation();
const slide = pptx.addSlide();
slide.addText('Hello World', {
x: 1,
y: 1,
w: 8,
h: 1.5,
fontSize: 44,
color: '003366',
bold: true,
align: 'center',
});
slide.addShape('rect', { x: 1, y: 3, w: 8, h: 0.05, fill: '4472C4' });
await pptx.toFile('output.pptx');特性
- 文本 — 字体、大小、颜色、粗体/斜体/下划线、对齐方式、行距
- 图片 — 支持文件路径或 Buffer(PNG、JPEG 等)
- 形状 — 矩形、椭圆、圆角矩形、菱形、三角形、心形、星形、箭头等
- 表格 — 单元格级别格式化、边框、自定义列宽/行高
- 图表 — 柱状图、条形图、折线图、饼图、面积图、散点图、环形图(原生 PowerPoint 图表)
- 媒体 — 视频和音频嵌入
- 母版与版式 — 自定义背景、基于占位符的版式布局
- 主题 — 完整的配色方案和字体自定义
- 演讲者备注 — 逐页添加备注
- 插件 — 通过自定义元素类型进行扩展
预览
示例
表格
slide.addTable(
[
[
{ text: 'Name', options: { bold: true, fill: '4472C4', color: 'FFFFFF' } },
{ text: 'Score', options: { bold: true, fill: '4472C4', color: 'FFFFFF' } },
],
[{ text: 'Alice' }, { text: '95' }],
[{ text: 'Bob' }, { text: '87' }],
],
{ x: 1, y: 1, w: 6, h: 3, border: { color: 'CCCCCC', width: 1 } },
);图表
slide.addChart(
'col',
{
categories: ['Q1', 'Q2', 'Q3', 'Q4'],
series: [
{ name: '2024', values: [120, 180, 150, 210], color: '4472C4' },
{ name: '2025', values: [140, 200, 190, 250], color: 'ED7D31' },
],
},
{ x: 1, y: 1, w: 8, h: 5, title: 'Revenue', showLegend: true },
);母版与版式
const pptx = new Presentation();
pptx.defineSlideMaster({ name: 'Corporate', background: '003366' });
pptx.defineLayout({
name: 'Title Slide',
type: 'titleAndContent',
placeholders: [
{ type: 'title', placement: { x: 0.5, y: 0.5, w: 9, h: 1 }, idx: 0 },
{ type: 'body', placement: { x: 0.5, y: 2, w: 9, h: 5 }, idx: 1 },
],
});
const slide = pptx.addSlide('Title Slide');插件
import type { IPlugin } from 'pptx-craft';
const myPlugin: IPlugin = {
name: 'my-plugin',
install(registry) {
registry.registerElement('badge', badgeSerializer);
},
};
pptx.use(myPlugin);输出
// Buffer
const buffer = pptx.toBuffer();
// 文件
await pptx.toFile('presentation.pptx');文档
完整文档请访问文档站点。本地运行:
npm run docs:dev致谢
本项目基于 Brent Ely 的 PptxGenJS 衍生开发。
许可证
MIT — 详见 LICENSE。
