PowerPoint API duplicate slide

9 views (last 30 days)
TamasM
TamasM on 23 Nov 2021
Answered: Abhiroop Rastogi on 16 Dec 2021
Hi,
I have started to use PowerPoint report generator with the PPT API package.
I managed to add slides by the
mlreportgen.ppt.add()
function based on my template, however I can't seem to find how can I duplicate already existing slides.
Any help appreciated!

Answers (1)

Abhiroop Rastogi
Abhiroop Rastogi on 16 Dec 2021
Hi Tamas,
Currently there is no direct way for duplicating slides using the MATLAB report generator. But there are a couple of workarounds available for doing so, as provided below. Let me go in order starting with creating a new presentation, then moving to the approaches for duplicating slides.
% Starting with importing the package
import mlreportgen.ppt.*
ppt = Presentation('myPresentation.pptx'); % Creating the presentation object
% Creating title slide
titleSlide = add(ppt,'Title Slide');
replace(titleSlide,'Title','Create Presentation');
subtitleText = Paragraph('The ');
funcName = Text('SubText for ppt');
funcName.Font = 'Courier New';
append(subtitleText,funcName);
append(subtitleText,' Function');
replace(titleSlide,'Subtitle',subtitleText);
% Creating the content slide to be duplicated
textSlide1 = add(ppt,'Title and Content');
titleText = Paragraph('What You Can Do with ');
func = Text('presentations');
func.Font = 'Courier New';
append(titleText,func);
replace(textSlide1,'Title',titleText);
replace(textSlide1,'Content',{'Create presentations',...
'Specify:',{'Workflow','Explanations'},...
'Provide examples'});
% Add second slide to the presentation
textSlide2 = add(ppt,'Title and Content');
  • Approach-1: Duplicating contents of "textSlide1" into "textSlide2".
% --- Approach-1 --- %
% Duplicate first slide content into the second slide by copying all the
% placeholder's content including content formatting.
for iPlaceholder = 1:length(textSlide1.Children)
currPlaceholder = textSlide1.Children(iPlaceholder);
for iChildPara = 1:length(currPlaceholder.Children)
slide1ParaObj = currPlaceholder.Children(iChildPara);
slide2ParaObj = Paragraph(slide1ParaObj.Children(1).Content); % Copy para content
% Copy para properties Level-1
paraObjProp1 = properties(slide1ParaObj);
for iParaProp = 1:(length(paraObjProp1)-4)
slide2ParaObj.(paraObjProp1{iParaProp}) = slide1ParaObj.(paraObjProp1{iParaProp});
end
% Copy para properties Level-2
for iiChildPara = 1:length(slide1ParaObj.Children)
if iiChildPara>1
% appending extra children for the paragraph
append(slide2ParaObj,slide1ParaObj.Children(iiChildPara).Content);
end
paraObjProp2 = properties(slide1ParaObj.Children(iiChildPara));
for iParaProp = 1:(length(paraObjProp2)-4)
slide2ParaObj.Children(iiChildPara).(paraObjProp2{iParaProp})...
= slide1ParaObj.Children(iiChildPara).(paraObjProp2{iParaProp});
end
end
textSlide2.Children(iPlaceholder).add(slide2ParaObj);
end
end
close(ppt);
rptview(ppt);
  • Approach-2: An array of objects is created containing identical content and are placed consecutively.
% --- Approach-2 --- %
% Duplicate slides consecutively with the same content
for i=1:numSlides
textSlide(i) = add(ppt,'Title and Content');
titleText = Paragraph('What You Can Do with ');
func = Text('presentations');
func.Font = 'Courier New';
append(titleText,func);
replace(textSlide(i),'Title',titleText);
replace(textSlide(i),'Content',{'Create presentations',...
'Specify:',{'Workflow','Explanations'},...
'Provide examples'});
end
close(ppt);
rptview(ppt);
Note: The duplicating of slides using Approach-1 works for text content only, as duplicating slides feature is under development.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!