Automate and validate Excel workbook tasks in this repository. In WSL or headless Linux environments, default to openpyxl; use xlwings only when the active environment actually supports desktop Excel automation. Use when creating/updating `.xlsx` or `.xlsm` files, writing formulas, filling ranges, or generating Excel reports. Always reopen the output workbook to verify formula/function errors and unexpected blank cells, minimize sheet count, and document every Excel Table with a `table_guide` sheet. In `analysis/` workflows tied to statistical or aggregation exploration, prefer `.py` for Excel generation while allowing `.ipynb` for exploration.
openpyxl for workbook generation and validation.xlwings only when the active environment actually supports desktop Excel automation and the task benefits from native Excel behavior.xlwings is unavailable or the current environment cannot automate a desktop Excel session, explicitly note that constraint and continue with openpyxl.pandas.ExcelWriter or shell-only alternatives as the primary Excel engine unless the user explicitly overrides..xlsx (switch to .xlsm only when the user explicitly needs macros).number_format"@"str(...)analysis/ tasks in statistical-analysis or aggregation contexts:
.ipynb for exploratory analysis/aggregation attempts..py scripts for final Excel workbook generation.tables)table_guidesummary sheet:
table reading guide block above the first tableselected, raw, or detail tables connect to each othersummary only when the workbook has important detail sheets.
purpose, key columns, and at least one worked example.ListObject) rather than loose ranges whenever feasible.table_guide is mandatory when tables are generated. Each table must have a human-readable description.table_guide should act as a reading index, not just a raw inventory.
column_guide field or equivalent text that explains what the main columns mean.PowerShell or cmd syntax only when the environment supports Windows Excel automation. In non-Windows or headless environments, document the limitation and use the available Python-based fallback path.xlwings, do not start cell text with = (for example avoid === ... ===).
= as a formula and may throw COM error 0x800A03EC/-2146827284.= prefixes for section titles (for example [section] ...).analysis/ statistical or aggregation workflows, use .ipynb for exploration when needed..py script with xlwings when available; otherwise use an explicit openpyxl fallback and record why.table_guide with per-table descriptions and concise column-reading help.wb.close(), app context exit).tbl_<domain>_<scope>.table_guide columns:
table_namesheet_nametable_rangedescriptionkey_columnscolumn_guide (recommended for interpretation workbooks)notes (optional)description should explain what the table represents and how to interpret it in one short sentence.column_guide should briefly explain what the main columns mean, using the exact column names where possible.Run every check after reopening the output workbook:
Formula/function error check:
#DIV/0!, #N/A, #NAME?, #NULL!, #NUM!, #REF!, #VALUE!.Broken formula check:
#REF!).Blank-cell check:
None or empty string after trim).Table integrity check:
table_guide is missing.table_guide row.description is blank in table_guide.purpose, key columns, example) are missing on important sheets.Record a validation summary:
Label safety check (xlwings write path):
=.= before writing to cells.Engine disclosure check:
openpyxl was the default engine.xlwings was not used because desktop Excel automation was unavailable, say so explicitly.from openpyxl import load_workbook
ERROR_TOKENS = {"#DIV/0!", "#N/A", "#NAME?", "#NULL!", "#NUM!", "#REF!", "#VALUE!"}
def is_blank(value):
return value is None or (isinstance(value, str) and value.strip() == "")
def validate_book(path, required_ranges):
issues = {"errors": [], "blanks": [], "tables": []}
wb = load_workbook(path)
try:
for sheet_name, addr in required_ranges:
sheet = wb[sheet_name]
for row in sheet[addr]:
for cell in row:
value = cell.value
if isinstance(value, str) and value in ERROR_TOKENS:
issues["errors"].append((sheet_name, cell.coordinate, value))
if is_blank(value):
issues["blanks"].append((sheet_name, cell.coordinate))
if "table_guide" not in wb.sheetnames:
issues["tables"].append(("table_guide", "missing"))
finally:
wb.close()
return issues
Use this when you are writing IDs/codes/labels (not formulas and not numeric analytics columns).
def write_text_cell(cell, value):
cell.number_format = "@"
cell.value = "" if value is None else str(value)
NOTE: scripts/excel_job.py is a placeholder example path (this repo may not include that file). Replace it with your actual Excel-generation script under scripts/.
PowerShell:
conda run -n cuda python -c "import sys; print(sys.executable)"conda run -n cuda python .\scripts\excel_job.py --input ".\input.xlsx" --output ".\output.xlsx"Start-Process ".\output.xlsx"