Replace an image in existing powerpoint with .jpg

10 次查看(过去 30 天)
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.
  1 个评论
Rik
Rik 2022-8-1
improve your chances of getting an answer. Have a read here and here. It will greatly improve your chances of getting an answer.
What you describe consists of many individual steps. It is not clear which one you tried and which is causing you issues to implement.

请先登录,再进行评论。

采纳的回答

Madheswaran
Madheswaran 2025-1-22
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 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Report Generator 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by