ExcelMaster¶
Excel report engine. ExcelMaster wraps xlsxwriter and adds a cursor: every write
advances the current row, so you stack tables, images, and charts on a sheet without computing cell coordinates. It also
provides charts and conditional formatting. User guide: Excel Report Generation.
The package has no top-level exports. Import the class from its module:
Cell Formats: ExcelFormatTool¶
ExcelFormatTool
¶
ExcelFormat
¶
Initialize excel format.
Opens the output workbook and registers the library of preset cell formats (dict_cell_format) that the writer
classes ExcelWorkbook and ExcelMaster refer to by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path of the |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
engine |
ExcelWriter
|
Writer (xlsxwriter engine) through which DataFrames are written. |
workbook |
Workbook
|
The underlying xlsxwriter workbook ( |
basename |
str
|
File name part of |
base_filepath |
str
|
Directory part of |
dict_cell_format |
dict
|
Maps format names to xlsxwriter |
Notes
Register your own formats with add_new_format; presets cannot be overridden.
Source code in ExcelMaster/ExcelFormatTool.py
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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 | |
add_new_format
¶
Add a new user-defined format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
format_dict
|
dict
|
xlsxwriter format properties, for example |
required |
format_name
|
str
|
Name under which the format is registered in |
required |
Returns:
| Type | Description |
|---|---|
int
|
0 when the format was added; 1 when |
Source code in ExcelMaster/ExcelFormatTool.py
Writer: ExcelMaster¶
ExcelMaster
¶
ExcelWorkbook
¶
Bases: ExcelFormat
Write anything to Excel (Workbook-level Operator).
Adds workbook-level helpers (conditional formats, color scales, data bars, cell range conversion and clean-up of
temporary images) on top of ExcelFormat, which creates the workbook and the preset cell formats and provides the
attributes engine, workbook, basename, base_filepath and dict_cell_format. The worksheet-level
writing API is in the subclass ExcelMaster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path of the output |
required |
verbose
|
bool
|
If True, the writing methods log the cell range they wrote through |
True
|
Attributes:
| Name | Type | Description |
|---|---|---|
verbose |
bool
|
The |
ws_dict |
dict
|
Worksheets by name, |
Source code in ExcelMaster/ExcelMaster.py
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 | |
to_cell_range_text
¶
To Excel Cell Range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
first_row
|
int
|
Zero-based index of the first row. |
required |
first_col
|
int
|
Zero-based index of the first column. |
required |
last_row
|
int
|
Zero-based index of the last row. |
required |
last_col
|
int
|
Zero-based index of the last column. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The range in A1 notation, for example |
Source code in ExcelMaster/ExcelMaster.py
cell_range_to_loc
¶
Convert text cell range expression to a list of values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cell_range_text
|
str
|
Range in A1 notation ( |
required |
Returns:
| Type | Description |
|---|---|
list of int
|
Zero-based |
Source code in ExcelMaster/ExcelMaster.py
colletter_to_textloc
¶
Append Row Index to the given Column Letter-Formatted Index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row_index
|
int
|
Excel row number (one-based, as shown in Excel) appended to the column letter(s). |
required |
col_letter
|
str
|
A column letter such as |
required |
Returns:
| Type | Description |
|---|---|
str
|
A one-row range in A1 notation: |
Source code in ExcelMaster/ExcelMaster.py
set_color_scale
¶
Set Color Scale.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
worksheet
|
Worksheet
|
Worksheet that receives the conditional format. |
required |
cell_range
|
str or list or tuple
|
Cell range in A1 notation ( |
required |
colors
|
tuple of str
|
Hex colors: three give a 3-color scale (minimum, midpoint, maximum), two give a 2-color scale (minimum, maximum). |
("#F8696B", "#FFEB84", "#63BE7B")
|
Returns:
| Type | Description |
|---|---|
int
|
Always 0. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in ExcelMaster/ExcelMaster.py
set_data_bar
¶
Set Data Bar in a Worksheet for a range of cells.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
worksheet
|
Worksheet
|
Worksheet that receives the conditional format. |
required |
cell_range
|
str or list or tuple
|
Cell range in A1 notation ( |
required |
bar_color
|
str
|
Hex color of the bars (Excel 2010 style data bars). |
"#63C384"
|
Returns:
| Type | Description |
|---|---|
int
|
Always 0. |
Source code in ExcelMaster/ExcelMaster.py
set_cell_format
¶
Set format for a range of cell.
The format is applied as an Excel conditional format (type no_errors, or cell when cell_condition is
given), not as a static cell format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
worksheet
|
Worksheet
|
Worksheet that receives the format. |
required |
cell_range
|
str or list or tuple
|
Cell range in A1 notation ( |
required |
cformat
|
str or Format
|
Name of a format in |
required |
cell_condition
|
tuple or None
|
|
None
|
Returns:
| Type | Description |
|---|---|
int
|
Always 0. |
Source code in ExcelMaster/ExcelMaster.py
set_cell_format_rbyr
¶
set_cell_format_rbyr(worksheet, start_row, condition_list, condition, ifelse_col, cformat='YELLOW_BG')
Conditionally Highlight Cells Row by Row.
Tests every element of condition_list with val_input_condition and highlights, in the row that belongs to the
element, the cells of the column(s) given for the true case or for the false case.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
worksheet
|
Worksheet
|
Worksheet that receives the formats. |
required |
start_row
|
int
|
Zero-based row of the first element of |
required |
condition_list
|
list
|
Values to test, one per row. |
required |
condition
|
tuple or str
|
Condition as understood by |
required |
ifelse_col
|
tuple of str or None
|
|
required |
cformat
|
str or Format
|
Format of the highlighted cells (see |
"YELLOW_BG"
|
Returns:
| Type | Description |
|---|---|
int
|
Always 0. |
Notes
An operator that val_input_condition does not know yields None, which counts as the false case.
Source code in ExcelMaster/ExcelMaster.py
remove_tmp_img
¶
Remove Temp Images.
Deletes the temporary box plot images (.tmp_image_<digits>.png) that write_boxplot leaves in the current working
directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_pattern
|
str
|
Regular expression searched ( |
r".tmp_image_[0-9]+.png"
|
Returns:
| Type | Description |
|---|---|
int or None
|
0 when at least one matching file was found and removed, None when nothing matched. |
Notes
Files are removed by bare file name, so only matches located directly in the current working directory can be deleted;
a match inside a sub-directory makes os.remove raise FileNotFoundError. close_workbook calls this method
after saving the workbook.
Source code in ExcelMaster/ExcelMaster.py
close_workbook
¶
Finish writing the contents of the workbook and close the file.
Fits the columns of the worksheets added with auto_fit=True, saves the workbook to filepath (until then the
file is empty) and then calls remove_tmp_img to delete the temporary box plot images from the working directory.
Source code in ExcelMaster/ExcelMaster.py
ExcelMaster
¶
Bases: ExcelWorkbook
Write anything to Excel (Worksheet-level Operator)
Keeps a zero-based cursor (curr_row, curr_col). Every writing method starts at the cursor, or at loc when
given, and then moves the cursor below (skipby='row') or to the right of (skipby='col') the block it wrote,
leaving gap_number blank cells. Only that one coordinate is updated: with skipby='row' the column of the cursor
keeps its value, even when loc pointed to another column. The class builds on ExcelWorkbook (conditional
formats) and ExcelFormat (workbook creation and the named cell formats) and adds tables, merged titles, text,
images and charts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path of the output |
required |
verbose
|
bool
|
Required (it has no default). If True, the writing methods log the cell range they wrote through
|
required |
gap_number
|
int
|
Spacing between consecutive blocks. The attribute |
2
|
init_loc
|
tuple of int
|
Initial cursor |
(0, 0)
|
Attributes:
| Name | Type | Description |
|---|---|---|
curr_row |
int
|
Row of the cursor (zero-based). |
curr_col |
int
|
Column of the cursor (zero-based). |
gap_number |
int
|
Blank rows (or columns) left after a block: exact for tables and merged cells, approximate for charts and images, whose size is measured in cells of the default size. |
default_row_height |
int or float
|
Height in pixels of a row (20 at the default scale). It is used to convert chart and image sizes into cells and is
overwritten by |
default_col_width |
int or float
|
Width in pixels of a column (64 at the default scale), updated like |
max_nrows |
int
|
Number of rows of a worksheet (1048576). |
max_ncols |
int
|
Number of columns of a worksheet (16384). |
Source code in ExcelMaster/ExcelMaster.py
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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 | |
add_worksheet
¶
add_worksheet(name, hide_grid=True, reset_loc=True, cell_scale=True, auto_fit=False, zoom_perc=100, tab_color=None)
Add a worksheet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the worksheet (unique in the workbook, at most 31 characters). |
required |
hide_grid
|
bool
|
If True, hide the gridlines on screen and in print. |
True
|
reset_loc
|
bool
|
If True, reset the cursor to (0, 0). |
True
|
cell_scale
|
bool or tuple of float
|
|
True
|
auto_fit
|
bool
|
If True, widen the columns of the sheet to fit their content. The widths are fitted by |
False
|
zoom_perc
|
int
|
Zoom of the worksheet in percent (xlsxwriter accepts 10 to 400). |
100
|
tab_color
|
str or None
|
Color of the sheet tab, a hex code such as |
None
|
Returns:
| Type | Description |
|---|---|
Worksheet
|
The new worksheet, also registered in |
Notes
cell_scale True or a tuple overwrites default_row_height and default_col_width, which later charts and
images use to convert their size into cells.
Source code in ExcelMaster/ExcelMaster.py
reset_curr_loc
¶
Reset Current Location.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loc
|
tuple of int
|
New cursor position |
(0, 0)
|
Returns:
| Type | Description |
|---|---|
int
|
Always 0. |
Source code in ExcelMaster/ExcelMaster.py
set_cell_size
¶
Set Cell Size in Scale.
Sets the pixel height of every row (all 1,048,576) and the pixel width of every column of the worksheet, and stores the
sizes in default_row_height and default_col_width, which later chart and image sizes are converted with.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
worksheet
|
Worksheet
|
Worksheet to resize. |
required |
size_scale
|
tuple of float
|
|
(1, 1)
|
Returns:
| Type | Description |
|---|---|
int
|
Always 0. |
Notes
The row height is set as the default row height of the sheet. Setting the 1,048,576 rows one by one made xlsxwriter keep a record for each of them: about 0.5 GB of memory and 8 seconds per worksheet, and 15 MB per sheet in the file, so a report of twenty sheets could exhaust the memory of the machine.
Source code in ExcelMaster/ExcelMaster.py
get_curr_loc
¶
Get Current Location in worksheet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
toCell
|
bool
|
If True, return the cursor as an A1-style cell reference instead of a tuple. |
False
|
Returns:
| Type | Description |
|---|---|
tuple of int or str
|
|
Source code in ExcelMaster/ExcelMaster.py
set_border_line
¶
Set border line for a range of cells.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
worksheet
|
Worksheet
|
Worksheet that receives the border. |
required |
valuerange
|
str or list or tuple
|
Cell range in A1 notation ( |
required |
border_line
|
int
|
xlsxwriter border style index, used for the top, bottom, left and right border of every cell of the range (1 thin, 2 medium, 5 thick, ...). |
1
|
Returns:
| Type | Description |
|---|---|
int
|
Always 0. |
Notes
The border is applied as a conditional format (through set_cell_format).
Source code in ExcelMaster/ExcelMaster.py
merge_col
¶
merge_col(worksheet, loc=None, nrows=1, ncols=1, text='', skipby='row', cformat='BLUE_H4', retCellRange=None)
Merge columns in a single row.
Merges the block of nrows x ncols cells that starts at loc (one row by default) and writes text in it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
worksheet
|
Worksheet
|
Worksheet to write on. |
required |
loc
|
tuple of int or None
|
|
None
|
nrows
|
int
|
Number of rows to merge. |
1
|
ncols
|
int
|
Number of columns to merge. |
1
|
text
|
str
|
Text written in the merged cell. |
""
|
skipby
|
str or None
|
Cursor move after the write: |
'row'
|
cformat
|
str
|
Name of a format in |
'BLUE_H4'
|
retCellRange
|
str or None
|
|
None
|
Returns:
| Type | Description |
|---|---|
int or str or list of int
|
0, or the written range as selected by |
Notes
A block of one row and one column is written as a plain cell, because xlsxwriter cannot merge a single cell.
Source code in ExcelMaster/ExcelMaster.py
write_dataframe
¶
write_dataframe(worksheet, df, loc=None, title=None, index=False, header=True, skipby='row', titleformat='BLUE_H4', headerformat='TABLE_HEADER', valueformat='----', retCellRange=None)
Write a dataframe to excel file.
Writes the optional title row (merged across the table), the header and the values with DataFrame.to_excel, then
formats the header and the value cells.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
worksheet
|
Worksheet
|
Worksheet to write on. The table goes through the pandas writer, which finds the sheet by its name. |
required |
df
|
DataFrame
|
Table to write. |
required |
loc
|
tuple of int or None
|
|
None
|
title
|
str or None
|
Title written in a merged row above the table (format |
None
|
index
|
bool
|
If True, write the index as the leading column(s), one per index level. |
False
|
header
|
bool
|
If True, write the column names, one header row per column level. |
True
|
skipby
|
str or None
|
Cursor move after the write: |
'row'
|
titleformat
|
str
|
Name of a format in |
"BLUE_H4"
|
headerformat
|
str or Format
|
Format of the header cells: a name in |
"TABLE_HEADER"
|
valueformat
|
str or Format
|
Format of every value cell, as a name in |
"----"
|
retCellRange
|
str or None
|
|
None
|
Returns:
| Type | Description |
|---|---|
int or str or list of int
|
0, or the written range as selected by |
Notes
The header and value formats are applied as conditional formats (see set_cell_format).
Source code in ExcelMaster/ExcelMaster.py
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 | |
insert_image
¶
Insert an image to the sheet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
worksheet
|
Worksheet
|
Worksheet to write on. |
required |
figPath
|
str
|
Path of the image file (for example PNG or JPEG). |
required |
figScale
|
tuple of float
|
|
(1, 1)
|
loc
|
tuple of int or None
|
|
None
|
skipby
|
str or None
|
Cursor move after the write: |
'row'
|
retCellRange
|
str or None
|
|
None
|
Returns:
| Type | Description |
|---|---|
int or str or list of int
|
0, or the covered range as selected by |
Notes
The size of the image in cells is computed from the scaled pixel size and default_row_height /
default_col_width (rows rounded down, columns rounded up), so the returned range and the cursor move are
approximate.
Source code in ExcelMaster/ExcelMaster.py
write_chart
¶
write_chart(worksheet, df, y_list, x=None, title='', chart_size=(30, 13), chart_type='line', y_axis_range=(None, None), y_num_format=None, y2_axis=False, loc=None, retChart=False, skipby='row', outputData=False, retCellRange=None, xy_axes_name=('', ''), major_gridlines=False, legend='bottom', line_marker='circle', line_type='solid', chart_style=None, append_to_chart=None)
Write line chart to Excel worksheet.
Builds an xlsxwriter chart from df and inserts it at loc (or at the cursor). Despite the summary, chart_type
also gives column, stacked column and pie charts. The source data of the chart are written to a hidden worksheet named
__CHRT_DATA_<N>, or to worksheet itself with outputData=True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
worksheet
|
Worksheet
|
Worksheet that receives the chart. |
required |
df
|
DataFrame
|
Source data, one row per category. |
required |
y_list
|
list of str
|
Columns of |
required |
x
|
str or list of str or None
|
Column(s) of |
None
|
title
|
str
|
Title of the chart. |
""
|
chart_size
|
tuple of int
|
|
(30, 13)
|
chart_type
|
str or dict
|
|
"line"
|
y_axis_range
|
tuple
|
|
(None, None)
|
y_num_format
|
str or None
|
Number format of the value axis, for example |
None
|
y2_axis
|
bool
|
If True, plot the series on the secondary y axis; the axis settings (range, format, title, gridlines) then apply to that axis. |
False
|
loc
|
tuple of int or None
|
|
None
|
retChart
|
bool
|
If True, return the chart object instead of inserting it: nothing is inserted and the cursor does not move. Use it
with |
False
|
skipby
|
str or None
|
Cursor move after the insertion: |
"row"
|
outputData
|
bool
|
If True, write the table of chart data to |
False
|
retCellRange
|
str or None
|
|
None
|
xy_axes_name
|
tuple of str
|
|
("", "")
|
major_gridlines
|
bool
|
If True, show the major gridlines of the value axis. |
False
|
legend
|
str
|
xlsxwriter legend position: |
"bottom"
|
line_marker
|
str
|
Marker type of every series (xlsxwriter |
"circle"
|
line_type
|
str
|
Dash type of the line of every series (xlsxwriter |
"solid"
|
chart_style
|
int or None
|
xlsxwriter built-in chart style number ( |
None
|
append_to_chart
|
Chart or None
|
Existing chart (obtained with |
None
|
Returns:
| Type | Description |
|---|---|
xlsxwriter.chart.Chart or int or str or list of int
|
The chart when |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Notes
Every call adds a hidden worksheet __CHRT_DATA_<N> to the workbook (it is registered in ws_dict).
Source code in ExcelMaster/ExcelMaster.py
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 | |
write_combined_chart
¶
write_combined_chart(worksheet, chart1, chart2, loc=None, chart_size=(30, 13), skipby='row', retCellRange=None)
Combined two chart objects and then write to Excel worksheet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
worksheet
|
Worksheet
|
Worksheet that receives the combined chart. |
required |
chart1
|
Chart
|
Primary chart, obtained with |
required |
chart2
|
Chart or None
|
Secondary chart, obtained with |
required |
loc
|
tuple of int or None
|
|
None
|
chart_size
|
tuple of int
|
|
(30, 13)
|
skipby
|
str or None
|
Cursor move after the insertion: |
"row"
|
retCellRange
|
str or None
|
|
None
|
Returns:
| Type | Description |
|---|---|
int or str or list of int
|
0, or the covered range as selected by |
Source code in ExcelMaster/ExcelMaster.py
write_duo_chart
¶
write_duo_chart(worksheet, df, y1_list, y2_list=None, x=None, c1_type='column', c2_type='line', y1_axis_range=(0, 1), y2_axis_range=None, y1_num_format=None, y2_num_format=None, y1_line_marker='circle', y2_line_marker='circle', y1_line_type='solid', y2_line_type='solid', loc=None, title='', chart_size=(30, 13), xy_axes_name=('', ''), major_gridlines=False, retChart=False, retCellRange=None, skipby='row')
Write duo-chart sharing the same x axis.
Builds two charts from df and x with write_chart (the first from y1_list on the primary y axis, the
second from y2_list on the secondary y axis), combines them and inserts the result. Typical use: columns for counts
on the left axis and a line for a rate on the right axis. Without y2_list only the first chart is built and
inserted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
worksheet
|
Worksheet
|
Worksheet that receives the chart. |
required |
df
|
DataFrame
|
Source data, one row per category. |
required |
y1_list
|
list of str
|
Columns drawn on the primary y axis, in the chart type |
required |
y2_list
|
list of str
|
Columns drawn on the secondary y axis, in the chart type |
None
|
x
|
str or list of str or None
|
Column(s) that give the category labels, as in |
None
|
c1_type
|
str or dict
|
Type of the first chart: |
"column"
|
c2_type
|
str or dict
|
Type of the second chart, as |
"line"
|
y1_axis_range
|
tuple or None
|
|
(0, 1)
|
y2_axis_range
|
tuple or None
|
|
None
|
y1_num_format
|
str or None
|
Number format of the primary y axis, for example |
None
|
y2_num_format
|
str or None
|
Number format of the secondary y axis. |
None
|
y1_line_marker
|
str
|
Marker type of the series of the first chart. |
"circle"
|
y2_line_marker
|
str
|
Marker type of the series of the second chart. |
"circle"
|
y1_line_type
|
str
|
Dash type of the lines of the first chart. |
"solid"
|
y2_line_type
|
str
|
Dash type of the lines of the second chart. |
"solid"
|
loc
|
tuple of int or None
|
|
None
|
title
|
str
|
Title of the chart (the combined chart shows the title of the first one). |
""
|
chart_size
|
tuple of int
|
|
(30, 13)
|
xy_axes_name
|
tuple of str
|
|
("", "")
|
major_gridlines
|
bool
|
If True, show the major gridlines of the primary y axis; the secondary axis never shows gridlines. |
False
|
retChart
|
bool
|
If True, return the tuple |
False
|
retCellRange
|
str or None
|
|
None
|
skipby
|
str or None
|
Cursor move after the insertion: |
"row"
|
Returns:
| Type | Description |
|---|---|
tuple of xlsxwriter.chart.Chart or int or str or list of int
|
The tuple |
Notes
One hidden worksheet __CHRT_DATA_<N> per chart is added to the workbook.
Source code in ExcelMaster/ExcelMaster.py
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 | |
write_text_by_dict
¶
Write text using Python dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
worksheet
|
Worksheet
|
Worksheet to write on. |
required |
dict_cells
|
dict
|
Maps a cell (
|
required |
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. The cursor is not used or changed. |
Source code in ExcelMaster/ExcelMaster.py
write_text_content
¶
Write text content line by line to the worksheet.
Every line becomes one row, starting at loc (or the cursor), in a single column. Inline markup in a line:
{FORMAT_NAME}styles the text that follows it with a format ofdict_cell_format({B},{I},{HEADER_2},{BLUE_H2}, ...). Several signs in one line give a rich string with mixed formats (text before the first sign is allowed then). A line with exactly one sign must start with it, otherwiseTypeError." [>] "(with the blanks) splits a line into cells that are written side by side in the same row.- a trailing
[[FORMAT_NAME]]formats the whole cell (as a conditional format).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
worksheet
|
Worksheet
|
Worksheet to write on. |
required |
input_text
|
str or None
|
Text to write, lines separated by |
None
|
txt_path
|
str or None
|
Path of a text file whose lines are written. Give exactly one of |
None
|
loc
|
tuple of int or None
|
|
None
|
retCellRange
|
str or None
|
|
None
|
Returns:
| Type | Description |
|---|---|
int or str or list of int
|
0, or the range as selected by |
Raises:
| Type | Description |
|---|---|
ValueError
|
If both or neither of |
Notes
The cursor is set to the last row written (curr_row = start_row + number_of_lines - 1) and curr_col is not
changed, even when loc is given. End input_text with "\n" so that the next block starts below the text;
otherwise it overwrites the last line. The file given by txt_path is opened and not closed explicitly.
Source code in ExcelMaster/ExcelMaster.py
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 | |
plot_boxplot
staticmethod
¶
plot_boxplot(df, x, y, y_percentage=False, colored_box=True, color_grp=(10, 1), title='', fontsize=14, figsize=(8, 6), show_fig=True, img_path=None, transp_bg=False)
Plot Box Plot Chart using matplotlib.
Draws one box per distinct value of x (in ascending order) from the values of y. Both axis labels are
reformatted with string_proc ("credit_score" becomes "Credit Score"). The figure is closed before the
function returns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Source data. |
required |
x
|
str
|
Column that defines the groups, one box per distinct value. |
required |
y
|
str
|
Numeric column whose distribution is drawn in each box. |
required |
y_percentage
|
bool
|
If True, multiply the values of |
False
|
colored_box
|
bool
|
If True, fill the boxes with the colors of |
True
|
color_grp
|
tuple or str or list of str
|
Box colors, as accepted by |
(10, 1)
|
title
|
str
|
Title of the plot. |
""
|
fontsize
|
int
|
Font size of the axis labels and ticks (the title is |
14
|
figsize
|
tuple of float
|
Figure size |
(8, 6)
|
show_fig
|
bool
|
If True, display the figure with |
True
|
img_path
|
str or None
|
If given, save the figure to this path (200 dpi). |
None
|
transp_bg
|
bool
|
If True, save the image with a transparent background. |
False
|
Returns:
| Type | Description |
|---|---|
tuple
|
|
Notes
y_percentage=True multiplies the values of y by 100 and labels the axis as percent, so it is meant for rates
between 0 and 1. With y_percentage=False the values are drawn as they are.
Source code in ExcelMaster/ExcelMaster.py
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 | |
write_boxplot
¶
write_boxplot(ws, df, x, y, y_percentage=False, colored_box=True, color_grp=(10, 1), title='', fontsize=14, figsize=(30, 13), show_fig=False, img_path=None, transp_bg=False, loc=None, skipby='row', retCellRange=None)
Write boxplot to Worksheet.
Draws the box plot with plot_boxplot into a temporary PNG file (./.tmp_image_<date time><random number>.png in
the working directory), resizes the file to figsize worksheet cells and inserts it with insert_image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ws
|
Worksheet
|
Worksheet to write on (note the name: |
required |
df
|
DataFrame
|
Source data. |
required |
x
|
str
|
Column that defines the groups, one box per distinct value. |
required |
y
|
str
|
Numeric column whose distribution is drawn in each box. |
required |
y_percentage
|
bool
|
If True, multiply the values by 100 and format the y axis as percent (see |
False
|
colored_box
|
bool
|
If True, fill the boxes with the colors of |
True
|
color_grp
|
tuple or str or list of str
|
Box colors, as accepted by |
(10, 1)
|
title
|
str
|
Title of the plot. |
""
|
fontsize
|
int
|
Font size of the axis labels and ticks. |
14
|
figsize
|
tuple of int
|
Size of the inserted image as |
(30, 13)
|
show_fig
|
bool
|
If True, display the figure with |
False
|
img_path
|
str or None
|
Not used: the image is always saved to the temporary file. |
None
|
transp_bg
|
bool
|
If True, save the image with a transparent background. |
False
|
loc
|
tuple of int or None
|
|
None
|
skipby
|
str or None
|
Cursor move after the insertion, as in |
"row"
|
retCellRange
|
str or None
|
|
None
|
Returns:
| Type | Description |
|---|---|
int or str or list of int
|
The result of |
Notes
The temporary image stays in the working directory until close_workbook (or remove_tmp_img) deletes it.
Source code in ExcelMaster/ExcelMaster.py
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 | |
getStartDateofLatestWeek
¶
Date of first day of last week.
Returns the Sunday that immediately precedes the Monday starting the current week (weeks start on Monday and are
counted with %W). Every day from Monday to Sunday of one week gives the same result, for example 2026-09-27 for
2026-09-28 through 2026-10-04.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
retStr
|
bool
|
If True, return the date as a |
True
|
Returns:
| Type | Description |
|---|---|
str or datetime
|
The date described above. |
Source code in ExcelMaster/Utility.py
getLastCompletedVintage
¶
Last Completed Vintage
Returns the last day of the month that precedes the month of start_date (or of today), that is the most recent
completed month.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_date
|
str or None
|
Reference date as text in |
None
|
format
|
str
|
|
"%Y-%m-%d"
|
vintage
|
bool
|
If True, return the month as a |
False
|
Returns:
| Type | Description |
|---|---|
str or int
|
The last day of the previous month formatted with |
Source code in ExcelMaster/Utility.py
vin2quar
¶
String Vintage to Quarter (if completed month).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strDate
|
str
|
Vintage as |
required |
Returns:
| Type | Description |
|---|---|
str
|
|
Source code in ExcelMaster/Utility.py
list_files
¶
List all files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
location
|
str
|
Directory that is searched recursively ( |
required |
pattern
|
str
|
Regular expression searched ( |
required |
Returns:
| Type | Description |
|---|---|
list of str
|
Bare file names (without their directory) of the matching files in walk order; a name that exists in several directories appears once per directory. |
Source code in ExcelMaster/Utility.py
getCurrentDateTime
¶
Get Current DateTime
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fmt
|
str
|
|
"%Y%m%d%H%M%S"
|
Returns:
| Type | Description |
|---|---|
str
|
Current local date and time formatted with |
Source code in ExcelMaster/Utility.py
input_table_proc
¶
Process Input Table.
Lower-cases the column names of a table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tbl
|
DataFrame
|
Table with string column names. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
The same object with lower-cased column names; the caller's DataFrame is renamed in place. |
Source code in ExcelMaster/Utility.py
get_file_extension
¶
Get File Extentsion for a given file Path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
str
|
File path or name. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The extension including the leading dot, in its original case ( |
Source code in ExcelMaster/Utility.py
input_validation
¶
Input Validation.
Loads a table from a file path, or accepts a DataFrame, and lower-cases its column names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
str or DataFrame
|
A DataFrame, or the path of a file: a |
required |
sep
|
str
|
Field delimiter of the text file; ignored for |
","
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The table with lower-cased column names. A DataFrame passed in is returned as the same object, so its columns are renamed in place. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If |
Source code in ExcelMaster/Utility.py
val_input_condition
¶
Condition Tuple Validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
float or str
|
Value to test. |
required |
condition
|
tuple or str
|
A string tests equality: |
(">", 20)
|
Returns:
| Type | Description |
|---|---|
bool or None
|
The result of the test. None (falsy) for an operator that is not in the list above, for example |
Source code in ExcelMaster/Utility.py
get_quarter
¶
Get the quarter number of a vintage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strDate
|
str
|
Vintage as |
required |
Returns:
| Type | Description |
|---|---|
int
|
Quarter of the month, 1 to 4 (for example |
Source code in ExcelMaster/Utility.py
tanspose_dataframe
¶
Transpose Pandas DataFrame.
The spelling of the function name is historical.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Table to transpose. |
required |
index_col
|
str or list of str
|
Column(s) set as the index before the transposition; their values become the column labels of the result. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
|
Source code in ExcelMaster/Utility.py
convert_perc_str_to_float
¶
Percentage to Float.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Table to convert. |
required |
cols
|
list of str
|
Columns to convert. A column is converted only when its dtype is |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
|
Source code in ExcelMaster/Utility.py
color_hex2rgb
¶
Convert Color Hex Code to RGB Tuple.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hex_code
|
str
|
Color as six hexadecimal digits with an optional leading |
required |
Returns:
| Type | Description |
|---|---|
tuple of int
|
|
Source code in ExcelMaster/Utility.py
get_color_set
¶
Return a set of color code without replacement.
Picks colors from the XKCD color table of matplotlib (949 named colors).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of colors. |
required |
start_num
|
int
|
Index in the XKCD table of the first color. |
0
|
step
|
int
|
Distance between two consecutive picked indices ( |
1
|
retName
|
bool
|
If True, return a dict |
False
|
lookupName
|
str or None
|
Regular expression that replaces the index-based picks: the first |
None
|
Returns:
| Type | Description |
|---|---|
list of str or dict
|
Hex color codes ( |
Source code in ExcelMaster/Utility.py
string_proc
¶
Process a given String.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
str
|
Text whose words are separated by underscores, for example a column name. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The words capitalized (first letter upper-case, the rest lower-case) and joined by single spaces, for example
|
Source code in ExcelMaster/Utility.py
convert_to_boxplot_data
¶
Convert dataframe to boxplot data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Source table. |
required |
x
|
str
|
Column that defines the groups (one box per distinct value). |
required |
y
|
str
|
Column with the values of each group. |
required |
y_percentage
|
bool
|
If True, multiply every value by 100. |
False
|
Returns:
| Type | Description |
|---|---|
dict
|
|
Source code in ExcelMaster/Utility.py
color_input_validation
¶
Perform Color Input Validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
color_grp
|
tuple or str or list of str
|
A 2-tuple |
required |
val_n
|
int
|
Number of colors needed (number of boxes). |
required |
Returns:
| Type | Description |
|---|---|
list of str
|
Color codes, one per box. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in ExcelMaster/Utility.py
get_metric_shift
¶
Calculate Metric Shift for Variable Reduction.
For every row, the slope between consecutive rows: (metric - metric of the previous row) / (nvars - nvars of the
previous row), that is the change of the metric per variable added (or removed, when the rows are ordered by
decreasing number of variables).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Table with one row per model, ordered by the number of variables. |
required |
metric_name
|
str
|
Column holding the metric. |
required |
nvars_col
|
str
|
Column holding the number of variables. |
"nvars"
|
Returns:
| Type | Description |
|---|---|
Series
|
The shift of every row, with the index of |
Source code in ExcelMaster/Utility.py
get_metrics_shift
¶
Get Shift for List of Metrics.
Adds a column <metric>_shift for every metric, computed with get_metric_shift and its default
nvars_col="nvars", so data needs a column named nvars.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Table with one row per model, ordered by the number of variables. |
required |
metric_cols
|
list of str
|
Metric columns. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
|
Source code in ExcelMaster/Utility.py
compute_overfitting_shift
¶
Calculate Overfitting Performance Shift.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Table with the metric columns of both samples, named |
required |
sample_prefix
|
tuple of str
|
|
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the two prefixes select a different number of columns. |
Source code in ExcelMaster/Utility.py
proc_psi_raw_report
¶
Processing Raw PSI Report generated from Takecopter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
psi_raw_table
|
str or DataFrame
|
Raw PSI table: a DataFrame or the path of a csv / sas7bdat file, read with |
required |
psi_title
|
str
|
Title placed on the top level of the two-level column header of the result. |
required |
keep_list
|
list of str or None
|
Columns to keep, spelled as in the processed table (upper-case when |
None
|
varname
|
str
|
New name of the |
"variable"
|
upper
|
bool
|
If True, upper-case the names of the remaining columns. |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Table indexed by |
Source code in ExcelMaster/Utility.py
get_mean_risk
¶
get average risk
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bivar_single_attr
|
DataFrame
|
Bivariate table of one attribute with one row per bin. |
required |
value_range_col
|
list of str
|
Columns with the bounds of each bin. A row whose bounds are all missing is the missing-value bin and is left out of
|
['min_indep', 'max_indep']
|
dep_col
|
str
|
Column with the bad rate of each bin. |
"dep"
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
|
Source code in ExcelMaster/Utility.py
Report Templates: Template¶
Template
¶
getStartDateofLatestWeek
¶
Date of first day of last week.
Returns the Sunday that immediately precedes the Monday starting the current week (weeks start on Monday and are
counted with %W). Every day from Monday to Sunday of one week gives the same result, for example 2026-09-27 for
2026-09-28 through 2026-10-04.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
retStr
|
bool
|
If True, return the date as a |
True
|
Returns:
| Type | Description |
|---|---|
str or datetime
|
The date described above. |
Source code in ExcelMaster/Utility.py
getLastCompletedVintage
¶
Last Completed Vintage
Returns the last day of the month that precedes the month of start_date (or of today), that is the most recent
completed month.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_date
|
str or None
|
Reference date as text in |
None
|
format
|
str
|
|
"%Y-%m-%d"
|
vintage
|
bool
|
If True, return the month as a |
False
|
Returns:
| Type | Description |
|---|---|
str or int
|
The last day of the previous month formatted with |
Source code in ExcelMaster/Utility.py
vin2quar
¶
String Vintage to Quarter (if completed month).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strDate
|
str
|
Vintage as |
required |
Returns:
| Type | Description |
|---|---|
str
|
|
Source code in ExcelMaster/Utility.py
list_files
¶
List all files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
location
|
str
|
Directory that is searched recursively ( |
required |
pattern
|
str
|
Regular expression searched ( |
required |
Returns:
| Type | Description |
|---|---|
list of str
|
Bare file names (without their directory) of the matching files in walk order; a name that exists in several directories appears once per directory. |
Source code in ExcelMaster/Utility.py
getCurrentDateTime
¶
Get Current DateTime
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fmt
|
str
|
|
"%Y%m%d%H%M%S"
|
Returns:
| Type | Description |
|---|---|
str
|
Current local date and time formatted with |
Source code in ExcelMaster/Utility.py
input_table_proc
¶
Process Input Table.
Lower-cases the column names of a table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tbl
|
DataFrame
|
Table with string column names. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
The same object with lower-cased column names; the caller's DataFrame is renamed in place. |
Source code in ExcelMaster/Utility.py
get_file_extension
¶
Get File Extentsion for a given file Path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
str
|
File path or name. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The extension including the leading dot, in its original case ( |
Source code in ExcelMaster/Utility.py
input_validation
¶
Input Validation.
Loads a table from a file path, or accepts a DataFrame, and lower-cases its column names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
str or DataFrame
|
A DataFrame, or the path of a file: a |
required |
sep
|
str
|
Field delimiter of the text file; ignored for |
","
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The table with lower-cased column names. A DataFrame passed in is returned as the same object, so its columns are renamed in place. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If |
Source code in ExcelMaster/Utility.py
val_input_condition
¶
Condition Tuple Validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
float or str
|
Value to test. |
required |
condition
|
tuple or str
|
A string tests equality: |
(">", 20)
|
Returns:
| Type | Description |
|---|---|
bool or None
|
The result of the test. None (falsy) for an operator that is not in the list above, for example |
Source code in ExcelMaster/Utility.py
get_quarter
¶
Get the quarter number of a vintage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strDate
|
str
|
Vintage as |
required |
Returns:
| Type | Description |
|---|---|
int
|
Quarter of the month, 1 to 4 (for example |
Source code in ExcelMaster/Utility.py
tanspose_dataframe
¶
Transpose Pandas DataFrame.
The spelling of the function name is historical.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Table to transpose. |
required |
index_col
|
str or list of str
|
Column(s) set as the index before the transposition; their values become the column labels of the result. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
|
Source code in ExcelMaster/Utility.py
convert_perc_str_to_float
¶
Percentage to Float.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Table to convert. |
required |
cols
|
list of str
|
Columns to convert. A column is converted only when its dtype is |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
|
Source code in ExcelMaster/Utility.py
color_hex2rgb
¶
Convert Color Hex Code to RGB Tuple.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hex_code
|
str
|
Color as six hexadecimal digits with an optional leading |
required |
Returns:
| Type | Description |
|---|---|
tuple of int
|
|
Source code in ExcelMaster/Utility.py
get_color_set
¶
Return a set of color code without replacement.
Picks colors from the XKCD color table of matplotlib (949 named colors).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of colors. |
required |
start_num
|
int
|
Index in the XKCD table of the first color. |
0
|
step
|
int
|
Distance between two consecutive picked indices ( |
1
|
retName
|
bool
|
If True, return a dict |
False
|
lookupName
|
str or None
|
Regular expression that replaces the index-based picks: the first |
None
|
Returns:
| Type | Description |
|---|---|
list of str or dict
|
Hex color codes ( |
Source code in ExcelMaster/Utility.py
string_proc
¶
Process a given String.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
str
|
Text whose words are separated by underscores, for example a column name. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The words capitalized (first letter upper-case, the rest lower-case) and joined by single spaces, for example
|
Source code in ExcelMaster/Utility.py
convert_to_boxplot_data
¶
Convert dataframe to boxplot data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Source table. |
required |
x
|
str
|
Column that defines the groups (one box per distinct value). |
required |
y
|
str
|
Column with the values of each group. |
required |
y_percentage
|
bool
|
If True, multiply every value by 100. |
False
|
Returns:
| Type | Description |
|---|---|
dict
|
|
Source code in ExcelMaster/Utility.py
color_input_validation
¶
Perform Color Input Validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
color_grp
|
tuple or str or list of str
|
A 2-tuple |
required |
val_n
|
int
|
Number of colors needed (number of boxes). |
required |
Returns:
| Type | Description |
|---|---|
list of str
|
Color codes, one per box. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in ExcelMaster/Utility.py
get_metric_shift
¶
Calculate Metric Shift for Variable Reduction.
For every row, the slope between consecutive rows: (metric - metric of the previous row) / (nvars - nvars of the
previous row), that is the change of the metric per variable added (or removed, when the rows are ordered by
decreasing number of variables).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Table with one row per model, ordered by the number of variables. |
required |
metric_name
|
str
|
Column holding the metric. |
required |
nvars_col
|
str
|
Column holding the number of variables. |
"nvars"
|
Returns:
| Type | Description |
|---|---|
Series
|
The shift of every row, with the index of |
Source code in ExcelMaster/Utility.py
get_metrics_shift
¶
Get Shift for List of Metrics.
Adds a column <metric>_shift for every metric, computed with get_metric_shift and its default
nvars_col="nvars", so data needs a column named nvars.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Table with one row per model, ordered by the number of variables. |
required |
metric_cols
|
list of str
|
Metric columns. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
|
Source code in ExcelMaster/Utility.py
compute_overfitting_shift
¶
Calculate Overfitting Performance Shift.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Table with the metric columns of both samples, named |
required |
sample_prefix
|
tuple of str
|
|
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the two prefixes select a different number of columns. |
Source code in ExcelMaster/Utility.py
proc_psi_raw_report
¶
Processing Raw PSI Report generated from Takecopter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
psi_raw_table
|
str or DataFrame
|
Raw PSI table: a DataFrame or the path of a csv / sas7bdat file, read with |
required |
psi_title
|
str
|
Title placed on the top level of the two-level column header of the result. |
required |
keep_list
|
list of str or None
|
Columns to keep, spelled as in the processed table (upper-case when |
None
|
varname
|
str
|
New name of the |
"variable"
|
upper
|
bool
|
If True, upper-case the names of the remaining columns. |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Table indexed by |
Source code in ExcelMaster/Utility.py
get_mean_risk
¶
get average risk
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bivar_single_attr
|
DataFrame
|
Bivariate table of one attribute with one row per bin. |
required |
value_range_col
|
list of str
|
Columns with the bounds of each bin. A row whose bounds are all missing is the missing-value bin and is left out of
|
['min_indep', 'max_indep']
|
dep_col
|
str
|
Column with the bad rate of each bin. |
"dep"
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
|
Source code in ExcelMaster/Utility.py
get_pva_report
¶
get_pva_report(em, ws, gains_result, sample_list, nbins=10, varcol='variable', chart_scale=(10, 10))
Plot PVA Table by Segment across Sample.
Writes a "PVA Charts" heading and then, for every segment, a heading with the segment name, one line chart per sample
side by side (predicted mean_score against actual interval_bad_rate by rank) and, to the right of the charts,
the pivoted table (rank by sample and predicted / actual).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
em
|
ExcelMaster
|
Writer. The report starts at its current cursor. |
required |
ws
|
Worksheet
|
Worksheet to write on. |
required |
gains_result
|
str or DataFrame
|
Gains table: a DataFrame or the path of a csv / sas7bdat file, read with |
required |
sample_list
|
list of str
|
Values of the |
required |
nbins
|
int
|
Number of bins (ranks) of the gains table. It only sizes the charts: |
10
|
varcol
|
str
|
Name of the column that holds the melted series names. It must stay |
"variable"
|
chart_scale
|
tuple of int
|
Extra |
(10, 10)
|
Returns:
| Type | Description |
|---|---|
Worksheet
|
|
Notes
Sets em.gap_number to 2. The charts use a fixed y axis from 0 to 1 (formatted as 0.00%) and the table values
are formatted as percentages with 3 decimals. After each segment the cursor is moved below the charts, in the column
of the first chart.
Source code in ExcelMaster/Template.py
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 | |
get_bivar_report
¶
get_bivar_report(em, ws, attr_info, bivar, sample_list, varcol='varname', sample_col='sample', x_cols=['min_indep', 'max_indep'], n_col='_freq_', dep_col='dep', chart_size=(20, 10), average_risk_line=False)
Get Bivar Report by Attribute across Samples.
Writes a "Bivar Plot" heading and then, for every attribute of attr_info (in the row order of that table), the
attribute's information table and, side by side for every sample, a combined chart: the number of observations per bin
as columns (primary axis) and the bad rate as a line (secondary axis, 0.00%), with the bin ranges [min, max]
on the x axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
em
|
ExcelMaster
|
Writer. The report starts at its current cursor. |
required |
ws
|
Worksheet
|
Worksheet to write on. |
required |
attr_info
|
str or DataFrame
|
Attribute information: a DataFrame or the path of a csv / sas7bdat file (column names are lower-cased by
|
required |
bivar
|
str or DataFrame
|
Bivariate table with one row per bin and sample (path or DataFrame, column names lower-cased), with the columns
|
required |
sample_list
|
list of str
|
Values of |
required |
varcol
|
str
|
Name of the attribute column in |
"varname"
|
sample_col
|
str
|
Name of the sample column in |
"sample"
|
x_cols
|
list of str
|
The two columns with the lower and the upper bound of each bin; they build the x axis labels |
["min_indep", "max_indep"]
|
n_col
|
str
|
Column with the number of observations of each bin (drawn as columns). |
"_freq_"
|
dep_col
|
str
|
Column with the bad rate of each bin (drawn as a line). |
"dep"
|
chart_size
|
tuple of int
|
|
(20, 10)
|
average_risk_line
|
bool
|
If True, two dashed lines are added to each chart: the mean bad rate over all bins ( |
False
|
Returns:
| Type | Description |
|---|---|
Worksheet
|
|
Notes
attr_infois filtered with the upper-cased attribute name andbivarwith the lower-cased one, so the attribute names must be upper case inattr_infoand lower case inbivar. Lower-case names inattr_inforaiseIndexError; upper-case names inbivarsilently give empty charts.- The mean lines come from
get_mean_riskwith its default column names, whateverx_colsanddep_colare:bivarmust always contain the columnsmin_indep,max_indepanddep(KeyErrorotherwise), even whenaverage_risk_lineis False. - Sets
em.gap_numberto 2. After each attribute the cursor is moved below the charts, in the column of the first chart.
Source code in ExcelMaster/Template.py
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 | |
add_scr_info
¶
Add Score Info Comparison
Writes, from left to right starting at the cursor, a block of identifying information followed by one block of score statistics per sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
em
|
ExcelMaster
|
Writer. The blocks start at its current cursor. |
required |
ws
|
Worksheet
|
Worksheet to write on. |
required |
data
|
DataFrame
|
One row per score or segment, with the columns named in |
required |
info
|
list of str
|
Identifying columns, at least three: the first three become the (multi-level) index of the first block, the others are its columns. |
required |
scr_info
|
list of str
|
Statistic names without the sample prefix (for example |
required |
sample_prefices
|
list of str
|
Column prefixes of the samples (for example |
required |
wTitle
|
bool
|
If True, write a title row over every block (a blank title over the information block). If False, no title row is written, which suits blocks that are stacked under blocks that already have titles. |
True
|
Returns:
| Type | Description |
|---|---|
tuple of int
|
|
Notes
Sets em.gap_number to 0, so the blocks touch each other. The NUM%.2 format is applied from the third column of
each sample block on; the first two columns are left unformatted.
Source code in ExcelMaster/Template.py
add_perf_metrics
¶
Add Perf Metrics Comparison
Writes, from left to right starting at the cursor, a block of identifying information followed by one block of performance metrics per sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
em
|
ExcelMaster
|
Writer. The blocks start at its current cursor. |
required |
ws
|
Worksheet
|
Worksheet to write on. |
required |
data
|
DataFrame
|
One row per score or segment, with the columns named in |
required |
info
|
list of str
|
Identifying columns, at least three: the first three become the (multi-level) index of the first block, the others are its columns. |
required |
perf_metrics
|
list of str
|
Metric names without the sample prefix (for example |
required |
sample_prefices
|
list of str
|
Column prefixes of the samples (for example |
required |
wTitle
|
bool
|
If True, write a title row over every block (a blank title over the information block). If False, no title row is written, which suits blocks that are stacked under blocks that already have titles. |
True
|
Returns:
| Type | Description |
|---|---|
tuple of int
|
|
Notes
Sets em.gap_number to 0, so the blocks touch each other. The NUM%.2 format is applied to every metric column of
a sample block except the last one.
Source code in ExcelMaster/Template.py
add_perf_lift
¶
Add Performance Lift.
Writes, from left to right starting at the cursor, a block of identifying information followed by one block per sample
with the relative lift of model 2 over model 1: m2 / m1 - 1 for every performance metric, as percentages with
data bars.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
em
|
ExcelMaster
|
Writer. The blocks start at its current cursor. |
required |
ws
|
Worksheet
|
Worksheet to write on. |
required |
m2_data
|
DataFrame
|
Performance table of model 2 (the numerator), with the columns named in |
required |
m1_data
|
DataFrame
|
Performance table of model 1, the baseline (the denominator), with the same layout. Its rows are matched with those
of |
required |
info
|
list of str
|
Identifying columns, at least three: the first three become the (multi-level) index of the first block. The value
of |
required |
perf_metrics
|
list of str
|
Metric names without the sample prefix. |
required |
sample_prefices
|
list of str
|
Column prefixes of the samples (for example |
required |
wTitle
|
bool
|
If True, write a title row over every block (a blank title over the information block); if False, write none. |
True
|
Returns:
| Type | Description |
|---|---|
list of int
|
|
Notes
Sets em.gap_number to 0, so the blocks touch each other. After writing, the cursor is placed in the column of the
information block, on the row directly below the blocks.
Source code in ExcelMaster/Template.py
get_seg_perf_comparison_report
¶
get_seg_perf_comparison_report(em, ws, m1_data, bad, sample_prefices, title='Performance Comparison', m2_data=None, perf_metrics=['ks', 'top10_cap', 'top20_cap', 'top40_cap', 'auc'], nbins=100)
Get Segment Perf Eval Report.
Writes a title, a note on the gains bins and then three groups of tables: score statistics (add_scr_info),
performance metrics (add_perf_metrics) and, when m2_data is given, the performance lift of model 2 over model 1
(add_perf_lift). The rows of model 2 are written directly under those of model 1 in the same columns, without
titles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
em
|
ExcelMaster
|
Writer. The report starts at its current cursor. |
required |
ws
|
Worksheet
|
Worksheet to write on. |
required |
m1_data
|
str or DataFrame
|
Per-segment table of model 1: a DataFrame or the path of a csv / sas7bdat file (column names are lower-cased by
|
required |
bad
|
str
|
Name of the bad class as it appears in the column names |
required |
sample_prefices
|
list of str
|
Column prefixes of the samples, in lower case like the lower-cased column names (for example
|
required |
title
|
str
|
Title of the report, written as a merged heading ( |
"Performance Comparison"
|
m2_data
|
str or DataFrame or None
|
Table of a second model to compare, with the same layout as |
None
|
perf_metrics
|
list of str
|
Metric names without the sample prefix. |
["ks", "top10_cap", "top20_cap", "top40_cap", "auc"]
|
nbins
|
int
|
Only used in the note |
100
|
Returns:
| Type | Description |
|---|---|
Worksheet
|
|
Notes
Leaves em.gap_number at 0 and the cursor under the last block. A DataFrame passed as m1_data or m2_data
is modified in place (lower-cased column names, converted percentage text in the metric columns).
Source code in ExcelMaster/Template.py
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 | |
get_means_chart_report
¶
get_means_chart_report(em, ws, means_rpt, by_class, varlist, class_name=None, stats_list=['N', 'NMISS', 'MIN', 'MEAN', 'MAX'], inc_miss_rate=True, varcol='variable', attr_info=None)
Plot Means Chart and Get Report.
Writes a "Means Chart by Attribute" heading and then, for every variable of varlist, an optional attribute
information table followed by one block per statistic of stats_list: a heading and a chart of that statistic by
class, side by side. With inc_miss_rate each chart combines columns for the statistic with a line for the missing
rate on a secondary axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
em
|
ExcelMaster
|
Writer. The report starts at its current cursor. |
required |
ws
|
Worksheet
|
Worksheet to write on. |
required |
means_rpt
|
str or DataFrame
|
Descriptive statistics, one row per variable and class: a DataFrame or the path of a csv / sas7bdat file (column
names are lower-cased by |
required |
by_class
|
str
|
Column that defines the classes on the x axis (for example a month); the rows are sorted by it in ascending order. |
required |
varlist
|
list of str
|
Variables to report (values of |
required |
class_name
|
str or None
|
Title of the x axis. None uses |
None
|
stats_list
|
list of str
|
Statistics to chart, one chart each. The column is found by the lower-cased name; headings and y axis titles show the name as given. |
['N', 'NMISS', 'MIN', 'MEAN', 'MAX']
|
inc_miss_rate
|
bool
|
If True, add the missing rate as a line on a secondary axis ( |
True
|
varcol
|
str
|
Name of the variable column in |
"variable"
|
attr_info
|
str or DataFrame or None
|
Attribute information (path or DataFrame, column names lower-cased). When given, the rows whose |
None
|
Returns:
| Type | Description |
|---|---|
Worksheet
|
|
Notes
Sets em.gap_number to 2 (0 while the attribute information is written) and resets the cursor to (0, 0) at the
end, so pass loc or call em.reset_curr_loc before the next write if the cursor position matters. A DataFrame
passed as means_rpt gets its column names lower-cased in place.
Source code in ExcelMaster/Template.py
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 | |
get_grid_boxplot_report
¶
get_grid_boxplot_report(em, ws, perf_res, hparam_list, metric_list, fontsize=12, figsize=(30, 13), transp_bg=True, color_grp=(20, 1), colored_box=True)
Plot Boxplots of Grid Search Result for Hyperparams.
Writes a "Boxplot For Grid Search Result" heading and then, for every metric, a heading and one box plot image per hyperparameter, side by side: the distribution of the metric (shown as percent) over the values of the hyperparameter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
em
|
ExcelMaster
|
Writer. The report starts at its current cursor. |
required |
ws
|
Worksheet
|
Worksheet to write on. |
required |
perf_res
|
DataFrame
|
Grid search results, one row per run, with the columns named in |
required |
hparam_list
|
list of str
|
Hyperparameter columns for the x axis, one plot each (one box per distinct value). |
required |
metric_list
|
list of str
|
Metric columns to plot, one heading and one row of plots each. |
required |
fontsize
|
int
|
Font size of the plot labels. |
12
|
figsize
|
tuple of int
|
Size of each inserted image as |
(30, 13)
|
transp_bg
|
bool
|
Save the plot images with a transparent background. |
True
|
color_grp
|
tuple or str or list of str
|
Colors of the boxes, as accepted by |
(20, 1)
|
colored_box
|
bool
|
Not used: the call always passes |
True
|
Returns:
| Type | Description |
|---|---|
Worksheet
|
|
Notes
Sets em.gap_number to 2. The plot images are temporary files (.tmp_image_*.png) in the working directory; they
are deleted by em.close_workbook().
Source code in ExcelMaster/Template.py
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 | |
get_var_reduct_report
¶
get_var_reduct_report(em, ws, vr_perf, metric_cols, target_metrics=None, basic_info_text=None, nvars_col='nvars')
Generate Variable Reduction Excel Report.
Writes a heading, an optional text, three tables side by side (model information, performance by metric and performance shift) and then one chart per target metric, two charts per row: the metric on the primary axis and its shift on the secondary axis, against the number of variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
em
|
ExcelMaster
|
Writer. The report starts at its current cursor. |
required |
ws
|
Worksheet
|
Worksheet to write on. |
required |
vr_perf
|
str or DataFrame
|
Variable reduction results, one row per model: a DataFrame or the path of a csv / sas7bdat file (column names are
lower-cased by |
required |
metric_cols
|
list of str
|
Metric columns, in lower case, written in the performance table. |
required |
target_metrics
|
list of str or None
|
Metrics for which the shift |
None
|
basic_info_text
|
str or None
|
Text written under the heading with |
None
|
nvars_col
|
str
|
Column with the number of variables, used as the x axis of the charts. The shift is always computed from the column
named |
"nvars"
|
Returns:
| Type | Description |
|---|---|
Worksheet
|
|
Notes
A DataFrame passed as vr_perf is modified in place: its columns are lower-cased, percentage text is converted and
the <metric>_shift columns are added. Sets em.gap_number to 2.
Source code in ExcelMaster/Template.py
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 | |
get_grid_search_report
¶
get_grid_search_report(em, ws, rs_perf, metric_cols, sample_prefices, basic_info_text=None, sortby='hd_auc')
Get Grid Search Report.
Writes a heading, an optional text and three tables side by side: model information, performance by metric and the overfitting shift of the later samples relative to the first one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
em
|
ExcelMaster
|
Writer. The report starts at its current cursor. |
required |
ws
|
Worksheet
|
Worksheet to write on. |
required |
rs_perf
|
str or DataFrame
|
Grid (or random) search results, one row per model: a DataFrame or the path of a csv / sas7bdat file (column names
are lower-cased by |
required |
metric_cols
|
list of str
|
Metric columns, in lower case, written in the performance table. |
required |
sample_prefices
|
list of str
|
Column prefixes of the samples (for example |
required |
basic_info_text
|
str or None
|
Text written under the heading with |
None
|
sortby
|
str or None
|
Column by which the rows are sorted in descending order. None or an empty string keeps the input order. |
"hd_auc"
|
Returns:
| Type | Description |
|---|---|
Worksheet
|
|
Notes
Sets em.gap_number to 0. The metric and shift tables are formatted as percentages with 2 decimals and the shift
table gets data bars. A DataFrame passed as rs_perf is modified in place: its column names are lower-cased and the
percentage text of metric_cols is converted (the *_shift columns are added to it only when sortby is
None, because sorting works on a copy).
Source code in ExcelMaster/Template.py
Utilities: Utility¶
Utility
¶
getStartDateofLatestWeek
¶
Date of first day of last week.
Returns the Sunday that immediately precedes the Monday starting the current week (weeks start on Monday and are
counted with %W). Every day from Monday to Sunday of one week gives the same result, for example 2026-09-27 for
2026-09-28 through 2026-10-04.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
retStr
|
bool
|
If True, return the date as a |
True
|
Returns:
| Type | Description |
|---|---|
str or datetime
|
The date described above. |
Source code in ExcelMaster/Utility.py
getLastCompletedVintage
¶
Last Completed Vintage
Returns the last day of the month that precedes the month of start_date (or of today), that is the most recent
completed month.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_date
|
str or None
|
Reference date as text in |
None
|
format
|
str
|
|
"%Y-%m-%d"
|
vintage
|
bool
|
If True, return the month as a |
False
|
Returns:
| Type | Description |
|---|---|
str or int
|
The last day of the previous month formatted with |
Source code in ExcelMaster/Utility.py
vin2quar
¶
String Vintage to Quarter (if completed month).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strDate
|
str
|
Vintage as |
required |
Returns:
| Type | Description |
|---|---|
str
|
|
Source code in ExcelMaster/Utility.py
list_files
¶
List all files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
location
|
str
|
Directory that is searched recursively ( |
required |
pattern
|
str
|
Regular expression searched ( |
required |
Returns:
| Type | Description |
|---|---|
list of str
|
Bare file names (without their directory) of the matching files in walk order; a name that exists in several directories appears once per directory. |
Source code in ExcelMaster/Utility.py
getCurrentDateTime
¶
Get Current DateTime
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fmt
|
str
|
|
"%Y%m%d%H%M%S"
|
Returns:
| Type | Description |
|---|---|
str
|
Current local date and time formatted with |
Source code in ExcelMaster/Utility.py
input_table_proc
¶
Process Input Table.
Lower-cases the column names of a table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tbl
|
DataFrame
|
Table with string column names. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
The same object with lower-cased column names; the caller's DataFrame is renamed in place. |
Source code in ExcelMaster/Utility.py
get_file_extension
¶
Get File Extentsion for a given file Path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
str
|
File path or name. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The extension including the leading dot, in its original case ( |
Source code in ExcelMaster/Utility.py
input_validation
¶
Input Validation.
Loads a table from a file path, or accepts a DataFrame, and lower-cases its column names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
str or DataFrame
|
A DataFrame, or the path of a file: a |
required |
sep
|
str
|
Field delimiter of the text file; ignored for |
","
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The table with lower-cased column names. A DataFrame passed in is returned as the same object, so its columns are renamed in place. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If |
Source code in ExcelMaster/Utility.py
val_input_condition
¶
Condition Tuple Validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
float or str
|
Value to test. |
required |
condition
|
tuple or str
|
A string tests equality: |
(">", 20)
|
Returns:
| Type | Description |
|---|---|
bool or None
|
The result of the test. None (falsy) for an operator that is not in the list above, for example |
Source code in ExcelMaster/Utility.py
get_quarter
¶
Get the quarter number of a vintage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strDate
|
str
|
Vintage as |
required |
Returns:
| Type | Description |
|---|---|
int
|
Quarter of the month, 1 to 4 (for example |
Source code in ExcelMaster/Utility.py
tanspose_dataframe
¶
Transpose Pandas DataFrame.
The spelling of the function name is historical.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Table to transpose. |
required |
index_col
|
str or list of str
|
Column(s) set as the index before the transposition; their values become the column labels of the result. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
|
Source code in ExcelMaster/Utility.py
convert_perc_str_to_float
¶
Percentage to Float.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Table to convert. |
required |
cols
|
list of str
|
Columns to convert. A column is converted only when its dtype is |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
|
Source code in ExcelMaster/Utility.py
color_hex2rgb
¶
Convert Color Hex Code to RGB Tuple.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hex_code
|
str
|
Color as six hexadecimal digits with an optional leading |
required |
Returns:
| Type | Description |
|---|---|
tuple of int
|
|
Source code in ExcelMaster/Utility.py
get_color_set
¶
Return a set of color code without replacement.
Picks colors from the XKCD color table of matplotlib (949 named colors).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of colors. |
required |
start_num
|
int
|
Index in the XKCD table of the first color. |
0
|
step
|
int
|
Distance between two consecutive picked indices ( |
1
|
retName
|
bool
|
If True, return a dict |
False
|
lookupName
|
str or None
|
Regular expression that replaces the index-based picks: the first |
None
|
Returns:
| Type | Description |
|---|---|
list of str or dict
|
Hex color codes ( |
Source code in ExcelMaster/Utility.py
string_proc
¶
Process a given String.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
str
|
Text whose words are separated by underscores, for example a column name. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The words capitalized (first letter upper-case, the rest lower-case) and joined by single spaces, for example
|
Source code in ExcelMaster/Utility.py
convert_to_boxplot_data
¶
Convert dataframe to boxplot data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Source table. |
required |
x
|
str
|
Column that defines the groups (one box per distinct value). |
required |
y
|
str
|
Column with the values of each group. |
required |
y_percentage
|
bool
|
If True, multiply every value by 100. |
False
|
Returns:
| Type | Description |
|---|---|
dict
|
|
Source code in ExcelMaster/Utility.py
color_input_validation
¶
Perform Color Input Validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
color_grp
|
tuple or str or list of str
|
A 2-tuple |
required |
val_n
|
int
|
Number of colors needed (number of boxes). |
required |
Returns:
| Type | Description |
|---|---|
list of str
|
Color codes, one per box. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in ExcelMaster/Utility.py
get_metric_shift
¶
Calculate Metric Shift for Variable Reduction.
For every row, the slope between consecutive rows: (metric - metric of the previous row) / (nvars - nvars of the
previous row), that is the change of the metric per variable added (or removed, when the rows are ordered by
decreasing number of variables).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Table with one row per model, ordered by the number of variables. |
required |
metric_name
|
str
|
Column holding the metric. |
required |
nvars_col
|
str
|
Column holding the number of variables. |
"nvars"
|
Returns:
| Type | Description |
|---|---|
Series
|
The shift of every row, with the index of |
Source code in ExcelMaster/Utility.py
get_metrics_shift
¶
Get Shift for List of Metrics.
Adds a column <metric>_shift for every metric, computed with get_metric_shift and its default
nvars_col="nvars", so data needs a column named nvars.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Table with one row per model, ordered by the number of variables. |
required |
metric_cols
|
list of str
|
Metric columns. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
|
Source code in ExcelMaster/Utility.py
compute_overfitting_shift
¶
Calculate Overfitting Performance Shift.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Table with the metric columns of both samples, named |
required |
sample_prefix
|
tuple of str
|
|
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the two prefixes select a different number of columns. |
Source code in ExcelMaster/Utility.py
proc_psi_raw_report
¶
Processing Raw PSI Report generated from Takecopter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
psi_raw_table
|
str or DataFrame
|
Raw PSI table: a DataFrame or the path of a csv / sas7bdat file, read with |
required |
psi_title
|
str
|
Title placed on the top level of the two-level column header of the result. |
required |
keep_list
|
list of str or None
|
Columns to keep, spelled as in the processed table (upper-case when |
None
|
varname
|
str
|
New name of the |
"variable"
|
upper
|
bool
|
If True, upper-case the names of the remaining columns. |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Table indexed by |
Source code in ExcelMaster/Utility.py
get_mean_risk
¶
get average risk
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bivar_single_attr
|
DataFrame
|
Bivariate table of one attribute with one row per bin. |
required |
value_range_col
|
list of str
|
Columns with the bounds of each bin. A row whose bounds are all missing is the missing-value bin and is left out of
|
['min_indep', 'max_indep']
|
dep_col
|
str
|
Column with the bad rate of each bin. |
"dep"
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
|