Create and Format Tables

Create a Table

To create a table, you can:

  • Create an empty Table object using the mlreportgen.ppt.Table constructor without arguments. Then append TableRow objects to the Table object and append TableEntry objects to the TableRow objects.

  • Create an empty Table object using the mlreportgen.ppt.Table constructor, specifying the number of columns.

  • Create a Table object whose rows and columns are populated by the values you specify in the constructor. You can specify a two-dimensional numeric array or a two-dimensional cell array of numbers, character vectors, and Paragraph objects. You can also use a combination of these kinds of values.

For an example of creating a table by appending table rows to an empty table, see mlreportgen.ppt.TableRow. For an example of creating a table by specifying values in the Table object constructor, see mlreportgen.ppt.Table.

Format a Table

You can specify a table style name for the overall look of a table, such as a table that shades alternating rows. You can set the StyleName property of a Table object to the name of a table style.

Table Styles in Templates

The PowerPoint® template must contain an instance of the table style for you to use it in a PPT API program. To list the instances of table styles in your template, use getTableStyleNames.

import mlreportgen.ppt.*

%% Create a new presentation and open it
slides = Presentation('myPrsentation');
open(slides);

%% Print out all table styles and
%% their universally unique identifiers (UUID)
pptStyles = getTableStyleNames(slides);
fprintf('Available table styles:\n');
for i = 1:length(pptStyles)
    fprintf('    Style name: ''%s''\n', pptStyles{i,1});
    fprintf('          UUID: ''%s''\n', pptStyles{i,2});
end

%% Close the presentation
close(slides);

Each style returned has a name and an ID. You can use the name or the ID with the Style property. Use the ID when the name can vary based on locale.

Available table styles:
    Style name: 'Medium Style 2 - Accent 1'
          UUID: '{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}'
    Style name: 'Light Style 1'
          UUID: '{9D7B26C5-4107-4FEC-AEDC-1716B250A1EF}'
    Style name: 'Light Style 1 - Accent 1'
          UUID: '{3B4B98B0-60AC-42C2-AFA5-B58CD77FA1E5}'
    Style name: 'Light Style 1 - Accent 2'
          UUID: '{0E3FDE45-AF77-4B5C-9715-49D594BDF05E}'

If the name of the style you want to use does not have an instance, create one.

  1. Create a slide in your PowerPoint template.

  2. In the slide, create a table.

  3. Apply the styles that you want to use in your program to the table. Applying a style creates an instance of the style in the template.

  4. Delete the slide, and save and close the template.

Format a Table Using a Table Style

This example shows how to format a table using a table style.

import mlreportgen.ppt.*

%% Create a new presentation and add two slides to it
slides = Presentation();
add(slides,'Title and Content');
add(slides,'Title and Content');


%% Save the two content placeholders named 'Content' in an array. 
%% Replace the first content placeholder with a 5x5 table and 
%% apply a table style to it. 
contents = find(slides,'Content');
tbl = replace(contents(1),Table(magic(5)));
tbl.StyleName = 'Medium Style 2 - Accent 1'

%% Replace the second content placeholder with a 10x10 table and 
%% apply a different table style.
%% Generate the presentation and open  it. 
tbl = replace(contents(2),Table(magic(10)));
tbl.StyleName = 'Medium Style 2 - Accent 2'
close(slides);

if ispc
    winopen(slides.OutputPath);
end

This code creates a PowerPoint presentation that has two slides. Each slide contains a table, and each table has a different table style applied to it.

Formatting Options

You can specify the location (upper-left x and y coordinates), height, and width properties of a table. When you add the table to a presentation programmatically, PowerPoint uses those properties, if all of the table content fits in the table. When you replace a TablePlaceholder or ContentPlaceholder with a table, PowerPoint fits the table in the placeholder location and dimensions.

You can specify default formatting for the contents of a table, a column, a table row, and a table entry. Table entry formatting takes precedence over the formatting you specify for a column or for a table row. Table row formatting takes precedence over table formatting.

You can specify these default formatting options for the contents of a Table object.

Table Object Formatting Format ObjectFormat Property

Table style from template

Use the PowerPoint template to specify table style formatting. Create an instance of the style in your template.

n/a

StyleName

Background color

BackgroundColor

BackgroundColor

Column formatting

ColSpec

ColSpecs

Vertical alignment of table cell content

VAlign

VAlign

Font family

FontFamily

Font

Font family for complex scripts to handle locales

FontFamily

ComplexScriptFont

Font size

FontSize

FontSize

