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