PNG file saving in power point presentation

10 views (last 30 days)
Joydeb Saha
Joydeb Saha on 31 May 2020
Answered: Madheswaran on 26 Jan 2025 at 14:11
clear
clc
%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;
If i have 4 PNG images in a folder , how can i save (present) those in a single slide in power point using MATLAB code? I am using the code below but showing some error.

Answers (1)

Madheswaran
Madheswaran on 26 Jan 2025 at 14:11
Hi Joydeb,
To display all the images in a single slide, you need to define specific positions for each image in a 2x2 grid layout. Here's a working solution:
project_dir = pwd();
files = dir('images/*.png'); % path to your images folder
g = actxserver('powerpoint.application');
g.Visible = 1;
Presentation = g.Presentation;
[fn, pn] = uigetfile('*.pptx', 'Select PowerPoint File');
filename = fullfile(pn, fn);
Presentation = invoke(Presentation, 'open', filename);
slide_count = get(Presentation.Slides, 'Count');
% Modify the layout as per your requirement
% here I used the 6th layout from my CustomLayout
layout = Presentation.SlideMaster.CustomLayouts.Item(6);
slide = Presentation.Slides.AddSlide(slide_count + 1, layout);
set(slide.Shapes.Title.Textframe.Textrange, 'Text', 'SomeTitle');
positions = [
0 80 360 270;
360 80 360 270;
0 350 360 270;
360 350 360 270
];
for i = 1:min(4, length(files))
slide.Shapes.AddPicture(fullfile(project_dir, 'images', files(i).name), 'msoFalse', 'msoTrue', ...
positions(i,1), positions(i,2), positions(i,3), positions(i,4));
end
Presentation.SaveAs(fullfile(project_dir, 'DRAFT.pptx'));
g.Quit;
g.delete;
This code creates a new slide at the end of an existing PowerPoint presentation and arranges up to four PNG images in a 2x2 grid layout using predefined positions for each image.
Hope this helps!

Categories

Find more on MATLAB Report Generator in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!