openavmkit.utilities.excel
write_to_excel
write_to_excel(df, path, metadata)
Write a DataFrame to an Excel file with custom column formats and conditional formatting.
Uses pandas ExcelWriter (XlsxWriter engine) to export df
to an Excel workbook, applies a bold,
wrapped header format, user-defined column formats, and conditional formatting rules as specified in
metadata
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
The DataFrame to write to Excel. Column order and values are preserved. |
required |
path
|
str
|
Filesystem path (including filename) where the Excel file will be created (e.g. 'out.xlsx'). |
required |
metadata
|
dict
|
Formatting instructions and sheet options. Keys include:
|
required |
Returns:
Type | Description |
---|---|
None
|
The function writes directly to disk and does not return a value. |
Notes
- Header cells are styled with bold text and text wrapping.
- Columns not listed in
metadata['columns']['formats']
are written without additional formatting. - Conditional formatting rules apply to the data rows (from row 1 to len(df)).
- Any unrecognized metadata keys are ignored.
Examples:
>>> metadata = {
... 'sheet_name': 'Sales',
... 'columns': {
... 'formats': {
... 'sales': {'num_format': '$#,##0.00', 'align': 'right'},
... 'region': {'text_wrap': True}
... },
... 'conditions': {
... 'sales': {
... 'type': 'cell',
... 'criteria': '>',
... 'value': 100000,
... 'format': {'bg_color': '#FFC7CE', 'font_color': '#9C0006'}
... }
... }
... }
... }
>>> write_to_excel(df, 'sales.xlsx', metadata)
Source code in openavmkit/utilities/excel.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|