PPTX
形状与文本
创建带格式文本、段落、对齐、列表和文本自适应的形状
形状是幻灯片的主要构建块。一个形状可以通过 textBody 属性来包含文本内容。
基本形状
import { generatePresentation } from "@office-open/pptx";
{
"slides": [
{
"children": [
{
"shape": {
"x": "1.3cm",
"y": "1.3cm",
"width": "13.2cm",
"height": "7.9cm",
"textBody": { "text": "Simple text" }
}
}
]
}
]
}
{
children: [
{
shape: {
x: "1.3cm",
y: "1.3cm",
width: "13.2cm",
height: "7.9cm",
textBody: { text: "Simple text" },
},
},
],
}
几何预设
使用 geometry 选项设置形状类型:
{
"slides": [
{
"children": [
{
"shape": {
"x": "2.6cm",
"y": "2.6cm",
"width": "5.3cm",
"height": "4.0cm",
"geometry": "ellipse",
"fill": "4472C4",
"outline": { "color": "2E74B5", "width": 12700 },
"textBody": { "text": "椭圆" }
}
},
{
"shape": {
"x": "10.6cm",
"y": "2.6cm",
"width": "5.3cm",
"height": "4.0cm",
"geometry": "triangle",
"fill": "ED7D31",
"outline": { "color": "C5504B", "width": 12700 },
"textBody": { "text": "三角形" }
}
}
]
}
]
}
[
{ shape: { geometry: "ellipse", fill: "4472C4", outline: { color: "2E74B5", width: 2 } } },
{ shape: { geometry: "triangle", fill: "ED7D31", outline: { color: "C5504B", width: 2 } } },
];
可用的几何类型:
| Geometry | 形状 |
|---|---|
"rect" | 矩形(默认) |
"ellipse" | 椭圆/圆形 |
"roundRect" | 圆角矩形 |
"triangle" | 三角形 |
"diamond" | 菱形 |
"star5" | 五角星 |
"hexagon" | 六边形 |
"chevron" | V 形箭头 |
"arrow" | 箭头 |
使用 outline 添加边框:{ "color": "2E74B5", "width": 12700 }。不要使用 strokeColor 或 strokeWidth,这些不是有效的属性。
段落和文本片段
使用 textBody.children 完全控制文本格式:
{
"slides": [
{
"children": [
{
"shape": {
"x": "1.3cm",
"y": "1.3cm",
"width": "15.9cm",
"height": "6.6cm",
"textBody": {
"children": [
{
"children": [{ "text": "Bold title", "bold": true, "size": 28, "fill": "2E74B5" }]
},
{
"children": [
{ "text": "Normal text with " },
{ "text": "italic", "italic": true },
{ "text": " and " },
{ "text": "colored", "fill": "FF0000", "underline": "SINGLE" }
]
}
]
}
}
}
]
}
]
}
{
children: [
{
shape: {
x: "1.3cm",
y: "1.3cm",
width: "15.9cm",
height: "6.6cm",
textBody: {
children: [
{
children: [{ text: "Bold title", bold: true, size: 28, fill: "2E74B5" }],
},
{
children: [
"Normal text with ",
{ text: "italic", italic: true },
" and ",
{ text: "colored", fill: "FF0000", underline: "SINGLE" },
],
},
],
},
},
},
],
}
TextRun 格式选项
{
text: "Formatted text",
size: 24, // 字号(磅)
bold: true,
italic: true,
underline: "SINGLE", // "SINGLE" | "DOUBLE" | "NONE"
fill: "4472C4", // 十六进制颜色,不带 #
font: "Calibri", // 字体
}
对齐方式
{
properties: { alignment: "left" }, // "left" | "center" | "right" | "justify"
children: [{ text: "Aligned text" }],
}
项目符号列表
{
"slides": [
{
"children": [
{
"shape": {
"x": "1.3cm",
"y": "1.3cm",
"width": "15.9cm",
"height": "9.3cm",
"textBody": {
"children": [
{
"properties": { "bullet": { "type": "char", "char": "●" } },
"children": [{ "text": "First item" }]
},
{
"properties": { "bullet": { "type": "char", "char": "●" } },
"children": [{ "text": "Second item" }]
},
{
"properties": { "bullet": { "type": "autoNum", "format": "arabicPeriod" } },
"children": [{ "text": "Numbered item" }]
}
]
}
}
}
]
}
]
}
{
children: [
{
shape: {
x: "1.3cm",
y: "1.3cm",
width: "15.9cm",
height: "9.3cm",
textBody: {
children: [
{
properties: { bullet: { type: "char", char: "●" } },
children: [{ text: "First item" }],
},
{
properties: { bullet: { type: "char", char: "●" } },
children: [{ text: "Second item" }],
},
{
properties: { bullet: { type: "autoNum", format: "arabicPeriod" } },
children: [{ text: "Numbered item" }],
},
],
},
},
},
],
}
文本锚点(垂直对齐)
控制文本在形状内的垂直位置:
{
"slides": [
{
"children": [
{
"shape": {
"x": "1.3cm",
"y": "1.3cm",
"width": "13.2cm",
"height": "7.9cm",
"textBody": {
"anchor": "top",
"text": "Top-aligned text"
}
}
}
]
}
]
}
{
children: [
{
shape: {
x: "1.3cm",
y: "1.3cm",
width: "13.2cm",
height: "7.9cm",
textBody: {
anchor: "top", // "top" | "center" | "bottom" | "justify" | "distribute"
text: "Top-aligned text",
},
},
},
],
}
文本自适应
{
"slides": [
{
"children": [
{
"shape": {
"x": "1.3cm",
"y": "1.3cm",
"width": "10.6cm",
"height": "5.3cm",
"textBody": {
"text": "This text will shrink to fit the shape",
"autoFit": "normal"
}
}
}
]
}
]
}
{
children: [
{
shape: {
x: "1.3cm",
y: "1.3cm",
width: "10.6cm",
height: "5.3cm",
textBody: {
text: "This text will shrink to fit the shape",
autoFit: "normal", // 自动缩小文本以适应形状
// 或 autoFit: "shape" 自动调整形状大小
},
},
},
],
}
旋转和翻转
{
"slides": [
{
"children": [
{
"shape": {
"x": "1.3cm",
"y": "1.3cm",
"width": "7.9cm",
"height": "5.3cm",
"rotation": 45,
"flipHorizontal": true,
"textBody": { "text": "Rotated shape" }
}
}
]
}
]
}
{
children: [
{
shape: {
x: "1.3cm",
y: "1.3cm",
width: "7.9cm",
height: "5.3cm",
rotation: 45, // 角度
flipHorizontal: true, // 水平镜像
textBody: { text: "Rotated shape" },
},
},
],
}
文本分栏
{
"slides": [
{
"children": [
{
"shape": {
"x": "1.3cm",
"y": "1.3cm",
"width": "21.2cm",
"height": "9.3cm",
"textBody": {
"columns": 2,
"columnSpacing": 0.5,
"children": [
{ "children": [{ "text": "Column 1 content..." }] },
{ "children": [{ "text": "Column 2 content..." }] }
]
}
}
}
]
}
]
}
{
children: [
{
shape: {
x: "1.3cm",
y: "1.3cm",
width: "21.2cm",
height: "9.3cm",
textBody: {
columns: 2,
columnSpacing: 0.5,
children: [
{ children: ["Column 1 content..."] },
{ children: ["Column 2 content..."] },
],
},
},
},
],
}
文本边距
{
"slides": [
{
"children": [
{
"shape": {
"x": "1.3cm",
"y": "2.6cm",
"width": "15.9cm",
"height": "7.9cm",
"textBody": {
"margins": { "top": 0.2, "bottom": 0.2, "left": 0.3, "right": 0.3 },
"text": "Text with custom margins"
}
}
}
]
}
]
}
{
children: [
{
shape: {
x: "1.3cm",
y: "2.6cm",
width: "15.9cm",
height: "7.9cm",
textBody: {
margins: { top: 0.2, bottom: 0.2, left: 0.3, right: 0.3 },
text: "Text with custom margins",
},
},
},
],
}