Main Content

Update Presentation Content Programmatically

You can use the PPT API to update content programmatically in an existing PowerPoint® presentation.

Generate the Existing Presentation

This example updates content in a PowerPoint presentation myPresentation. To generate the presentation, run the example in Create a Presentation Programmatically. Although you create the presentation programmatically, after you generate it, the presentation is like any other PowerPoint presentation. The presentation includes four slides:

  • Title slide with the title Create Histogram Plots

  • A histogram of a vector

  • Slide with the title What You Can Do with histogram

  • Histogram function parameters

The generated slides in PowerPoint

To use the PPT API to update content in an existing PowerPoint presentation programmatically, you:

  • Set up the PowerPoint presentation by naming content objects that you want to replace. If you want to add new content, insert placeholders in the presentation for that content.

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

  • Create a Presentation object that uses the existing presentation as the template for updated version.

  • Replace any existing slide content that you want to update.

  • Add slides any new slides.

  • Generate the presentation.

Updates to the Presentation

In this example, you use the PPT API to make these changes to the myPresentation presentation:

  • Replace the picture on the second slide.

  • Replace the text on the third slide.

  • Replace the table on the fourth slide.

  • Insert a new slide before the slide with the plot.

Here is the updated presentation:

The second slide is now a description of histogram plots. The plot in the third slide is a histogram with specified bin edges. The fourth slide lists related functions.

Set Up the Existing Presentation

A PPT API program uses a PowerPoint template to generate a presentation. When you update an existing PowerPoint presentation programmatically, use that presentation as the template for the updated presentation. To update content in the Slide objects, use the PPT API.

  1. Open the myPresentation presentation. In PowerPoint, click View > Normal.

  2. View the names of content objects in the slides. In the Home tab, click Select > Selection Pane. When you click content in a slide, the Selection pane highlights the name of the content object.

    The PowerPoint Normal View with the "Histogram Of Vector" slide selected and the Picture name highlighted in the Selection pane.

  3. Rename content objects. In the PowerPoint Selection pane, click in the content name box and replace the current name with the name you want. Use these unique names to update content objects.

    • In the second slide, change the Title object name to Histogram and the Picture object name to HistBins.

    • In the third slide, change Title to RelatedFuncs. Change Content to FuncList.

    • In the fourth slide, change Table to ParamTable.

Import the PPT API namespace

All PPT API class names include the prefix mlreportgen.ppt. To avoid the need to include the prefix in your code, insert this statement at the beginning of a PPT API program.

import mlreportgen.ppt.*

Note

The import line is the first line in the example program. This example creates a PPT API program in sections and therefore does not show the import command. To view the complete program, click myUpdatedPresentation program.

Create the Presentation Object

Create a Presentation object. Specify:

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

  • myPresentation.pptx as the PowerPoint template. Use the presentation file that you want to update as the template file.

ppt = Presentation('myUpdatedPresentation.pptx','myPresentation.pptx');
open(ppt);

Specifying a different name for the output file preserves the original presentation. If you want to overwrite the existing presentation, you can use the template file name as the file name for the output file.

Replace a Picture

Change the title of the second slide. Create a Picture object to replace the existing picture. You can use a find method with the Presentation object to find content objects named HistBins and Histogram (the unique names you specified using PowerPoint).

histTitle = Paragraph('Histogram with Specified Bin Edges');
replace(ppt,'Histogram',histTitle);

x = randn(1000,1);
edges = [-10 -2:0.25:2 10];
h = histogram(x,edges);
saveas(gcf,'hist_plot.png');

plotEdges = Picture('hist_plot.png');

replace(ppt,'HistBins',plotEdges);

Replace Text with Links

Change the title of the third slide. Create text to replace the existing text. The text includes links to the MathWorks® online documentation. Append ExternalLink objects to Paragraph objects, and replace the slide content using a cell array of the Paragraph objects.

funcsTitle = Paragraph('Related Functions');
replace(ppt,'RelatedFuncs',funcsTitle);

histCounts = Paragraph();
histCountsLink = ExternalLink...
('https://www.mathworks.com/help/matlab/ref/histcounts.html','histcounts');
append(histCounts,histCountsLink);

