This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

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, first create a Table object. You can add a table by using an add method with a Slide object.

import mlreportgen.ppt.*;
slides = Presentation('myPresentation.pptx');
tableSlide = add(slides,'Blank');

magicTable = Table(magic(5));
magicTable.X = '3in';
magicTable.Y = '5in';

add(tableSlide,magicTable);

close(slides);

To replace content in a table, replace the whole table. To replace a Table object, use the replace method with the Table object, and specify another Table object. To replace a table in a slide content placeholder, use the replace method with the Slide object and specify a Table object.

slides = Presentation('myPresentation');
tableSlide = add(slides,'Title and Table');

table1 = Table(magic(9));
contents = find(tableSlide,'Table');
replace(contents(1),table1);

close(slides);

Add or Replace a Picture

You can add a picture by using an add method or a replace method with a Slide object.

import mlreportgen.ppt.*;
slides = Presentation('myPresentation.pptx');
pictureSlide = add(slides,'Blank');

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

add(pictureSlide,plane);

close(slides);

You can use a replace method with a Picture or PicturePlaceholder object. For example, the default template has a Title and Picture slide layout that has a picture placeholder called Picture.

import mlreportgen.ppt.*;
slides = Presentation('myPresentation');
pictSlide = add(slides,'Title and Picture');

plane2 = Picture(which('b747.jpg'));
contents = find(pictSlide,'Picture');
replace(contents(1),plane2);

close(slides);

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.

Related Examples

More About