Font color

FontColor

FontColor

Upper-left x-coordinate of the table

n/a

X

Upper-left y-coordinate of the table

n/a

Y

Table width

n/a

Width

Table height

n/a

Height

To specify default formatting for the contents of a TableRow object, use the Style property with these format objects.

TableRow Object Formatting Format ObjectFormat Property

Background color

BackgroundColor

n/a

Vertical alignment of table cell content

VAlign

n/a

Font family

FontColor

n/a

Font family for complex scripts

FontFamily

n/a

Font size

FontSize

n/a

Text color

FontColor

n/a

Bold

Bold

n/a

Italic

Italic

n/a

Strike

Strike

n/a

Underline

Underline

n/a

Background color

BackgroundColor

n/a

To specify default formatting for the contents of a TableEntry object, use these formatting options.

TableEntry Object Formatting Format ObjectFormat Property

Background color

BackgroundColor

BackgroundColor

Column width

ColWidth

n/a

Vertical alignment of table cell content

VAlign

VAlign

Font family

FontFamily

Font

Font family for complex scripts to handle locales

FontFamily

ComplexScriptFont

Text color

FontColor

FontColor

Font size

FontSize

FontSize

Bold

Bold

n/a

Italic

Italic

n/a

Strike

Strike

n/a

Underline

Underline

n/a

Access a Table Row or Entry

To access a row in a table, use the mlreportgen.ppt.Table.row method. Specify the Table object and the number of the row you want to access. For example, to access a TableRow object for the second row of myTable, use:

myTable = Table(magic(5));
row2 = row(myTable,2);

To access an entry in a table, use the mlreportgen.ppt.Table.entry method. Specify the Table object and the number of the row and the number of the column that you want to access. For example, to access a TableEntry object for the third entry in the second row of myTable, use:

myTable = Table(magic(5));
entry3row2 = entry(myTable,2,3);

Alternatively, you can access a table row by using the Children property of a Table object. You can access a table entry by using the Children property of a TableRow object. For example, to access the third entry in the second row of myTable:

myTable = Table(magic(5));
entry3row2 = myTable.Children(2).Children(3);

Format a Column

To format a column in a table, use one or more mlreportgen.ppt.ColSpec objects. Create a ColSpec object for each column that you want to format and specify the formatting for each ColSpec object. Then define an array of the ColSpec objects and use that with the ColSpecs property of the Table object.

The format specification for a table row takes precedence over the format specification for a column.

import mlreportgen.ppt.*
slidesFile = 'myColSpecs.pptx'
slides = Presentation(slidesFile);
add(slides,'Title and Content');

t = Table(magic(12));
t.Style = {HAlign('center')};

colSpecs(2) = ColSpec('1.5in');
colSpecs(1) = ColSpec('1.5in');
colSpecs(1).BackgroundColor = 'red';
colSpecs(2).BackgroundColor = 'green';
t.ColSpecs = colSpecs;
t.row(2).Style = {VAlign('bottom')};
t.row(2).BackgroundColor = 'tan';
t.entry(2,3).FontColor = 'red';
t.entry(2,3).FontSize = '30pt';

replace(slides,'Content',t);

close(slides);
if ispc
winopen(slides.OutputPath);
end

When you create a ColSpec object, you can specify the column width in the constructor. For example:

myColSpec = ColSpec('3in');
Also, you can specify the column width using the Width property of a ColSpec object. You specify other formatting properties of the ColSpec object, such as BackgroundColor.

View Table Style Names

If you use the PPT API, to specify a table style other than the default, you need to know the names of table styles in a PowerPoint template. You can view the name in PowerPoint or using the PPT API.

  1. In PowerPoint, select View > Slide Master.

  2. In a slide layout that has a table, click Table (or anywhere in that placeholder). On the Insert tab, click Table.

  3. Create an empty table in the slide layout.

    A panel of Table Styles appears. To see the name of a table style, hover over the table style image.

To see table style names using the PPT API, use the getTableStyleNames method with an mlreportgen.ppt.Presentation object. The output in this example shows just the first two of many table styles in the default template.

import mlreportgen.ppt.*
slides = Presentation('myPlaceholderPresentation');

getTableStyleNames(slides)
ans = 

'Medium Style 2 - Accent 1'     '{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}'
'Light Style 1'                 '{9D7B26C5-4107-4FEC-AEDC-1716B250A1EF}'

To use a table style name with the PPT API, you can use either the name or the numeric identifier.

See Also

Functions

Classes

Related Examples

More About