|
| 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