Create a Presentation Programmatically

This presentation example shows some common tasks involved in creating a presentation with the PPT API. This example produces these slides:

To use the PPT API to create a complete PowerPoint® presentation programmatically, you:

  • Set up an empty PowerPoint presentation as a template for the presentation.

  • In MATLAB®, import the mlreportgen.ppt PPT API package.

  • Create a Presentation object that contains the presentation code.

  • Add slides based on slide layouts in the template.

  • Add content to the slides.

  • Generate the presentation.

Tip

To see another example of a PPT API program in MATLAB, enter population_slides. (To run this example on Linux®, you must have Open Office software installed.)

Set Up a Template

A PPT API program uses a PowerPoint presentation as a template to generate a presentation. When you create a complete presentation programmatically, use an empty template. If slides in the template have content (such as text or tables), the content appears in the presentation that the PPT API program generates.

The PPT API provides a default PowerPoint template. You can use the PPT API to make a copy of the default template, which you then can customize to use with your PPT API program. This code creates a template called myTemplate, which is a copy of the default PPT API template.

import mlreportgen.ppt.*
slidesFile = 'myTemplate.pptx';
slides = Presentation('myTemplate');

open(slides);

close(slides);

Open the myTemplate.pptx file. On a Windows® platform, you can open the presentation in MATLAB:

if ispc
    winopen(slidesFile);
end

To see template elements, such as the slide master and slide layouts, in PowerPoint View pane, click Slide Master.

Use PowerPoint interactively to customize the template. To set default formatting for the whole presentation, customize a slide master. To set default formatting for a specific kind of slide, customize a slide layout. For example, you can use the slide master to set up the template to use bold text for slide titles.

  1. In the slide layout, right-click in Click to edit Master title style box.

  2. From the context menu, select B (bold). Also select the button to center the text.

  3. Save and close the template.

Import the PPT API Package

All PPT API class names include the prefix mlreportgen.ppt. To avoid including the package name when you invoke PPT API object constructors and method, import the package. Insert this statement at the beginning of a PPT API program.

import mlreportgen.ppt.*;

Note

The import line is the first line in this example program. This example creates a PPT API program in sections, and so you use the import command only once. To view the complete program, click myNewPPTPresentation program.

Create the Presentation Object

Create a Presentation object. Specify:

  • myNewPPTPresentation.pptx as the output file for the generated presentation.

  • myTemplate.pptx as the PowerPoint template.

slidesFile = 'myNewPPTPresentation.pptx';
slides = Presentation(slidesFile,'myTemplate');

Add a Presentation Title Slide

To add a slide programmatically, specify a slide layout in the template. To see the names of the slide layouts, in the PowerPoint Slide Master tab, hover over a slide layout.

The myTemplate template includes a Title Slide slide layout for the presentation title slide. To add a slide using the Title Slide layout, use the add method with slides, which is a Presentation object. In the slide layout name, do not include the word Layout, which appears at the end of slide layout names when you hover over slide layouts.

presentationTitleSlide = add(slides,'Title Slide');

To add content to the slide, first find out the names of the content objects in the slide layout.

  1. In PowerPoint, stay in the slide master view and select the Home tab.

  2. Click Select > Selection Pane.

  3. In the slide layout, click the slide layout content item whose name you want.

Specify a title and a subtitle. Specify the slide, the name of the content objects you want to replace, and the text for the title and subtitle. For the subtitle, to specify a different font for the word histogram, use a Paragraph object for that text.

replace(presentationTitleSlide,'Title','Create Histogram Plots');

subtitleText = Paragraph('The ');
funcName = Text('histogram');
funcName.Font = 'Courier New';

append(subtitleText,funcName);
append(subtitleText,' Function');
replace(presentationTitleSlide,'Subtitle1',subtitleText);

Add a Slide with a Picture

