How can I add slides to an existing ppt presentation?

31 views (last 30 days)
This is the code I am using but rather then adding a slide it replaces the existing slide:
slides = Presentation('PhaseNoiseData.pptx');
open(slides);
slide = add(slides, 'Title and Picture');
plot1 = Picture('pn_plot.png');
slidetitle = sprintf('data_%s.pptx', datestr(now,'mm-dd-yyyy HH-MM'));
replace(slide, 'Title', slidetitle);
replace(slide, 'Picture',plot1);

Answers (2)

Cris LaPierre
Cris LaPierre on 30 Jul 2021
I found this statement in the documentation:
"To add a slide, use the add method with an mlreportgen.ppt.Presentation object. For example, using the default PPT API template, you can add a slide using the Title and Content slide layout."
import mlreportgen.ppt.*;
slides = Presentation('myPresentation');
slide1 = add(slides,'Title and Content');
Try adding the import line to your code and see if that works.
  2 Comments
Norman Matthews
Norman Matthews on 30 Jul 2021
The import line was in my code just above the code I showed. What I am trying to do is is to add a slide to the same ppt presentation everytime I run my app.
Cris LaPierre
Cris LaPierre on 30 Jul 2021
I may have jumped in over my head. The closest thing I could find that helped was this section of the Add Slide page. Basically, the report generator appears to be designed to create presentations, not modify existing ones. To get it to work, you need to be able to tell the method where to place the slide. Follow the instructions for how to assign a name to a slide in an existing presentation (Step #2, only has to be done once). Then follow the instructions in Step #3 for adding a new slide to the exiting pptx. This can be run multiple times. Each time it places the new slide before the named slide (I tested 3x).
Another option may be to use activeX. It has it's drawbacks as well. You can find a demo script and getting started instructions here.

Sign in to comment.


nailah oliver
nailah oliver on 15 Jan 2022
Edited: nailah oliver on 15 Jan 2022
To add a slide to an existing presentation, use the PowerPoint slides of interest as your powerpoint template when using the Presentation class. For example, your code would instead be:
%{
The second argument of the Presentation class is the template. The
template and the powerpoint are allowed to be the same, and the below
assignment will allow you to retain your old slides (because the old
slides are a part of the template) and your new slides.
%}
slides = Presentation('PhaseNoiseData.pptx','PhaseNoiseData.pptx'); % <-- corrected
open(slides) % not needed if you just want to add slides
slide = add(slides, 'Title and Picture');
plot1 = Picture('pn_plot.png');
slidetitle = sprintf('data_%s.pptx', datestr(now,'mm-dd-yyyy HH-MM'));
replace(slide, 'Title', slidetitle);
replace(slide, 'Picture',plot1);
close(slides) % needed to save slides
You should then see a new 'Title and Picture' slide with all the things you include with all your previous slides still there! Since you are using the slides of interest as your template, MATLAB assumes those existing slides are suppose to be there and therefore won't write over them.

Tags

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!