How do I add multiple .PNG files to an existing PowerPoint (.pptx) file using a for loop?

Using MATLAB 2015a, I've been attempting to add numerous .png files to an existing PowerPoint 2010 presentation (.pptx) using the following code;
%
% Clear out all workspace variables, close all open figures, and clear the
% command window.
clear all;
close all;
clc;
% List all current folder contents ending with .png. Resulting names will
% appear in the order returned by the operating system.
files = dir('*.png');
% Create Common Object Model (COM) server so MATLAB can export data to
% PowerPoint
g = actxserver('powerpoint.application');
% Open PowerPoint and make it visible
g.Visible = 1;
Presentation = g.Presentation;
% Prompt the user for the PowerPoint file to amend
[fn, pn] = uigetfile('*.pptx', 'Select PowerPoint File To Amend');
filemane = fullfile(pn, fn);
Presentation = invoke(Presentation, 'open', filename);
% Get current number of slides
slide_count = get(Presentation.Slides, 'Count');
% Export all PNGs in the current directory to the PowerPoint file specified
% above. The following slides will be added to the END of the PowerPoint
% file. All slides will have a common title.
for i=1:length(files)
slide_count = int32(double(slide_count)+1);
slide = invoke(Presentation.Slides, 'Add', slide_count{i}, 11);
set(slide.Shapes.Title.Textframe.Textrange, 'Text', 'SomeTitle');
Image{i} = slide.Shapes.AddPicture(',Current Directory>\files(i).name', 'msoFalse', 'msoTrue', 0, 80, 720, 440);
end
% Save the amended PowerPoint presentation to the current directory
Presentation.SaveAs('<Currrent directory>\DRAFT.pptx');
% Close PowerPoint as a COM automation server
g.Quit;
g.delete;
However, I keep getting the following error:
Invoke Error, Dispatch Exception. Description: The specified file wasn't found.
The line in question is this:
Image{i} = slide.Shapes.AddPicture('C:\Users\onstottb\Documents\MATLAB\files(i).name', 'msoFalse', 'msoTrue', 0, 80, 720, 440);
It appears I am implementing something wrong, but I'm not sure where.
Any advice is greatly appreciated.
Thank you.

 Respuesta aceptada

%removed the clear all, close all, clc, as they are irrelevant here and can only cause problems
project_dir = pwd(); %new
% List all current folder contents ending with .png. Resulting names will
% appear in the order returned by the operating system.
files = dir( fullfile(project_dir, '*.png')); %changed
% Create Common Object Model (COM) server so MATLAB can export data to
% PowerPoint
g = actxserver('powerpoint.application');
% Open PowerPoint and make it visible
g.Visible = 1;
Presentation = g.Presentation;
% Prompt the user for the PowerPoint file to amend
[fn, pn] = uigetfile('*.pptx', 'Select PowerPoint File To Amend');
filename = fullfile(pn, fn); %changed
Presentation = invoke(Presentation, 'open', filename);
% Get current number of slides
slide_count = get(Presentation.Slides, 'Count');
% Export all PNGs in the current directory to the PowerPoint file specified
% above. The following slides will be added to the END of the PowerPoint
% file. All slides will have a common title.
for i=1:length(files)
slide_count = int32(double(slide_count)+1);
slide = invoke(Presentation.Slides, 'Add', slide_count{i}, 11);
set(slide.Shapes.Title.Textframe.Textrange, 'Text', 'SomeTitle');
slidefile = fullfile(project_dir, files(i).name); %new
Image{i} = slide.Shapes.AddPicture(slidefile, 'msoFalse', 'msoTrue', 0, 80, 720, 440); %changed
end
% Save the amended PowerPoint presentation to the current directory
outfile = fullfile(project_dir, 'DRAFT.pptx'); %new
Presentation.SaveAs(outfile); %changed
% Close PowerPoint as a COM automation server
g.Quit;
g.delete;

10 comentarios

Walter, thank you for taking a look at this. I flat out forgot about taking the approach you did. After making 2 minor mods, this code runs like a champ. The mods are:
files = dir( fullfile(project_dir, '*.png'));
slide = invoke(Presentation.Slides, 'Add', slide_count, 11);
Thanks again!
I am also doing similar job of adding .png files in existing ppt.
This code helps me but there is some little change in my activity that I want to add .png file in existing ppt slides with information.
This code add .png file to the end of ppt but how can I add .png file to existing slides
suggest me changes in code
Thank you in advance!!
It appears to me that where I invoke 'Add' that instead you would invoke 'Slides' and a slide number in order to select the slide. After that you would AddPicture or ReplacePicture (guessing at the function name)
Note: I do not have PowerPoint so I have never done any of this myself.
First of all, Thanks for helping me!!
I tried in this way but I coundnt get it.error gives as, 'Invoke Error: Unknown name or named argument'
So can you pl. elaborate the code for it
slide = invoke(Presentation.Slides, 'Slides', slide_number_to_affect);
as a guess.
I do not have Powerpoint (I have not even booted a windows system for months) so I cannot test this.
Hi,
I got the answer for adding images to existing slide but I cant specify its particular position.
I dont get how position is specified in this following line:
Image{i} = slide.Shapes.AddPicture(slidefile, 'msoFalse', 'msoTrue', 0, 80, 720, 440);
Thank u in advance!!
https://docs.microsoft.com/en-us/office/vba/api/powerpoint.shapes.addpicture
Is there any command to open the amended PowerPoint presentation after saving it to specified location?
winopen(path_to_file)
Or with the report generator you can control showing it/playing it etc:
https://www.mathworks.com/help/rptgen/ug/mlreportgen.utils.pptpres-class.html

Iniciar sesión para comentar.

Más respuestas (1)

I'd strongly encourage you to look at the MATLAB Report Generator for this which has the ability to easily add or replace content in a presentation.

Categorías

Más información sobre MATLAB Report Generator en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 20 de En. de 2016

Comentada:

el 10 de Feb. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by