Main Content

Add and Replace Presentation Content

To use the PPT API to add, or replace, content in a PowerPoint® presentation:

  • Set up a PowerPoint template to hold the presentation content you want to add or replace.

  • Create PPT API content objects, such as Paragraph, Table, and Picture objects.

  • Use PPT API content objects to add or replace presentation content.

You can add and replace content in several ways. For example, you can:

  • Add or replace content globally in a presentation or locally in a specific slide.

  • Add content to a text box.

  • Replace a text box, table, or picture with content of the same type.

  • Replace a placeholder with content corresponding to that placeholder.

You cannot replace part of a paragraph, table, or text box. Replace the whole content object.

Set Up the Template

You can replace or add content to an existing PowerPoint presentation without modifying the template. However, using the PPT API requires knowledge of template and slide objects, including:

  • Slide master names

  • Slide layout names

  • Slide placeholder and content object names

  • Table style names

You can use using PowerPoint to add placeholders to a presentation and then use the PPT API to replace the placeholder with content. To replace a specific content object in a presentation, you can use PowerPoint to give a unique name to the content object. Then use that name with the PPT API.

For more information about using PowerPoint templates with a PPT API program, see:

Replace Content

You can replace content by specifying the content object name in a replace method with a Slide object. For example, in the default PPT API template, the Title Slide layout has a content object called Title.

titleSlide = add(ppt,'Title Slide');

replace(titleSlide,'Title','This Is My Title');

To replace presentation content, you can use a find method with a Presentation or Slide object. The find method searches for content objects whose Name property value matches the search value you specify. Then you can use the index of the returned item that you want to update.

ppt = Presentation('myPresentation');
titleSlide = add(ppt,'Title Slide');

contents = find(ppt,'Title');
replace(contents(1),'This Is My Title');

Add and Replace Text

You can use these approaches to add or replace text in a presentation.

Text Specification TechniqueAssociated PPT API Objects

Specify text as part of creating these objects.

  • Text

  • Paragraph

  • ExternalLink

  • InternalLink

Append text to a paragraph.

Append text to these PPT API objects:

  • Paragraph

  • TableEntry

Replace a Paragraph object in a presentation or slide.

Specify a character vector, Paragraph object, or a cell array of character vectors or Paragraph objects or a combination of both kinds of objects, for the replace method with these objects:

  • Presentation

  • Slide

Add to or replace text in a placeholder object.

  • Add to a ContentPlaceholder object a character vector, Paragraph object, or with a cell array of character vectors or Paragraph objects, or a combination of both.

  • Replace a ContentPlaceholder object with a Paragraph object.

  • Add to a TextBoxPlaceholder object a character vector, Paragraph object, or with a cell array of character vectors or Paragraph objects or a combination of both.

  • Replace a TextBoxPlaceholder object with a Paragraph object.

See Add and Replace Text in Placeholders.

Add to, or replace, a text box.

Add to or replace a TextBox object with a character vector, Paragraph object, or with a cell array of character vectors or Paragraph objects, or a combination of both.

See Add or Replace Text in a Text Box.

Add and Replace Text in Placeholders

You can add or replace text in a ContentPlaceholder and a TextBoxPlaceholder, specifying:

  • A character vector

  • A Paragraph object

  • A cell array of character vectors or Paragraph objects or a combination of character vectors and Paragraph objects. An inner cell array specifies inner list (indented) items.

The slide layout specifies whether the text appears as paragraphs, a bulleted list, or a numbered list.

import mlreportgen.ppt.*
 
name1 = 'before';
ppt = Presentation(name1);
open(ppt);

add(ppt,'Comparison');
replace(ppt, 'Left Content', 'dummy content');
replace(ppt, 'Right Content', 'dummy content');
close(ppt);
 
name2 = 'after';
slides = Presentation(name2, name1);
 
lefts = find(ppt, 'Left Content');
rights = find(ppt, 'Right Content');
 
para = replace(lefts(1), 'Left item in the list' );
para.Italic = true;
para.FontColor = 'green';
 
replace(rights(1), { ...
    'Right List item', ...
        { 'Inner right list item', 'Other inner right list item' }...
    'Right List item', ...
    });
 
close(ppt);
rptview(ppt);

Add or Replace Text in a Text Box

A text box in a slide is a box that you can add text to. You can programmatically add or replace the content of a text box in a presentation.

  1. Create a TextBox object. Specify the location and width of the text box.

  2. Add text by using the add method with the TextBox object.

  3. Add the TextBox object to a presentation using the add method with a Presentation object or the add method with a Slide object.

For example:

import mlreportgen.ppt.*
ppt = Presentation('myPresentation.pptx');
open(ppt);
titleSlide = add(ppt,'Title Slide');

