Create Text Box in PowerPoint file
40 views (last 30 days)
Show older comments
Hi everyone,
I have the MATLAB Report Generator.
i want to create a powerpoint file with just one slide. In this slide, I want to add text boxes in some exact locations and some text inside the text boxes.
How do I do that?
Kevin
0 Comments
Answers (2)
Kevin Holly
on 18 Feb 2022
The following script was attached to the answer here: https://www.mathworks.com/matlabcentral/answers/99150-is-there-an-example-of-using-matlab-to-create-powerpoint-slides?s_tid=ta_ans_results
This shows how to attach both images and text. It has mulitple slides, you can reduce this.
%% Open PowerPoint as a COM Automation server
h = actxserver('PowerPoint.Application')
% Show the PowerPoint window
h.Visible = 1;
% Bring up the PowerPoint session help window - this contains the API, the
% complete function list. At first it's a little intimidating, but after
% acclimating it's not hard to find the methods you're looking for. Also,
% the function signatures are given in VBA syntax, so they takes a little
% deciphering.
h.Help
%% ADD PRESENTATION
% View the methods that can be invoked
h.Presentation.invoke
% Add a presentation via "Add" method
Presentation = h.Presentation.Add
%% ADD SLIDES
% View the methods that can be invoked
Presentation.Slides.invoke
% Add a slide via "Add" method
% IF USING OFFICE 2003, use these commands:
Slide1 = Presentation.Slides.Add(1,'ppLayoutBlank')
Slide2 = Presentation.Slides.Add(2,'ppLayoutBlank')
% IF USING OFFICE 2007, use these commands:
blankSlide = Presentation.SlideMaster.CustomLayouts.Item(1);
Slide1 = Presentation.Slides.AddSlide(1,blankSlide);
Slide2 = Presentation.Slides.AddSlide(1,blankSlide);
%% GENERATE MATLAB IMAGES
figure;
plot(1:10)
print('-dpng','-r150','<full path>\test1.png')
figure;
image(ceil(64*rand(20,20)))
print('-dpng','-r150','<full path>\test2.png')
% Note: it is still necessary to save these files to disk, and then import
% them into PowerPoint from a disk file, because PowerPoint does not
% understand MATLAB data types. However, we can script this all from
% MATLAB.
%% ADD IMAGES TO SLIDES WITH TITLES
% Note: Change the image file full path names to where you save them
Image1 = Slide1.Shapes.AddPicture('<full path>\test1.png','msoFalse','msoTrue',100,20,500,500)
Image2 = Slide2.Shapes.AddPicture('<full path>\test2.png','msoFalse','msoTrue',100,20,500,500)
Title1 = Slide1.Shapes.AddTextbox('msoTextOrientationHorizontal',200,10,400,70)
Title1.TextFrame.TextRange.Text = 'plot(1:10)'
Title2 = Slide2.Shapes.AddTextbox('msoTextOrientationHorizontal',200,10,400,70)
Title2.TextFrame.TextRange.Text = 'image(ceil(64*rand(20,20)))'
%% SAVE PRESENTATION
% Note: Change the presentation full path name to where you save it
Presentation.SaveAs('C:\...\ExamplePresentation.ppt')
%% Close PowerPoint as a COM Automation server
h.Quit;
h.delete;
7 Comments
Kevin Holly
on 19 Feb 2022
I was able to remove the date&time and footer information with a template. See "Add a Slide Layout" section in the link below.
I saved my blank template PowerPoint file as BlankPresentation.pptx
I then ran the following (kudos to Sean for method to insert textbox):
import mlreportgen.ppt.*
ppt = Presentation('myTextBoxAddPresentation.pptx',"BlankPresentation.pptx");
slide = add(ppt,'Blank');
tb = TextBox();
tb.X = '1in';
tb.Y = '1in';
tb.Width = '4 in';
tb.Height = '2in';
add(ppt.Children(1),tb);
para = add(tb,'This is the content');
para.Bold = true;
close(ppt);
rptview(ppt);
Sean de Wolski
on 19 Feb 2022
Edited: Sean de Wolski
on 19 Feb 2022
All you have to do is set X/Y/Width/Height properties of the TextBox presentation element. See this example that does exactly what you want:
0 Comments
See Also
Categories
Find more on MATLAB Report Generator in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!