How can I programmatically add tables in PowerPoint?

8 visualizzazioni (ultimi 30 giorni)
Saria Ahmad
Saria Ahmad il 26 Ott 2022
Risposto: Deep il 10 Set 2024
I have several tables that I want to insert in PPT programatically. The tables are splits in chunks. and I store the tables in .mat format. Now I want to insert them into slides in a loop. How can I do this? As I didn't find anything in the mlreportgen.ppt document.
import mlreportgen.ppt.*
ppt = Presentation('myfile');
open(ppt);
dt = dir('*.mat');
for i = 1:length(dt)
TableSlide = add(ppt,'Blank');
img = mlreportgen.ppt.Table(sprintf('subTable%d',i))
add(TableSlide,img);
end
close (ppt)

Risposte (1)

Deep
Deep il 10 Set 2024
Hi Saria,
It looks like there was a small misunderstanding regarding the use of the "Table" class. The "Table" class in "mlreportgen.ppt", introduced in MATLAB R2015b, does not accept a string argument in its constructor.
The MATLAB documentation page for "mlreportgen.ppt.Table" outlines the correct approaches for using the "Table" constructor, which you can explore here - https://www.mathworks.com/help/rptgen/ug/mlreportgen.ppt.table-class.html#buxs7lc-3.
Here's a simple example of how you can create a slide with a table containing tabular data:
import mlreportgen.ppt.*
%% Using dummy data. You may load data from your .MAT files here.
data = {'Header1', 'Header2', 'Header3';
10, 20, 30;
40, 50, 60;
70, 80, 90};
%% Creates a PPT slide, adds a blank table
ppt = Presentation('myfile');
open(ppt);
slide = add(ppt, 'Blank');
pptTable = Table();
pptTable.Style = {RowHeight('0.5in'), ColWidth('1in')}; % Customize the table style
%% Loop through tabular data to create table entries
for rowIdx = 1:size(data, 1)
row = TableRow();
for colIdx = 1:size(data, 2)
entry = TableEntry(num2str(data{rowIdx, colIdx}));
append(row, entry);
end
append(pptTable, row);
end
%% Add the slide to the PPT, and use "close" to finish writing.
add(slide, pptTable);
close(ppt);
For more detailed guidance, you might find the following resources helpful:
Hope this helps!

Community Treasure Hunt

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

Start Hunting!

Translated by