tb = TextBox();
tb.X = '2in';
tb.Y = '2in';
tb.Width = '5in';
add(tb,'Text for text box');

add(titleSlide,tb);
close(ppt);

Add or Replace a Table

To add or replace a table in a presentation, use one these approaches:

  • Add a table directly to a slide.

  • Replace a placeholder from a slide layout with a table. For example, add a slide with a Title and Content or Title and Table layout and replace the content or table placeholder with a table.

  • Replace a template table from a template presentation with a different table.

Add Table to Blank Slide

Create an mlreportgen.ppt.Table object and add it to slide.

import mlreportgen.ppt.*
ppt = Presentation('myPresentation.pptx');
open(ppt);
tableSlide = add(ppt,'Blank');
magicTable = Table(magic(5));
magicTable.X = '3in';
magicTable.Y = '5in';
add(tableSlide,magicTable);
close(ppt);

You can replace a table that you have already added to a slide by using the replace method. For example:

import mlreportgen.ppt.*
ppt = Presentation('myPresentation.pptx');
open(ppt);
tableSlide = add(ppt,'Blank');
magicTable = Table(magic(5));
add(tableSlide,magicTable);
newTable = Table(magic(4));
replace(magicTable,newTable);
close(ppt);

Replace Table Placeholder

You can replace a table placeholder that comes from a slide layout. For example, add a slide with a Title and Table layout. A table placeholder is represented by an mlreportgen.ppt.TablePlaceholder object. To replace the table placeholder, use the replace method of the TablePlaceholder object.

import mlreportgen.ppt.*
ppt = Presentation('myPresentation.pptx');
open(ppt);
tableSlide = add(ppt,'Title and Table');
table1 = Table(magic(9));
tblplaceholderObj = find(tableSlide,'Table');
replace(tblplaceholderObj,table1);
close(ppt);

Replace Template Table

If you create a presentation from an existing presentation, a table from the existing presentation (a template table) is represented by an mlreportgen.ppt.TemplateTable object. You can change the position, width, and height of the template table by setting properties of the object. You can also modify the XML markup of the template table. To replace the template table, use the replace method of the TemplateTable object. For example, suppose that you create a presentation from an existing presentation myPresentation that has a slide with the 'Title and Table' layout. The following code replaces the template table with another table.

import mlreportgen.ppt.*
ppt = Presentation('myNewPresentation.pptx','myPresentation.pptx');
open(ppt);
slide1 = ppt.Children(1);
templateTableObj = find(slide1,'Table');
replace(templateTableObj,Table(magic(4)));
close(ppt);

Add or Replace a Picture

To add or replace a picture in a presentation, use one of these approaches:

  • Add a picture directly to a slide.

  • Replace a placeholder that comes from a slide layout with a picture. For example, add a slide with a Title and Content or Title and Picture layout and replace the content or picture placeholder with a picture.

  • Replace a template picture from a template presentation with a different picture.

Include a Picture in a Presentation

Use an mlreportgen.ppt.Picture object to include a picture of an airplane in a presentation.

Create a presentation.

import mlreportgen.ppt.*

ppt = Presentation("myPicturePresentation.pptx");
open(ppt);

Add a slide with a Title and Content layout to the presentation.

add(ppt,"Title and Content");

Create a Picture object using an image of an airplane. Specify the size of the picture.

plane = Picture(which("b747.jpg"));
plane.Width = "5in";
plane.Height = "2in";

Replace the content of the slide with the picture.

replace(ppt,"Content",plane);

Close and view the presentation.

close(ppt);
rptview(ppt);

Here is the generated slide with the image of the airplane:

Replace a Picture

Replace a picture in a presentation.

Create a presentation.

import mlreportgen.ppt.*
ppt = Presentation("myPictureReplacePresentation");
slide1 = add(ppt,"Blank");

Create an mlreportgen.ppt.Picture object.

plane = Picture(which("b747.jpg"));
plane.X = "1in";
plane.Y = "1in";
plane.Width = "5in";
plane.Height = "2in";

Add the picture to the slide.

add(slide1,plane);

Create a second picture.

peppers = Picture(which("peppers.png"));
peppers.X = "1in";
peppers.Y = "1in";
peppers.Width = "3in";
peppers.Height = "3in";

Replace the plane picture with the peppers picture.

replace(plane,peppers);

Close and view the presentation.

close(ppt);
rptview(ppt);

Replace Picture Placeholder with Picture

Add a Title and Picture slide to a presentation and then replace the title and picture placeholders with your own title and picture.

Import the PPT namespace so that you do not have to use long, fully qualified names for the PPT API classes.

import mlreportgen.ppt.*

Create a presentation.

ppt = Presentation("myPicturePlaceholderPresentation.pptx");
open(ppt);

