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 the PowerPoint presentation myNewPPTPresentation. Although you create the presentation programmatically, after you generate it, the presentation is like any other PowerPoint presentation. To generate the presentation, click myNewPPTPresentation program and execute the code in MATLAB®. The presentation includes four slides:

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 package.

  • 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 myNewPPTPresentation 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.

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 myNewPPTPresentation 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.

  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 Content object name to HistBins.

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

    • In the fourth slide, change Content to ParamTable.

Import the PPT API Package

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.

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

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

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(slides,'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(slides,'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(slides,'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/fewerbins.html','fewerbins');
append(fewerbins,fewerbinsLink);

replace(slides,'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(slides,'ParamTable');
replace(slides,'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(slides,'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.

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

refSlide = find(slides,'ReferenceSlide');
introSlide = add(slides,'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 Open the Presentation

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

close(slides);

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

if ispc
    winopen(slidesFile);
end

Code for myUpdatedPresentation

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

Note

This code requires that the myNewPPTPresentation.pptx file be in your current folder. To generate that presentation, click myNewPPTPresentation program and execute the code in MATLAB. 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.*;

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

histTitle = Paragraph('Histogram with Specified Bin Edges');
replace(slides,'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(slides,'HistBins',plotEdges)

funcsTitle = Paragraph('Related Functions');
replace(slides,'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/fewerbins.html','fewerbins');
append(fewerbins,fewerbinsLink);

replace(slides,'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(slides,'ParamTable');
replace(slides,'ParamTable',table2);


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

refSlide = find(slides,'ReferenceSlide');
introSlide = add(slides,'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(slides);
if ispc
    winopen(slidesFile);
end

Related Topics