通过编程创建演示文稿
此示例展示如何使用 PowerPoint 的 MATLAB® API (PPT API) 创建 PowerPoint® 演示文稿。该示例生成以下幻灯片:
要以编程方式创建演示文稿:
导入 PPT API 命名空间。
创建一个展示容器。
添加幻灯片。
向幻灯片添加内容。
生成演示文稿。
导入 PPT API 命名空间
PPT API 类属于 mlreportgen.ppt
命名空间。导入此命名空间,这样您在调用 PPT API 对象构造函数和方法时就不必使用长而完全限定的名称。
import mlreportgen.ppt.*
创建演示容器
创建一个 mlreportgen.ppt.Presentation
对象来包含演示文稿。对于此示例,指定输出文件名为 myPresentation
并且不指定模板。
ppt = Presentation('myPresentation.pptx');
由于您没有指定模板,PPT API 使用默认模板。模板定义默认的幻灯片布局和样式。要创建自定义模板,请参阅 设置 PowerPoint 演示模板。您可以使用格式属性和对象覆盖模板定义的默认样式。请参阅演示格式化方法。
添加幻灯片和幻灯片内容
要添加幻灯片,请使用 add
方法并指定模板中可用的幻灯片布局。请参阅设置 PowerPoint 演示模板。此示例使用默认模板中包含的以下幻灯片布局:
Title Slide
Title and Picture
Title and Content
Title and Table
要向幻灯片添加内容,请使用 replace
方法将内容占位符替换为新内容。有关添加和替换内容的更多信息,请参阅 添加和替换演示文稿内容。要使用 replace
方法,您必须指定标识幻灯片布局中的占位符的名称。例如,Title Slide
布局有一个 Title
占位符和一个 Subtitle
占位符。有关如何查找特定幻灯片布局的内容占位符名称的信息,请参阅 访问 PowerPoint 模板元素。
添加标题幻灯片
要添加标题幻灯片,请使用 Title Slide
布局。
titleSlide = add(ppt,'Title Slide');
Title Slide
布局有以下占位符:
Title
Subtitle
用标题文本替换 Title
占位符。
replace(titleSlide,'Title','Create Histogram Plots');
分部分构建标题文本,以便您可以使用不同的字体格式化 histogram
函数名称。
subtitleText = Paragraph('The '); funcName = Text('histogram'); funcName.Font = 'Courier New'; append(subtitleText,funcName); append(subtitleText,' Function');
将 Subtitle
占位符替换为 SubtitleText
中包含的文本。
replace(titleSlide,'Subtitle',subtitleText);
添加带有图片的幻灯片
创建一个用于幻灯片图片的图像文件。
x = randn(10000,1);
h = histogram(x);
saveas(gcf,'myPlot_img.png');
从图像文件创建一个 mlreportgen.ppt.Picture
对象。
plot1 = Picture('myPlot_img.png');
使用 Title and Picture
布局向演示文稿添加图片幻灯片。
pictureSlide = add(ppt,'Title and Picture');
Title and Picture
布局有以下占位符:
Title
Picture
将 Title
占位符替换为标题文本,将 Picture
占位符替换为 plot1
。
replace(pictureSlide,'Title','Histogram of Vector'); replace(pictureSlide,'Picture',plot1);
当演示文稿关闭时,图像文件的内容将被复制到演示文稿中。演示文稿关闭之前请勿删除或覆盖图像文件。如果您的演示程序创建了多个图像文件,请为它们指定唯一的文件名。
添加带有文本的幻灯片
要添加带有文本的幻灯片,请使用 Title and Content
布局。
textSlide = add(ppt,'Title and Content');
Title and Content
布局有以下占位符:
Title
Content
分部分构建标题文本,以便您可以使用不同的字体格式化 histogram
函数名称。
titleText = Paragraph('What You Can Do with '); func = Text('histogram'); func.Font = 'Courier New'; append(titleText,func);
替换 Title
和 Content
占位符。
replace(textSlide,'Title',titleText); replace(textSlide,'Content',{'Create histogram plot of x',... 'Specify:',{'Number of bins','Edges of the bins'},... 'Plot into a specified axes'});
添加带有表格的幻灯片
要添加带有表格的幻灯片,请使用 Title and Table
布局。
tableSlide = add(ppt,'Title and Table');
The Title and Table
布局有以下占位符:
Title
Table
替换 Title
占位符。
replace(tableSlide,'Title','Parameters');
您可以使用多种方法来创建表。请参阅创建和格式化表格。此示例逐行构建表格。
创建一个表作为
mlreportgen.ppt.Table
对象。为表的每一行创建一个
mlreportgen.ppt.TableRow
对象。创建
mlreportgen.ppt.TableEntry
对象并将其追加到表行。
paramTable = Table(); colSpecs(2) = ColSpec('6in'); colSpecs(1) = ColSpec('3in'); paramTable.ColSpecs = colSpecs; tr1 = TableRow(); tr1.Style = {Bold(true)}; tr1te1Text = Paragraph('Value'); tr1te2Text = Paragraph('Description'); tr1te1 = TableEntry(); tr1te2 = TableEntry(); append(tr1te1,tr1te1Text); append(tr1te2,tr1te2Text); append(tr1,tr1te1); append(tr1,tr1te2); tr2 = TableRow(); tr2te1Text = Paragraph('auto'); tr2te1Text.Font = 'Courier New'; tr2te2Text = Paragraph('The default auto algorithm chooses a bin width to '); append(tr2te2Text,'cover the data range and reveal the shape of the distribution.'); tr2te1 = TableEntry(); tr2te2 = TableEntry(); append(tr2te1,tr2te1Text); append(tr2te2,tr2te2Text); append(tr2,tr2te1); append(tr2,tr2te2); tr3 = TableRow(); tr3te1Text = Paragraph('scott'); tr3te1Text.Font = 'Courier New'; tr3te2Text = Paragraph(' Is optimal if the data is close '); append(tr3te2Text,'to being jointly normally distributed. This rule is '); append(tr3te2Text,'appropriate for most other distributions, as well.'); tr3te1 = TableEntry(); tr3te2 = TableEntry(); append(tr3te1,tr3te1Text); append(tr3te2,tr3te2Text); append(tr3,tr3te1); append(tr3,tr3te2); append(paramTable,tr1); append(paramTable,tr2); append(paramTable,tr3);
将 Table
占位符替换为 paramTable
。
replace(tableSlide,'Table',paramTable);
生成并查看演示文稿
close(ppt); rptview(ppt);
另请参阅
mlreportgen.ppt.Presentation
| mlreportgen.ppt.Slide
| mlreportgen.ppt.Paragraph
| mlreportgen.ppt.Text
| mlreportgen.ppt.Table
| mlreportgen.ppt.TableRow
| mlreportgen.ppt.TableEntry