Workbook output¶
The input shapes write_xlsx accepts, how values are coerced, the
summary sheet and the errors it raises. This page is the README's
Features section, included verbatim so the two cannot drift.
Input shapes¶
write_xlsx(data, path, *, sheet_name="Transactions", summary=None)
accepts three input shapes and normalises each to a flat table:
| Input | Column order |
|---|---|
pandas.DataFrame (from a parser's .parse()) |
the DataFrame's own column order |
list[bankstatementparser.Transaction] |
the stable Transaction field order |
list[dict] |
the union of keys, in first-seen order |
A header row (bold) is written to the sheet_name sheet, followed by
one row per record. Columns are auto-sized to their widest cell, capped
at 60 characters so wide descriptions don't run off-screen (the cell
content itself is never truncated; only the displayed column width).
Empty input is accepted: an empty list writes an empty sheet (no
header), while an empty DataFrame that still carries column labels
writes a header-only sheet.
Value coercion¶
Spreadsheet cells can't hold arbitrary Python objects, so the writer coerces the rich types the parser emits:
| Python type | Written as |
|---|---|
decimal.Decimal |
float (Excel has no decimal type; floats aggregate natively) |
datetime.date / datetime.datetime |
native Excel date cell (unchanged) |
str, int, float, bool, None |
unchanged |
None / float('nan') (e.g. a missing DataFrame cell) |
blank cell |
| anything else | str(value) |
bool is preserved as a true/false cell (it is not coerced to 0/1),
and a missing list[dict] key writes a blank cell in the same way a
None value does.
Every string (values, column names and the summary) is written as a
text cell holding exactly the original text: one that starts with =
is never stored as a formula, so a crafted transaction description
cannot become a live formula in the workbook (CWE-1236).
Errors¶
write_xlsx validates its data argument and fails fast with a precise
exception rather than writing a malformed workbook:
| Condition | Raised |
|---|---|
data is neither a DataFrame nor a list/tuple (e.g. a str or int) |
TypeError |
data is a non-empty sequence whose items are neither dict records nor Transaction objects (e.g. [42]) |
ValueError |
A value, a column name or the summary holds a control character an .xlsx cannot store (C0 characters other than tab, newline and carriage return) |
ValueError |
from bankstatementparser_writer_xlsx import write_xlsx
try:
write_xlsx("not-a-table", "out.xlsx") # a bare str is not a table
except TypeError as exc:
print(f"rejected: {exc}")
try:
write_xlsx([42], "out.xlsx") # 42 is not a dict/Transaction
except ValueError as exc:
print(f"rejected: {exc}")
The summary sheet¶
If you pass summary= a mapping (for example a parser's
get_summary() result), the writer adds a second sheet titled
Summary with a bold Key / Value header and one row per item:
from decimal import Decimal
from bankstatementparser_writer_xlsx import write_xlsx
transactions = [
{"date": "2026-06-01", "description": "Salary", "amount": Decimal("3000.00")},
{"date": "2026-06-03", "description": "Coffee Shop", "amount": Decimal("-4.20")},
]
write_xlsx(
transactions,
"out.xlsx",
summary={
"account_id": "DE89370400440532013000",
"transaction_count": 128,
"total_amount": Decimal("12045.67"),
"currency": "EUR",
},
)