-
Notifications
You must be signed in to change notification settings - Fork 20
/
examples_exportToPPTX.m
executable file
·369 lines (295 loc) · 13.8 KB
/
examples_exportToPPTX.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
%
% Example 1: usage of exportToPPTX with blank presentation
%
%% Start new presentation
pptx = exportToPPTX('', ...
'Dimensions',[12 6], ...
'Title','Example Presentation', ...
'Author','MATLAB', ...
'Subject','Automatically generated PPTX file', ...
'Comments','This file has been automatically generated by exportToPPTX');
% Another way of setting some properties
pptx.title = 'Demonstration Presentation';
pptx.author = 'exportToPPTX Example';
pptx.subject = 'Demonstration of various exportToPPTX commands';
pptx.description = 'Description goes in here';
% Additionally background color for all slides can be set as follows:
% exportToPPTX('new','BackgroundColor',[0.5 0.5 0.5]);
%% Add some slides
figH = figure('Renderer','zbuffer'); mesh(peaks); view(0,0);
for islide=1:5,
slideId = pptx.addSlide();
fprintf('Added slide %d\n',slideId);
pptx.addPicture(figH);
pptx.addTextbox(sprintf('Slide Number %d',slideId));
pptx.addNote(sprintf('Notes data: slide number %d',slideId));
% Rotate mesh on each slide
view(18*islide,18*islide);
end
close(figH);
%% Add a hidden slide
slideId = pptx.addSlide('Hidden', true);
fprintf('Added hidden slide %d\n',slideId);
%% Check current presentation
fprintf('Presentation size: %f x %f\n',pptx.dimensions);
fprintf('Number of slides: %d\n',pptx.numSlides);
%% Save presentation and close presentation -- overwrite file if it already exists
% Filename automatically checked for proper extension
newFile = pptx.save('example');
clear pptx
%% Open presentation again
pptx = exportToPPTX('example');
%% Add multiple images in different input formats with custom sizes
slideId = pptx.addSlide();
% Upper left corner picture with a blue frame inserted via figure handle
load earth; figure('Renderer','zbuffer'); image(X); colormap(map); axis off;
pptx.addPicture(gcf,'Position',[1 1 3 2],'EdgeColor',[0 0 0.8],'LineWidth',3);
pptx.addTextbox('Inserted via figure handle','Position',[1 0.5 3 0.5],'Vert','bottom');
close(gcf);
% Upper right corner picture with white background inserted via axes handle
load mandrill; figure('Renderer','zbuffer','Color','w'); image(X); colormap(map); axis off;
pptx.addPicture(gca,'Position',[6 1 3 2]);
pptx.addTextbox('Inserted via axes handle','Position',[6 0.5 3 0.5],'Vert','bottom');
close(gcf);
% Add note to the slide
pptx.addNote('Testing multiple images placement on a single slide.');
pptx.addNote('Repeated addnote calls on the same slide overwrite existing comments.','FontWeight','bold');
% Lower left corner picture inserted via image CDATA (height x width x 3)
rgb = imread('ngc6543a.jpg');
pptx.addPicture(rgb,'Position',[1 3.5 3 2]);
pptx.addTextbox('Inserted via image CDATA','Position',[1 3 3 0.5],'Vert','bottom');
close(gcf);
% Lower right corner picture inserted via image filename
fullPath = which('ngc6543a.jpg');
pptx.addPicture(fullPath,'Position',[6 3.5 4 2]);
pptx.addTextbox('Inserted via filename, no aspect ratio','Position',[6 3 3 0.5],'Vert','bottom');
%% Add image that takes up as much of the slide as possible without losing its aspect ratio
load mandrill; figure('color','w'); image(X); colormap(map); axis off; axis image;
pptx.addSlide();
pptx.addPicture(gcf,'Scale','maxfixed','Position',[5 0 7 6]);
pptx.addTextbox('exportToPPTX','FontSize',50,'Position',[0 0 5 1],'FontWeight','bold');
pptx.addTextbox({'load mandrill;'
'figure(''color'',''w''); image(X);'
'colormap(map); axis off; axis image;'
''
'pptx = exportToPPTX();'
''
'pptx.addSlide();'
'pptx.addPicture(gcf,''Scale'',''maxfixed'',''Position'',[5 0 7 6]);'
'pptx.addTextbox(''exportToPPTX'',''FontSize'',50, ...'
' ''Position'',[0 0 5 1],''FontWeight'',''bold'');'
'pptx.addTextbox(''Scale = maxfixed'',''Position'',[8 5 4 1], ...'
' ''VerticalAlignment'',''bottom'',''HorizontalAlignment'',''right'');'
''
'pptx.save(''screenshot'');'}, ...
'FontSize',10,'FontName','FixedWidth','Position',[0 1.5 6 4.5]);
pptx.addTextbox('Scale = maxfixed','Position',[8 5 4 1], ...
'VerticalAlignment','bottom','HorizontalAlignment','right');
pptx.addSlide();
pptx.addPicture(gcf,'Scale','max');
pptx.addTextbox('Scale = max');
close(gcf);
%% Add image in a vector (non-raster) format
figure, plot(rand(10,10),'-b.'); grid on; xlabel('Random'); ylabel('Random');
saveas(gcf,'vectorFile','png');
pptx.addSlide();
pptx.addPicture('vectorFile.png');
close(gcf);
% Add this image again to make sure supported image types are only added once
pptx.addSlide();
pptx.addPicture('vectorFile.png');
delete('vectorFile.png');
%% Add multiple text boxes with custom sizes and formatting
pptx.addSlide();
pptx.addTextbox('-Red Left-top', ...
'Position',[0 0 4 2], ...
'Color','r');
pptx.addTextbox('Green Center-top', ...
'Position',[4 0 4 2], ...
'HorizontalAlignment','center', ...
'Color','g');
pptx.addTextbox('Right-top and bold italics', ...
'Position',[8 0 4 2], ...
'HorizontalAlignment','right', ...
'FontWeight','bold', ...
'FontAngle','italic');
pptx.addTextbox('Soft Blue Left-middle', ...
'Position',[0 2 4 2], ...
'VerticalAlignment','middle', ...
'Color',[0.58 0.70 0.84]);
pptx.addTextbox(sprintf('Center-middle and bold on light red background\nWith Multiple\nLines'), ...
'Position',[4 2 4 2], ...
'VerticalAlignment','middle', ...
'HorizontalAlignment','center', ...
'FontWeight','bold', ...
'BackgroundColor','c', ...
'LineWidth',2);
pptx.addTextbox('Right-middle', ...
'Position',[8 2 4 2], ...
'VerticalAlignment','middle', ...
'HorizontalAlignment','right', ...
'EdgeColor','y');
pptx.addTextbox('Left-bottom and italics', ...
'Position',[0 4 4 2], ...
'VerticalAlignment','bottom', ...
'HorizontalAlignment','left', ...
'FontAngle','italic');
pptx.addTextbox('Center-bottom size 10', ...
'Position',[4 4 4 2], ...
'VerticalAlignment','bottom', ...
'HorizontalAlignment','center', ...
'FontSize',10, ...
'EdgeColor',[0.5 0.5 0.5], ...
'LineWidth',4);
pptx.addTextbox('Right-bottom size 30', ...
'Position',[8 4 4 2], ...
'VerticalAlignment','bottom', ...
'HorizontalAlignment','right', ...
'FontSize',30);
%% Add one more slide
pptx.addSlide();
pptx.addTextbox('Textbox rotated by 45 degrees', ...
'Position',[3 2.5 6 1], ...
'VerticalAlignment','middle', ...
'HorizontalAlignment','center', ...
'FontSize',30, ...
'Rotation',45);
%% Add slide with a differently colored background
pptx.addSlide('BackgroundColor',[0 0 1]);
pptx.addTextbox('This slide has a blue background', ...
'Position',[3 2.5 6 1], ...
'VerticalAlignment','middle', ...
'HorizontalAlignment','center', ...
'FontSize',30);
%% Add slide with different XML entities
pptx.addSlide('BackgroundColor','g');
pptx.addTextbox(sprintf('Standard XML entities such as \n & and < and > and '' and " \n are properly escaped'), ...
'VerticalAlignment','middle', ...
'HorizontalAlignment','center', ...
'FontSize',15);
pptx.addNote(sprintf('Standard XML entities such as \n & and < and > and '' and " \n are properly escaped'));
%% Add slide with markdown
pptx.addSlide();
pptx.addTextbox(sprintf('Numbered items:\n# Each numbered line must start with a "#" symbol\n# Use \\n to start new line\n# Last item is a very, very, very, very, very long one, so that when the text wraps to the new line a proper hanging line can be observed\n#No space between # and text so it does not become a list item.'));
pptx.addTextbox( ...
{'Bulleted items:', ...
'- Each bulleted line must start with a "-" symbol', ...
'- Use cell arrays to add multiple lines of text', ...
'- Last item is a very, very, very, very, very long one, so that when the text wraps to the new line a proper hanging line can be observed.', ...
'','', ...
'-Last item does not have a space between the hyphen and the text so it does not become a list item.'}, ...
'Position',[0 3 12 3]);
pptx.addSlide();
pptx.addTextbox( ...
{'The following in-text markdown formatting options are supported:', ...
'- Words can be **bolded** by surrounding them with "**" symbols', ...
'- Words can be *italicized* using "*"', ...
'- Words can also be _underlined_ by using "_" symbol', ...
'- **Various** types of formatting *can be* combined _together_', ...
'- Variable NAMES_WITH_UNDERSCORES are not underlined', ...
'- _Markdown_ formatting *can span multiple* words', ...
'- Markdown symbol * or ** or _ that is followed by a non-alphanumeric character is not used to format * or ** or _', ...
sprintf('\t- It can even be tabbed (if using printable \\t sequence)'), ...
sprintf('\t\t- Further subtabbing is allowed as well'), ...
'- Markdown with non-alphanumeric characters: 1**, 2**, 3**, 4**', ...
'- Example of markdown without escaping *literal asterisks* and with escaping \*literal asterisks\*', ...
'- Example of markdown without escaping **literal double-asterisks** and with escaping \**literal double-asterisks\**', ...
'- Example of markdown without escaping _literal underscores_ and with escaping \_literal underscores\_', ...
'- Escape character \ can be itself escaped \\'});
%% Add slide with markdown formatting, but markdown formatting disabled
pptx.addSlide();
pptx.addTextbox( ...
{'This shows the ability to use markdown characters without the actual markdown formatting by using ''markdown'',false input property-value pair', ...
'The following in-text markdown formatting options are supported:', ...
'- Words can be **bolded** by surrounding them with "**" symbols', ...
'- Words can be *italicized* using "*"', ...
'- Words can also be _underlined_ by using "_" symbol', ...
'- **Various** types of formatting *can be* combined _together_', ...
'- Variable NAMES_WITH_UNDERSCORES are not underlined', ...
'- _Markdown_ formatting *can span multiple* words', ...
'- Markdown symbol * or ** or _ that is followed by a non-alphanumeric character is not used to format * or ** or _', ...
sprintf('\t- It can even be tabbed (if using printable \\t sequence)'), ...
'- Markdown with non-alphanumeric characters: 1**, 2**, 3**, 4**', ...
'- Example of markdown without escaping *literal asterisks* and with escaping \*literal asterisks\*', ...
'- Example of markdown without escaping **literal double-asterisks** and with escaping \**literal double-asterisks\**', ...
'- Example of markdown without escaping _literal underscores_ and with escaping \_literal underscores\_', ...
'- Escape character \ can be itself escaped \\'}, ...
'markdown',false);
%% Add slide out of order
pptx.addSlide('Position',1);
pptx.addTextbox('This slide was added last, but inserted into the first position. Additionally it has FontName property set to FixedWidth, which causes it use fixed width font.','FontName','FixedWidth');
pptx.addNote( ...
{'FontName property can also be used in the notes field.', ...
'It is useful for adding code type output.', ...
'', ...
'**To view text formatting changes that you make in the Notes pane, do the following**', ...
'# On the View tab, in the Presentation Views group, click Outline View.', ...
'# Right-click the Outline pane, and then select Show Text Formatting in the popped up menu.'}, ...
'FontName','FixedWidth','FontSize',10);
%% Add slide with linked textbox
% OnClick property take in the slide ID, which is returned during addslide command
pptx.addSlide();
pptx.addTextbox('Click here to jump to slide with different image insertion methods', ...
'OnClick',slideId, ...
'HorizontalAlignment','center', ...
'Position',[3 1.5 6 1], ...
'FontSize',20);
pptx.addTextbox('Click here to jump to a URL', ...
'OnClick','https://github.com/stefslon/exportToPPTX', ...
'HorizontalAlignment','center', ...
'Position',[3 3.5 6 1], ...
'FontSize',20);
%% Add lines
pptx.addSlide();
xData = linspace(0,12,101);
yData1 = 3+sin(xData*2)*0.3;
yData2 = 3+cos(xData*2)*0.6;
pptx.addShape(xData,yData1,'LineWidth',2,'LineStyle',':');
pptx.addShape(xData,yData2,'LineWidth',4,'LineColor','b');
theta = linspace(0,2*pi,101);
xData = sin(theta);
yData = cos(theta);
pptx.addShape(xData+4,yData+1,'LineWidth',2,'LineColor','r','LineStyle','--','BackgroundColor','g','ClosedShape',true);
pptx.addShape(xData+8,yData+1,'LineWidth',0,'LineStyle','--','BackgroundColor','c');
%% Add a table
slideId = pptx.addSlide();
customCell = { ...
'Click here to jump to slide with different image insertion methods', ...
'OnClick',slideId, ...
'FontSize',18, ...
'BackgroundColor','k', ...
'Vert','bottom', ...
'Horiz','right'};
tableData = { ...
'Header 1','Header 2','Header 3'; ...
'Row 1','Data A','Data B'; ...
'Row 2',10,20; ...
'Row 3','Data C',customCell; ...
'Row 4',NaN,'N/A'; };
pptx.addTable(tableData,'Position',[1 0.5 4 4], ...
'Vert','middle','Horiz','center','FontSize',13, ...
'ColumnWidth',[0.3 0.3 0.4],'BackgroundColor','c', ...
'EdgeColor','b','LineWidth',2);
pptx.addTable(tableData,'Position',[6 0.5 4 4], ...
'Vert','middle','Horiz','center','FontSize',12, ...
'ColumnWidth',[0.3 0.3 0.4], ...
'LineWidth',0);
%% Add movie to the slide
fullVideoPath = which('xylophone.mpg');
if ~isempty(fullVideoPath) && exist(fullVideoPath,'file'),
pptx.addSlide();
pptx.addPicture(fullVideoPath);
end
%% Save again
pptx.save();
%% Clear presentation
clear pptx
%% Open presentation again for modification
pptx = exportToPPTX('example');
%% Add slide out of order
pptx.switchSlide(16);
pptx.addTextbox('Added to the slide after using ''switchslide'' command.','Position',[4 5.5 4 0.5],'FontSize',11,'FontName','Times New Roman','Horiz','center');
pptx.addNote('Added to the slide after using ''switchslide'' command.');
%% Save
pptx.save();
fprintf('New file has been saved: <a href="matlab:open(''%s'')">%s</a>\n',newFile,newFile);