Excel workbooks (.xlsx)
Paths are relative to this skill's directory. Install pip install openpyxl pandas; LibreOffice is needed only for recalculation.
The one fact that shapes everything
openpyxl writes formulas but does not calculate them. A workbook it saves has
formulas and no stored results. Excel computes on open, but pandas,
data_only=True reads, previews and many viewers show nothing (None). So:
- Build with formulas, not pasted numbers, wherever the value is derived, so the
sheet stays live and auditable.
- Before you hand over or read results back, recalculate (below), or say that the
numbers appear only when the file is opened in a calculating application.
- Never read computed values from a file you just wrote with openpyxl and treat
None as zero.
Pick the route
| You want to |
Use |
| Load a sheet for analysis |
pandas.read_excel(path, sheet_name=...) (values only) |
| Write a data table quickly |
DataFrame.to_excel(...), then style with openpyxl |
| Build a model with formulas, formats, charts |
openpyxl, see references/openpyxl-recipes.md |
| Edit an existing workbook |
openpyxl.load_workbook(path) (not data_only=True, or formulas are lost on save) |
| See what is in a workbook, lint formulas |
scripts/xlsx_inspect.py model.xlsx |
| Find formula errors after calculation |
scripts/xlsx_recalc.py model.xlsx --check |
| Get stored values into the file |
scripts/xlsx_recalc.py model.xlsx -o calculated.xlsx |
| Fix package-level damage |
scripts/ooxml_tools.py check / unpack / pack |
Scripts
xlsx_inspect.py reads a workbook and reports sheets, formula counts, defined
names, and problems: cached errors, formulas with no stored value, references to
sheets that do not exist, links to other workbooks, volatile functions (INDIRECT,
OFFSET, NOW, ...) and a literal-zero divisor. Add --formulas to list each
formula with its stored value and --allow-uncalculated to skip the "no stored
value" finding for a file you just generated.
xlsx_recalc.py runs LibreOffice headless on a copy, reads back the computed
values, and lists every error cell. --check writes nothing. -o writes the
recalculated file; compare it with your original before replacing it, because
LibreOffice may restyle or drop features it does not support. If LibreOffice is
missing it exits 3 and says so; do not claim the workbook was calculated.
ooxml_tools.py is the shared package tool (unpack, pack, structural check).
Working rules
- Inputs apart from calculations. Put assumptions in one labelled place and
have formulas point at them. A constant buried inside a formula
(
=B7*1.0825) is a hidden assumption.
- Mark inputs. A common convention: blue font for typed inputs, black for
formulas, green for links to other sheets. Explain whichever you use in a note
on the sheet.
- Check the edges. Divide with a guard (
=IF(B2=0,"",A2/B2) or
IFERROR only where an error is truly acceptable), anchor ranges with $, and
keep ranges to the data you mean, not whole columns inside heavy formulas.
- Dates are numbers with a format. Write real
datetime objects and set
number_format; text that looks like a date will not sort or calculate.
- Keep it readable. Freeze header rows, set column widths, format numbers
(
'0.0%', '#,##0'), and avoid merged cells in data regions because they break
sorting and pandas reads.
- Totals reconcile. After building, recalculate and compare a few totals with
an independent calculation (pandas, or by hand) before reporting numbers.
- Report plainly. State which sheets you changed, which formulas you added,
and whether the workbook was recalculated and by what.
Limits
- openpyxl does not evaluate formulas, run macros (
.xlsm macros are kept only
with keep_vba=True), or render charts to images.
- Loading and saving with openpyxl can drop features it does not model: pivot
tables' cached data, slicers, some conditional-format extensions, form
controls, and images in certain forms. For a workbook that depends on them,
edit a copy and open the result in Excel to confirm, or edit the XML
(
ooxml_tools.py unpack) for a narrow change.
- Data-only reads (
data_only=True) return the values stored at the last save by
a calculating application; stale if nothing recalculated since an edit.
- Large files: use
read_only=True or write_only=True modes and avoid
per-cell style loops.
1---2name: xlsx3description: Create, read, edit and check Excel (.xlsx) workbooks and tabular files. Use for building spreadsheets with live formulas, reading sheets into pandas, editing an existing workbook without losing formatting, formatting and charts, finding broken formulas (#REF!, #DIV/0!, missing sheets), and recalculating with LibreOffice so stored values exist. Not for Word, PowerPoint, PDF or Google Sheets.4license: MIT5metadata:6 source: https://skillmd.com/skills/raktim-mondol/xlsx7 version: "2.0"8 skill-author: Academic Harness9 skill-source: original; drives openpyxl, pandas and LibreOffice, none of which are bundled10---1112# Excel workbooks (.xlsx)1314Paths are relative to this skill's directory. Install `pip install openpyxl15pandas`; LibreOffice is needed only for recalculation.1617## The one fact that shapes everything1819openpyxl **writes formulas but does not calculate them**. A workbook it saves has20formulas and no stored results. Excel computes on open, but pandas,21`data_only=True` reads, previews and many viewers show nothing (`None`). So:2223- Build with formulas, not pasted numbers, wherever the value is derived, so the24 sheet stays live and auditable.25- Before you hand over or read results back, recalculate (below), or say that the26 numbers appear only when the file is opened in a calculating application.27- Never read computed values from a file you just wrote with openpyxl and treat28 `None` as zero.2930## Pick the route3132| You want to | Use |33|---|---|34| Load a sheet for analysis | `pandas.read_excel(path, sheet_name=...)` (values only) |35| Write a data table quickly | `DataFrame.to_excel(...)`, then style with openpyxl |36| Build a model with formulas, formats, charts | openpyxl, see [references/openpyxl-recipes.md](references/openpyxl-recipes.md) |37| Edit an existing workbook | `openpyxl.load_workbook(path)` (not `data_only=True`, or formulas are lost on save) |38| See what is in a workbook, lint formulas | `scripts/xlsx_inspect.py model.xlsx` |39| Find formula errors after calculation | `scripts/xlsx_recalc.py model.xlsx --check` |40| Get stored values into the file | `scripts/xlsx_recalc.py model.xlsx -o calculated.xlsx` |41| Fix package-level damage | `scripts/ooxml_tools.py check / unpack / pack` |4243## Scripts4445`xlsx_inspect.py` reads a workbook and reports sheets, formula counts, defined46names, and problems: cached errors, formulas with no stored value, references to47sheets that do not exist, links to other workbooks, volatile functions (`INDIRECT`,48`OFFSET`, `NOW`, ...) and a literal-zero divisor. Add `--formulas` to list each49formula with its stored value and `--allow-uncalculated` to skip the "no stored50value" finding for a file you just generated.5152`xlsx_recalc.py` runs LibreOffice headless on a copy, reads back the computed53values, and lists every error cell. `--check` writes nothing. `-o` writes the54recalculated file; compare it with your original before replacing it, because55LibreOffice may restyle or drop features it does not support. If LibreOffice is56missing it exits 3 and says so; do not claim the workbook was calculated.5758`ooxml_tools.py` is the shared package tool (unpack, pack, structural check).5960## Working rules6162- **Inputs apart from calculations.** Put assumptions in one labelled place and63 have formulas point at them. A constant buried inside a formula64 (`=B7*1.0825`) is a hidden assumption.65- **Mark inputs.** A common convention: blue font for typed inputs, black for66 formulas, green for links to other sheets. Explain whichever you use in a note67 on the sheet.68- **Check the edges.** Divide with a guard (`=IF(B2=0,"",A2/B2)` or69 `IFERROR` only where an error is truly acceptable), anchor ranges with `$`, and70 keep ranges to the data you mean, not whole columns inside heavy formulas.71- **Dates are numbers with a format.** Write real `datetime` objects and set72 `number_format`; text that looks like a date will not sort or calculate.73- **Keep it readable.** Freeze header rows, set column widths, format numbers74 (`'0.0%'`, `'#,##0'`), and avoid merged cells in data regions because they break75 sorting and pandas reads.76- **Totals reconcile.** After building, recalculate and compare a few totals with77 an independent calculation (pandas, or by hand) before reporting numbers.78- **Report plainly.** State which sheets you changed, which formulas you added,79 and whether the workbook was recalculated and by what.8081## Limits8283- openpyxl does not evaluate formulas, run macros (`.xlsm` macros are kept only84 with `keep_vba=True`), or render charts to images.85- Loading and saving with openpyxl can drop features it does not model: pivot86 tables' cached data, slicers, some conditional-format extensions, form87 controls, and images in certain forms. For a workbook that depends on them,88 edit a copy and open the result in Excel to confirm, or edit the XML89 (`ooxml_tools.py unpack`) for a narrow change.90- Data-only reads (`data_only=True`) return the values stored at the last save by91 a calculating application; stale if nothing recalculated since an edit.92- Large files: use `read_only=True` or `write_only=True` modes and avoid93 per-cell style loops.