fewerbins = Paragraph();
fewerbinsLink = ExternalLink...
('https://www.mathworks.com/help/matlab/ref/matlab.graphics.chart.primitive.histogram.fewerbins.html','fewerbins');
append(fewerbins,fewerbinsLink);

replace(ppt,'FuncList',{histCounts,fewerbins});

Replace a Table

To create a table, create a Table object. In the Table constructor, you can specify a cell array of values for the table cells. To get bold text for the top row, include Paragraph objects as the first three elements of the cell array. Then replace the table.

long = Paragraph('Long Name');
long.Bold = true;
short = Paragraph('Short Name');
short.Bold = true;
rgb = Paragraph('RGB triplet');
rgb.Bold = true;

table2 = Table({long,short,rgb;'yellow','y','[1 1 0]';'green','g','[1 0 1] '});

contents = find(ppt,'ParamTable');
replace(ppt,'ParamTable',table2);

Insert a New Slide

You can use the PPT API to insert a new slide in an existing presentation and you can specify the numerical location of the slide. For example, this code makes a new slide the fifth slide in a presentation.

newSlide = add(ppt,'Title and Content',5);

However, to have a slide precede a specific slide, even if later you add or remove other slides, you can specify a reference slide. To use this approach when updating an existing PowerPoint presentation, use the PPT API to name the reference slide. Use the name of the reference slide when you insert a new slide.

ppt.Children(2).Name = 'ReferenceSlide';

refSlide = find(ppt,'ReferenceSlide');
introSlide = add(ppt,'Title and Content',refSlide);

contents = find(introSlide,'Title');
replace(contents(1),'Histogram Plots');

introText = Paragraph('You can use the ');
code = Text('histogram');
code.Font = 'Courier New';
append(introText,code);
append(introText,'  function to create many types of plots.');

contents = find(introSlide,'Content');
replace(contents(1),introText);

Generate and View the Presentation

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

close(ppt);
rptview(ppt);

Code for myUpdatedPresentation

Here is the complete PPT API program to create the myUpdatedPresentation presentation.

Note

This code requires that the myPresentation.pptx file be in your current folder. To generate that presentation, run the example in Create a Presentation Programmatically. Before you run the code for myUpdatedPresentation, be sure that the existing presentation includes the changes described in Set Up the Existing Presentation.

import mlreportgen.ppt.*;

ppt = Presentation('myUpdatedPresentation.pptx','myPresentation.pptx');
open(ppt);

histTitle = Paragraph('Histogram with Specified Bin Edges');
replace(ppt,'Histogram',histTitle);

x = randn(1000,1);
edges = [-10 -2:0.25:2 10];
h = histogram(x,edges);
saveas(gcf,'hist_plot.png');

plotEdges = Picture('hist_plot.png');

replace(ppt,'HistBins',plotEdges)

funcsTitle = Paragraph('Related Functions');
replace(ppt,'RelatedFuncs',funcsTitle);

histCounts = Paragraph();
histCountsLink = ExternalLink...
('https://www.mathworks.com/help/matlab/ref/histcounts.html','histcounts');
append(histCounts,histCountsLink);

fewerbins = Paragraph();
fewerbinsLink = ExternalLink...
('https://www.mathworks.com/help/matlab/ref/matlab.graphics.chart.primitive.histogram.fewerbins.html','fewerbins');
append(fewerbins,fewerbinsLink);

replace(ppt,'FuncList',{histCounts,fewerbins});

long = Paragraph('Long Name');
long.Bold = true;
short = Paragraph('Short Name');
short.Bold = true;
rgb = Paragraph('RGB triplet');
rgb.Bold = true;

table2 = Table({long,short,rgb;'yellow','y','[1 1 0]'; 'green', 'g','[1 0 1] '});

contents = find(ppt,'ParamTable');
replace(ppt,'ParamTable',table2);


ppt.Children(2).Name = 'ReferenceSlide';

refSlide = find(ppt,'ReferenceSlide');
introSlide = add(ppt,'Title and Content',refSlide(1));

contents = find(introSlide,'Title');
replace(contents(1),'Histogram Plots');

introText = Paragraph('You can use the ');
code = Text('histogram ');
code.Style = {FontFamily('Courier New')};
append(introText,code);
append(introText,'function to create many types of plots.');

contents = find(introSlide,'Content');
replace(contents(1),introText);

close(ppt);
rptview(ppt);

Related Topics