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(slides,'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.

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

contents = find(slides,'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

Append text to a paragraph or external link.

Append text to these PPT API objects:

  • Paragraph

  • TableEntry

  • ExternalLink

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';
slides = Presentation(name1);
add(slides,'Comparison');
replace(slides, 'Left Content', 'dummy content');
replace(slides, 'Right Content', 'dummy content');
close(slides);
 
name2 = 'after';
slides = Presentation(name2, name1);
 
lefts = find(slides, 'Left Content');
rights = find(slides, '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(slides);
 
if ispc
    winopen(slides.OutputPath);
end

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.*
slides = Presentation('myPresentation.pptx');
titleSlide = add(slides,'Title Slide');

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

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

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.

Add Picture to Blank Slide

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

import mlreportgen.ppt.*
ppt = Presentation('myPresentation.pptx');
open(ppt);
pictureSlide = add(ppt,'Blank');
plane = Picture(which('b747.jpg'));
plane.X = '2in';
plane.Y = '2in';
plane.Width = '5in';
plane.Height = '2in';
add(pictureSlide,plane);
close(ppt);

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

import mlreportgen.ppt.*
ppt = Presentation('myPresentation.pptx');
open(ppt);
pictureSlide = add(ppt,'Blank');
plane = Picture(which('b747.jpg'));
plane.X = '2in';
plane.Y = '2in';
add(pictureSlide,plane);
peppers = Picture(which('peppers.png'));
replace(plane,peppers);
close(ppt);

Replace Placeholder

You can replace a placeholder picture with a picture. For example, add a slide with a Title and Picture layout. A picture placeholder is represented by an mlreportgen.ppt.PicturePlaceholder object. To replace the picture placeholder, use the replace method of the PicturePlaceholder object.

import mlreportgen.ppt.*
ppt = Presentation('myPresentation.pptx');
open(ppt);
tableSlide = add(ppt,'Title and Picture');
plane = Picture(which('b747.jpg'));
plane.X = '2in';
plane.Y = '2in';
picplaceholderObj = find(tableSlide,'Picture');
replace(picplaceholderObj,plane);
close(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.

import mlreportgen.ppt.*
ppt = Presentation('myNewPresentation.pptx','myPresentation.pptx');
open(ppt);
slide1 = ppt.Children(1);
templateTableObj = find(slide1,'Picture');
replace(templateTableObj,Picture(which('peppers.png')));
close(ppt); 

Related Examples

More About