Create a Presentation Generator
You can use the MATLAB® API for PowerPoint® (PPT API) to update and create PowerPoint presentations programmatically. For example, this MATLAB script creates a presentation that has a title page and one content slide
with a bulleted list.
import mlreportgen.ppt.*;
slidesFile = 'mySlides.pptx';
slides = Presentation(slidesFile);
slide1 = add(slides,'Title Slide');
replace(slide1,'Title','My Presentation');
replace(slide1,'Subtitle','Create a Presentation Program');
slide2 = add(slides,'Title and Content');
para = Paragraph('First Content Slide');
para.FontColor = 'blue';
replace(slide2,'Title',para);
replace(slide2,'Content',{'First item','Second item','Third item'});
close(slides);
After you create the presentation, which is named MySlides.pptx,
you can open it. On a Windows® platform, you can open the presentation in MATLAB:
if ispc
winopen(slidesFile);
end
The generated presentation MySlides.pptx includes these two
slides.
Update Presentation Content
PPT API programs generally include code that:
Imports the mlreportgen.ppt API package. To omit the
package name when you invoke PPT API object constructors and method, import
the package.
import mlreportgen.ppt.*;
Creates a Presentation object to:
Hold the presentation contents
Specify the output location for the generated presentation
Indicate the PowerPoint template
The following code creates a presentation using the template from
the presentation in the file mySlides.pptx and
overwrites mySlides.pptx with the new
presentation.
slidesFile = 'mySlides.pptx';
slides = Presentation(slidesFile, slidesFile);
open(slides);
Adds or replaces slide content.
slide2 = slides.Children(2);
contents = find(slide2,'Title');
replace(contents,Paragraph('Modified Content Slide'));
contents = find(slide2,'Content');
datePara = Paragraph('Fourth item: Updated item');
add(contents,datePara);
The PPT API replaces PowerPoint template placeholders with content defined in the program. In
the template, you can interactively add placeholders or rename placeholders
for your program to interact with.
Closes the presentation, which generates the content and formatting of the
presentation.
close(slides);
You can include code to open the presentation on Windows platforms. Use winopen with the name of the file,
which in this case is stored in the slidesFile variable.
if ispc
winopen(slidesFile);
end
The updated slide looks like this:
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.)Two Ways to Use the PPT API
You can create a PPT API program that:
Replaces content in, or adds content to, an existing PowerPoint presentation
Generates a complete PowerPoint presentationAdd Content to an Existing Presentation
To add or update content to an existing presentation without manually updating
the presentation each time content changes, use the PPT API. This approach is
useful when you want to use most of the content and formatting in an existing
presentation.
You can use the PPT API and MATLAB functions to generate content for a presentation from
MATLAB code and Simulink® models.
You can update a presentation by overwriting the presentation file or
create a separate version of the presentation with a different
presentation name.Create a Complete Presentation
To create a complete presentation when you want to use the same content using
multiple PowerPoint templates, use the PPT API.PPT API Applications and PowerPoint Templates
The PPT API uses PowerPoint presentations as templates to generate presentations. Templates allow you to
specify the fixed content and default layout and appearance of the slides in your
presentations. Your MATLAB program can use the PPT API to override the default layout and format of
specific slides.
The template can be an empty presentation or a presentation with slides. You can use the
following as templates for a PPT API presentation:
The default PPT API PowerPoint template
A customized copy of the default PPT API PowerPoint template
An existing PowerPoint presentation whose content
you want to update
A PowerPoint template that you create or update interactively in PowerPointTemplate Elements
PowerPoint templates include several elements that the
PPT API uses to generate a presentation. To customize formatting defined
in a template, modify one or more of these template elements.
PowerPoint Template ElementPurposeSlide mastersApplies the slide master formatting globally to the presentation.
Specifies a layout and formats common to a set of slide layouts
Slide layoutsSpecifies a variant of a slide master layout.
Table stylesSpecifies the default appearance of a table. PowerPoint defines
a standard set of table styles. You cannot modify these styles but
you can use the PPT API to apply these styles to tables you create
and override the styles for particular tables.
PlaceholdersSpecifies an area of a slide layout that you can replace
with text, a list, picture, table, or other content. Every placeholder
has a name. You can use PowerPoint interactively to assign a
name to a placeholder. You can then use the name in your PPT program
to replace the placeholder with content.
Related Topics