Create Text Box in PowerPoint file

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

Answers (2)

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

Hi Kevin,
Great. Thank you very much for your quick reply. I will try out the code.
But is it possible to just use MATLAB Report Generator (instead of COM Automation server)?
I am thinking about the case where I don't have PowerPoint installed. I am guessing that MATLAB Report Generator can still generate PPT file without PowerPoint being installed
Thanks
Hi Kevin,
I have run the script and sucessfully generate the PPT file. This is great. But I have a few questions:
  1. How to get rid of the two text boxes: Click to add title and Click to add subtitle
  2. How to get documentation on these matlab functions, eg. Slide1.Shapes.AddTextbox
  3. In PowerPoint, the positions and height and size of the text box are in inches but I think the matlab function is using different units. How do I convert between the two?
Thanks
Yes, you can do it in Report Generator (You will need the Report Generator Toolbox). Option 2 in the link previously provided.
Also see this guide here:
Hi Kevin,
Let's try this out with MATLAB Report Generator.
I have tried the followings:
ppt = mlreportgen.ppt.Presentation('V:\khung\skunk\trunk\kevin0896.pptx');
titleSlide = add(ppt, 'Title Slide');
close(ppt);
But this creates two text boxes (Click to add title and Click to add subtitle) and a text box showing date and a text box showing Footer and page number at the bottom right.
Can I get rid of all these boxes? I just want a blank slide.
Thanks
titleSlide = add(ppt, 'Blank');
This will get rid of the two text boxes in the middle. But I still get date, Footer and page number at the bottom of the slide.
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);
So I guess it is not possible to create a totally blank PPT file without using a template.

Sign in to comment.

Sean de Wolski
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:

Tags

Asked:

on 18 Feb 2022

Commented:

on 19 Feb 2022

Community Treasure Hunt

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

Start Hunting!