Replace an image in existing powerpoint with .jpg
16 views (last 30 days)
Show older comments
Sarah Reed
on 29 Jul 2022
Answered: Madheswaran
on 22 Jan 2025 at 10:56
I have a presentation with 4 slides. Each slide has a picture and 3 text boxes. I want to replace the pictures on each slide with different pictures I have stored in another folder, but I don't know how to call out the slide and picture I want to replace and perform the action. Also, I would like to replace the 3 text boxes with new text.
Accepted Answer
Madheswaran
on 22 Jan 2025 at 10:56
Hi Sarah,
You can automate PowerPoint using MATLAB's ActiveX interface to replace both images and text across multiple slides. Before implementing the solution, it's important to note that you need to know the shape indices of the text boxes and images in your presentation - in this solution, I'm assuming the first three shapes are text boxes and the fourth shape is an image.
Here's the simple MATLAB code replacing images and texts in the slides:
pptApp = actxserver('PowerPoint.Application');
pptApp.Visible = 1;
pptFileName = fullfile(pwd, 'try.pptx');
ppt = pptApp.Presentations.Open(pptFileName);
% Loop through all 4 slides
for slideNum = 1:4
slide = ppt.Slides.Item(slideNum);
% Modify the text and file names as per your need
newText1 = sprintf('This is new text for box 1 on slide %d', slideNum);
newText2 = sprintf('This is new text for box 2 on slide %d', slideNum);
newText3 = sprintf('This is new text for box 3 on slide %d', slideNum);
newImagePath = fullfile('images/', sprintf('%d.jpg', slideNum));
for i = 1:slide.Shapes.Count
shape = slide.Shapes.Item(i);
shapeType = char(shape.Type);
if strcmp(shapeType, 'msoTextBox')
textFrame = shape.TextFrame;
switch i
case 1
textFrame.TextRange.Text = newText1;
case 2
textFrame.TextRange.Text = newText2;
case 3
textFrame.TextRange.Text = newText3;
end
elseif strcmp(shapeType, 'msoPicture')
left = shape.Left;
top = shape.Top;
width = shape.Width;
height = shape.Height;
shape.Delete;
slide.Shapes.AddPicture(...
newImagePath, ...
0, -1, ...
left, top, ...
width, height);
end
end
end
ppt.Save;
ppt.Close;
pptApp.Quit;
pptApp.delete;
The code iterates through each slide and automatically identifies text boxes and pictures based on their shape type, then replaces them with new content while preserving their original positions.
For more information, refer to the following documentation: https://mathworks.com/help/matlab/ref/actxserver.html
Hope this helps!
0 Comments
More Answers (0)
See Also
Categories
Find more on MATLAB Report Generator in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!