DEV Community

Cover image for Hand bank data from Python to Excel as XLSX, not CSV, to keep references intact
BankStatementScan
BankStatementScan

Posted on

Hand bank data from Python to Excel as XLSX, not CSV, to keep references intact

If a Python pipeline hands bank transactions to someone who works in Excel, write an XLSX file rather than a CSV. A CSV carries no column types, so every person who double-clicks it lets Excel guess again: references lose leading zeros, 16-digit identifiers end in 0, dates can flip. An XLSX stores each cell's type, so you decide once, in code.

Quoting fields in the CSV does not change this. Quotes only mark where a field starts and ends; they do not tell Excel that 00123 is text.

How do you write the workbook?

This ran locally with openpyxl on fictional rows:

from datetime import date
from decimal import Decimal
from openpyxl import Workbook, load_workbook

# Fictional transactions to hand over to an Excel user.
rows = [
    (date(2026, 9, 18), "00123", "Café, morning", Decimal("-19.80")),
    (date(2026, 9, 19), "1234567890123451", "=1+1", Decimal("50.00")),
]

wb = Workbook()
ws = wb.active
ws.append(["date", "reference", "description", "amount"])
for d, ref, desc, amount in rows:
    ws.append([d, ref, desc, float(amount)])
    r = ws.max_row
    ws.cell(r, 1).number_format = "yyyy-mm-dd"
    ws.cell(r, 2).number_format = "@"          # text format for the identifier
    ws.cell(r, 3).data_type = "s"              # store the description as a string
    ws.cell(r, 4).number_format = "0.00"
wb.save("handover.xlsx")

for row in load_workbook("handover.xlsx").active.iter_rows(min_row=2, values_only=True):
    print([type(v).__name__ for v in row], row[1], row[2])
Enter fullscreen mode Exit fullscreen mode

Reading the file back prints a date, two strings and a number for each row, with 00123 and 1234567890123451 intact and =1+1 stored as text.

Why force the description to a string?

Because openpyxl, like Excel, treats a string that starts with = as a formula by default: appending "=1+1" gives the cell the data type f. Bank descriptions and payment references are partly typed by other people, so a description beginning with =, + or @ must stay inert text. Setting data_type = "s" after the append stores it as a plain string; the saved sheet contains no formula element for that cell.

Why keep references as Python strings?

The pipeline must never pass references through int or float. Python integers keep 16 digits, but Excel keeps 15 significant digits in a number, so a numeric cell would be rounded the moment the workbook is opened. A string cell, with the @ text format, survives both sides.

What still needs checking?

  • Amounts: write them as numbers from Decimal values you have already validated, and keep the sign convention in the header or a note.
  • Dates: write real date objects; a display format such as yyyy-mm-dd is unambiguous for the reader.
  • Totals: recompute debits, credits and the closing balance on the written file, not on the data you meant to write.

When is a CSV still required?

When the receiving system's import specification asks for one. Then add a short field note: reference is text, date format, decimal mark, sign convention. And test the file through the recipient's real import route.

Read the full guides

BankStatementScan turns scanned or PDF bank statements into Excel rows that you review beside the source page. Get started

Top comments (0)