Skip to content

Commit f674967

Browse files
committed
add 2020b version of Excel demo
needs pyargs() for all keyword arguments
1 parent 029c6a8 commit f674967

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

matlab_expo_2022/demo_openpyxl.m

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
% Al Danial, David Garrison
2-
Im = @py.importlib.import_module;
3-
OP = Im("openpyxl");
4-
styles = Im("openpyxl.styles");
2+
OP = imp("openpyxl");
3+
styles = imp("openpyxl.styles");
54
Font = styles.Font;
65
Alignment = styles.Alignment;
76
PatternFill = styles.PatternFill;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
% Al Danial, David Garrison
2+
% This version works with MATLAB 2020b and newer
3+
% Note: 2020b requires Pyton v3.7 or v3.8
4+
OP = imp("openpyxl");
5+
styles = imp("openpyxl.styles");
6+
Font = styles.Font;
7+
Alignment = styles.Alignment;
8+
PatternFill = styles.PatternFill;
9+
book = OP.Workbook();
10+
sheet = book.active;
11+
sheet.title = "Pets by weight";
12+
13+
% font styles, background color
14+
ft_title = Font(...
15+
pyargs("name","Arial", ...
16+
"size",int64(14),"bold",py.True));
17+
ft_red = Font(pyargs("color","00FF0000"));
18+
ft_italics = Font(pyargs("bold",py.True,...
19+
"italic",py.True));
20+
bg_green = PatternFill( pyargs(...
21+
"fgColor","C5FD2F","fill_type","solid"));
22+
23+
sheet.merge_cells("B2:D3");
24+
B2 = sheet.cell(2,2);
25+
B2.value = "My Pets";
26+
B2.font = ft_title;
27+
B2.alignment = Alignment(pyargs(...
28+
"horizontal","center","vertical","center"));
29+
30+
% column headings
31+
category={"Name","Animal","weight [kg]"};
32+
row = int64(4); col = int64(1);
33+
for i = 1:length(category)
34+
nextCell = sheet.cell(row, col+i);
35+
nextCell.value = category{i};
36+
nextCell.fill = bg_green;
37+
end
38+
39+
pets = {{"Nutmeg", "Rabbit", 2.5},...
40+
{"Annabel", "Dog", 4.3}, ...
41+
{"Sunny", "Bird", 0.02}, ...
42+
{"Harley", "Dog", 17.1}, ...
43+
{"Toby", "Dog", 24.0}, ...
44+
{"Mr Socks", "Cat", 3.9}};
45+
46+
for P = pets
47+
row = row + 1;
48+
for j = 1:length(category)
49+
nextCell = sheet.cell(row,col+j);
50+
nextCell.value = P{1}{j};
51+
if j == 3 && P{1}{j} < 0.1
52+
nextCell = sheet.cell(row,col+j);
53+
nextCell.font = ft_red;
54+
end
55+
end
56+
end
57+
58+
% equation to sum all weights
59+
eqn = sprintf("=SUM(D4:D%d)", row);
60+
nextCell = sheet.cell(row+1, 4);
61+
nextCell.value = eqn;
62+
63+
nextCell = sheet.cell(row+1, 2);
64+
nextCell.value = "Total weight:";
65+
nextCell.font = ft_italics;
66+
67+
book.save("pets.xlsx")

0 commit comments

Comments
 (0)