To add a picture to a slide, create a Picture object, specifying an image file. This example creates a MATLAB plot and saves the plot as an image file. You can add the picture to a slide. Use a Title and Content slide layout and add a title and picture.

x = randn(10000,1);
h = histogram(x);

saveas(gcf,'myPlot_img.png');

plot1 = Picture('myPlot_img.png');

pictureSlide = add(slides,'Title and Content');
replace(slides,'Title','Histogram of Vector');
contents = find(pictureSlide,'Content');
replace(contents(1),plot1);

Add a Slide with Text

Depending on the slide layout, PowerPoint formats the text you add as a paragraph, a bulleted list, or a numbered list. This example creates another instance of a Title and Content slide, which formats the text as a bulleted list. You can use a nested cell array to specify levels for bullets.

textSlide = add(slides,'Title and Content');

titleText2 = Paragraph('What You Can Do with ');
func = Text('histogram');
func.Font = 'Courier New';
append(titleText2,func);
contents = find(textSlide,'Title');
replace(contents(1),titleText2);

contents = find(textSlide,'Content');
replace(contents(1),{'Create histogram plot of x',...
'Specify:',{'Number of bins','Edges of the bins'},...
'Plot into a specified axes'});

Add a Slide with a Table

You can use several approaches to add a table to a slide. This example shows how to build a table row by row.

  • Create a Table object.

  • Create a TableRow object for each row of the table.

  • Create TableEntry objects and append them to table rows.

  • Add the table to a slide.

tableSlide = add(slides,'Title and Content');
contents = find(tableSlide,'Title');
titleText3 = Paragraph('Parameters');
replace(contents(1),titleText3);

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);

contents = find(tableSlide,'Content');
replace(contents(1),paramTable);

Generate and Open the Presentation

Generate the PowerPoint presentation. Use a close method with a Presentation object.

close(slides);

Open myNewPPTPresentation.pptx. On a Windows platform, you can open it in MATLAB:

if ispc
    winopen(slidesFile);
end

Code for myNewPPTPresentation

Here is the complete PPT API program to create myNewPPTPresentation.

Note

The myTemplate.pptx file must be in the current folder. If it is not, see Set Up a Template.

import mlreportgen.ppt.*;

slidesFile = 'myNewPPTPresentation.pptx';
slides = Presentation(slidesFile,'myTemplate');

% Add a title slide
presentationTitleSlide = add(slides,'Title Slide');
replace(presentationTitleSlide,'Title','Create Histograms Plots');

subtitleText = Paragraph('The ');
funcName = Text('histogram');
funcName.Font = 'Courier New';
>> append(subtitleText,funcName);
append(subtitleText,' Function');
replace(presentationTitleSlide,'Subtitle',subtitleText);

% Add a picture slide
x = randn(10000,1);
h = histogram(x);

saveas(gcf,'myPlot_img.png');

plot1 = Picture('myPlot_img.png');

pictureSlide = add(slides,'Title and Content');
replace(slides,'Title','Histogram of Vector');
contents = find(pictureSlide,'Content');
replace(contents(1),plot1);

% Add a text slide
textSlide = add(slides,'Title and Content');

titleText2 = Paragraph('What You Can Do with ');
func = Text('histogram');
func.Font = 'Courier New';
append(titleText2,func);
contents = find(textSlide,'Title');
replace(contents(1),titleText2);

contents = find(textSlide,'Content');
replace(contents(1),{'Create histogram plot of x',...
'Specify:',{'Number of bins','Edges of the bins'},...
'Plot into a specified axes'});

% Add a table slide
tableSlide = add(slides,'Title and Content');
contents = find(tableSlide,'Title');
titleText3 = Paragraph('Parameters');
replace(contents(1),titleText3);

paramTable = Table();
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('Scott''s rule 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);

contents = find(tableSlide,'Content');
replace(contents(1),paramTable);

% Generate and open the presentation
close(slides);

if ispc
    winopen(slidesFile);
end

Related Topics