Using MATLAB to modify your PPT
Create a Presentation Programmatically
This example shows how to create a PowerPoint® presentation by using the MATLAB® API for PowerPoint (PPT API).
The example generates these slides:By your image host
To programmatically create a presentation:
- Import the PPT API package.
- Create a presentation container.
- Add slides.
- Add content to the slides.
- Generate the presentation.
Import the PPT API Package
The PPT API classes belong to the mlreportgen.ppt
package. Import this package so that you do not have to include the package name when you call the PPT API object constructors and methods.
1import mlreportgen.ppt.*
Create a Presentation Container
Create an mlreportgen.ppt.Presentation
object to contain the presentation. For this example, specify that the output file name is myPresentation
and do not specify a template.
1ppt = Presentation('myPresentation.pptx','TCMP_templete.pptx');
Because you did not specify a template, the PPT API uses the default template. A template defines the default slide layouts and styles. To create a custom template, see Set Up a PowerPoint Template . You can override the default style defined by a template by using format properties and objects. See Presentation Formatting Approaches .
Add Slides and Slide Content
To add a slide, use the add
method and specify a slide layout that is available in the template. See Set Up a PowerPoint Template
. This example uses these slide layouts that are included with the default template:
Title Slide
Title and Picture
Title and Content
Title and Table
To add content to a slide, use the replace
method to replace content placeholders with the new content. For more information about adding and replacing content, see Add and Replace Presentation Content
. To use the replace
method, you must specify the name that identifies a placeholder in the slide layout. For example, the Title Slide
layout has a Title
placeholder and a Subtitle
placeholder. For information about how to find the content placeholder names for a particular slide layout, see Access PowerPoint Template Elements
.
Add a Title Slide
To add a title slide, use the Title Slide
layout.
1titleSlide = add(ppt,'Title Slide');
The Title Slide
layout has these placeholders:
Title
Subtitle
Replace the Title
placeholder with the title text.
1replace(titleSlide,'Title','TCMP PROJECT');
Build the title text in pieces so that you can format the histogram
function name in a different font.
1subtitleText = Paragraph('The tour of ');
2ProjectName = Text('Topological Condensed Matter Physic');
3ProjectName.Font = 'Microsoft YaHei UI';
4append(subtitleText,ProjectName);
Replace the Subtitle
placeholder with the text contained in SubtitleText
.
1replace(titleSlide,'Subtitle',subtitleText);
Author Email and Date
1Author = 'Xu-Tao Zeng';
2replace(titleSlide,'Author',Paragraph(Author));
3Email = 'parkman@buaa.edu.cn';
4Emaillink = ExternalLink(Email,Email);
5replace(titleSlide,'Email',Paragraph(Emaillink));
6replace(titleSlide,'Date',Paragraph(date));
7%append(subtitleText,["";"Xu-Tao Zeng"]);
Add a Slide with a Picture
Create an image file to use for the slide picture.
1syms kx ky real;
2E = sqrt(kx^2+ky^2);
3h = fsurf([-E;E]);
4saveas(gcf,'DiracCone.png');
By your image host
Create an mlreportgen.ppt.Picture
object from the image file.
1plot1 = Picture('DiracCone.png');
Add a picture slide to the presentation by using a Title and Picture
layout.
1pictureSlide = add(ppt,'Title and Picture 1');
The Title and Picture
layout has these placeholders:
Title
Picture
Replace the Title
placeholder with the title text and the Picture
placeholder with plot1
.
1replace(pictureSlide,'Title','Dirac Cone');
2replace(pictureSlide,'Fig1',plot1);
The contents of an image file are copied into the presentation when the presentation is closed. Do not delete or overwrite the image file before the presentation is closed. If your presentation program creates multiple image files, give them unique file names.
Add a Slide with Text
To add a slide with text, use the Title and Content
layout.
1textSlide = add(ppt,'Title and Content 1');
The Title and Content
layout has these placeholders:
Title
Content
Build the title text in pieces so that you can format the histogram
function name in a different font.
1titleText = Paragraph('What You Can Do with ');
2func = Text('Dirac Cone');
3func.Font = 'Courier New';
4append(titleText,func);
Replace the Title
and Content
placeholders.
1replace(textSlide,'Title',titleText);
2replace(textSlide,'Content',{'Reveal the physic of a model',...
3'Inculding:',{'Fermi Velocity','Band Inversion'},...
4'Analyze the mass term.'});
Add a Slide with a Table
To add a slide with a table, use the Title and Table
layout.
1tableSlide = add(ppt,'Title and Table 1');
The Title and Table
layout has these placeholders:
Title
Table
Replace the Title
placeholder.
1replace(tableSlide,'Title','Parameters');
You can use several approaches to create a table. See Create and Format Tables . This example builds a table row by row.
- Create a table as an
mlreportgen.ppt.Table
object. - Create an
mlreportgen.ppt.TableRow
object for each row of the table. - Create
mlreportgen.ppt.TableEntry
objects and append them to the table rows.
1paramTable = Table();
2colSpecs(2) = ColSpec('6in');
3colSpecs(1) = ColSpec('3in');
4paramTable.ColSpecs = colSpecs;
5
6tr1 = TableRow();
7tr1.Style = {Bold(true)};
8
9tr1te1Text = Paragraph('Value');
10tr1te2Text = Paragraph('Description');
11tr1te1 = TableEntry();
12tr1te2 = TableEntry();
13append(tr1te1,tr1te1Text);
14append(tr1te2,tr1te2Text);
15append(tr1,tr1te1);
16append(tr1,tr1te2);
17
18tr2 = TableRow();
19tr2te1Text = Paragraph('auto');
20tr2te1Text.Font = 'Courier New';
21tr2te2Text = Paragraph('The default auto algorithm chooses a bin width to ');
22append(tr2te2Text,'cover the data range and reveal the shape of the distribution.');
23tr2te1 = TableEntry();
24tr2te2 = TableEntry();
25append(tr2te1,tr2te1Text);
26append(tr2te2,tr2te2Text);
27append(tr2,tr2te1);
28append(tr2,tr2te2);
29
30tr3 = TableRow();
31tr3te1Text = Paragraph('scott');
32tr3te1Text.Font = 'Courier New';
33tr3te2Text = Paragraph(' Is optimal if the data is close ');
34append(tr3te2Text,'to being jointly normally distributed. This rule is ');
35append(tr3te2Text,'appropriate for most other distributions, as well.');
36tr3te1 = TableEntry();
37tr3te2 = TableEntry();
38append(tr3te1,tr3te1Text);
39append(tr3te2,tr3te2Text);
40append(tr3,tr3te1);
41append(tr3,tr3te2);
42
43append(paramTable,tr1);
44append(paramTable,tr2);
45append(paramTable,tr3);
Replace the Table
placeholder with paramTable
.
1
2replace(tableSlide,'Table',paramTable);
Generate and View the Presentation
1close(ppt);
2rptview(ppt);
Copyright 2019 The MathWorks, Inc.