Add a slide that has a Title and Picture layout.

slide = add(ppt,"Title and Picture");

Use the find method of the slide object to find the placeholder object that has the name Title.

titlePlaceholderObj = find(slide,"Title")
titlePlaceholderObj = 
  TextBoxPlaceholder with properties:

                 Bold: []
                 Font: []
    ComplexScriptFont: []
            FontColor: []
             FontSize: []
               Italic: []
               Strike: []
            Subscript: []
          Superscript: []
            Underline: []
      BackgroundColor: []
               VAlign: []
                 Name: 'Title'
                    X: []
                    Y: []
                Width: []
               Height: []
                Style: []
             Children: []
               Parent: [1×1 mlreportgen.ppt.Slide]
                  Tag: 'ppt.TextBoxPlaceholder:30:125'
                   Id: '30:125'

The find method returns an mlreportgen.ppt.TextBoxPlaceholder object.

Replace the placeholder content with the title text.

replace(titlePlaceholderObj,"Airplane");

Use the find method of the slide object to find the placeholder object that has the name Picture.

picturePlaceholderObj = find(slide,"Picture")
picturePlaceholderObj = 
  PicturePlaceholder with properties:

                 Bold: []
                 Font: []
    ComplexScriptFont: []
            FontColor: []
             FontSize: []
               Italic: []
               Strike: []
            Subscript: []
          Superscript: []
            Underline: []
      BackgroundColor: []
               VAlign: []
                 Name: 'Picture'
                    X: []
                    Y: []
                Width: []
               Height: []
                Style: []
             Children: []
               Parent: [1×1 mlreportgen.ppt.Slide]
                  Tag: 'ppt.PicturePlaceholder:31:126'
                   Id: '31:126'

The find method returns an mlreportgen.ppt.PicturePlaceholder object.

Replace the picture placeholder with a picture.

replace(picturePlaceholderObj,Picture("b747.jpg"));

Close and view the presentation.

close(ppt);
rptview(ppt);

PowerPoint® adjusts the picture dimensions to fit in the picture placeholder. If the picture placeholder dimensions are bigger than the Picture object dimensions, the picture stretches proportionally. If the dimensions are smaller, the picture is centered.

Replace Template Picture

If you create a presentation from an existing presentation, a picture from the existing presentation (a template picture) is represented by an mlreportgen.ppt.TemplatePicture object. You can change the position, width, and height of the template picture by setting properties of the object. You can also modify the XML markup of the template picture. To replace the template picture, use the replace method of the TemplatePicture object. For example, suppose that you create a presentation from an existing presentation myPresentation that has a slide with the 'Title and Picture' layout. The following code replaces the template picture with a different picture.

Generate a presentation, MyPicturePresentation, that you then use as the template presentation for another presentation. MyPicturePresentation has one slide with one picture.

import mlreportgen.ppt.*
ppt = Presentation("MyPicturePresentation");
open(ppt);
slide1 = add(ppt,"Title and Picture");
replace(slide1,"Title","Street");
replace(slide1,"Picture",Picture("street1.jpg"));

Close and view the presentation.

close(ppt);

Create a presentation, MyNewPicturePresentation, from MyPicturePresentation. MyPicturePresentation is the template presentation for MyNewPicturePresentation,

ppt = Presentation("MyNewPicturePresentation","MyPicturePresentation");
open(ppt);

Find the template picture by using the find method of the slide object. Because the picture comes from a template presentation slide, find returns the picture as an mlreportgen.ppt.TemplatePicture object.

slide1 = ppt.Children(1);
templatePictureObj = find(slide1,"Picture") 
templatePictureObj = 
  TemplatePicture with properties:

    XMLMarkup: '<p:pic><p:nvPicPr><p:cNvPr id="8" name="Picture"/><p:cNvPicPr><a:picLocks noChangeAspect="1" noGrp="1"/></p:cNvPicPr><p:nvPr><p:ph idx="13" sz="quarter" type="pic"/></p:nvPr></p:nvPicPr><p:blipFill><a:blip r:embed="rId2"><a:extLst><a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}"><a14:useLocalDpi val="0" xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main"/></a:ext></a:extLst></a:blip><a:stretch><a:fillRect/></a:stretch></p:blipFill><p:spPr/></p:pic>'
         Name: 'Picture'
            X: []
            Y: []
        Width: []
       Height: []
        Style: []
     Children: []
       Parent: [1×1 mlreportgen.ppt.Slide]
          Tag: 'ppt.TemplatePicture:70:263'
           Id: '70:263'

Replace the picture with a different picture.

street2 = Picture("street2.jpg");
replace(templatePictureObj,street2);

Close and view the presentation.

close(ppt);
rptview(ppt);

Related Examples

More About