DEV Community

Martin Petrov
Martin Petrov

Posted on

Convert a camt.053 bank statement to CSV/Excel with plain Python

Most European banks give you the account statement as a camt.053 XML file (ISO 20022). It replaces the old MT940 text format. Accountants want it in Excel. Here is how to turn it into a clean CSV with only the Python standard library, and the traps that make naive parsers miss transactions.

What is inside a camt.053 file

Document
 └ BkToCstmrStmt
    └ Stmt              (one per account and period)
       ├ Acct/Id/IBAN
       ├ Bal            (opening and closing balance)
       └ Ntry           (one booked entry)
          ├ Amt Ccy="EUR"
          ├ CdtDbtInd   CRDT = money in, DBIT = money out
          ├ BookgDt/Dt
          └ NtryDtls/TxDtls   (one or more transactions inside the entry)
             ├ RltdPties/Dbtr or Cdtr  -> the other party
             └ RmtInf/Ustrd            -> the payment reference text
Enter fullscreen mode Exit fullscreen mode

Two things break simple scripts:

  1. Namespaces differ by version (camt.053.001.02, .001.04, .001.08 ...). Hard-coding one namespace silently returns zero rows for the others.
  2. One Ntry can hold many TxDtls (a batch booking, for example a salary run). If you read only the entry, you lose the details.

The parser

Python 3.8+ supports {*} as "any namespace" in ElementTree paths, which solves problem 1.

import csv
import sys
import xml.etree.ElementTree as ET

def text(el, path):
    v = el.findtext(path)
    return v.strip() if v else ""

def rows(path):
    root = ET.parse(path).getroot()
    for stmt in root.iter("{*}Stmt"):
        iban = text(stmt, "{*}Acct/{*}Id/{*}IBAN")
        for ntry in stmt.iter("{*}Ntry"):
            sign = -1 if text(ntry, "{*}CdtDbtInd") == "DBIT" else 1
            date = text(ntry, "{*}BookgDt/{*}Dt") or text(ntry, "{*}BookgDt/{*}DtTm")[:10]
            ccy = ntry.find("{*}Amt").get("Ccy")
            txs = list(ntry.iter("{*}TxDtls")) or [ntry]
            for tx in txs:
                amt_el = tx.find("{*}Amt") if tx is not ntry else None
                amount = float((amt_el if amt_el is not None else ntry.find("{*}Amt")).text)
                side = "Cdtr" if sign < 0 else "Dbtr"   # the other party
                party = text(tx, f".//{{*}}RltdPties/{{*}}{side}//{{*}}Nm")
                ref = " ".join(t.text.strip() for t in tx.iter("{*}Ustrd") if t.text)
                yield {"account": iban, "date": date, "amount": round(sign * amount, 2),
                       "currency": ccy, "party": party, "reference": ref}

if __name__ == "__main__":
    data = list(rows(sys.argv[1]))
    with open("statement.csv", "w", newline="", encoding="utf-8-sig") as f:
        w = csv.DictWriter(f, fieldnames=data[0].keys(), delimiter=";")
        w.writeheader()
        w.writerows(data)
    print(len(data), "rows")
Enter fullscreen mode Exit fullscreen mode

Run it:

python camt2csv.py statement_2026-10.xml
Enter fullscreen mode Exit fullscreen mode

Details that matter for Excel

  • utf-8-sig writes a BOM, so Excel shows umlauts and Cyrillic correctly when you double-click the file.
  • ; as delimiter, because Excel in most EU locales expects it (the comma is the decimal separator there).
  • Money out is negative. Your sum per month then equals closing balance minus opening balance, which is a good self-test:
total = round(sum(r["amount"] for r in data), 2)
print("sum of entries:", total)
Enter fullscreen mode Exit fullscreen mode

Compare that with the two Bal elements (Tp/CdOrPrtry/Cd = OPBD and CLBD). If they do not match, you missed entries, usually because of problem 2 above.

MT940 too?

Some banks still export MT940. It is a line format (:61: for the booking, :86: for the details) and every bank fills :86: a little differently, so a parser needs per-bank rules.

If you just need the spreadsheet and not the code, the free bank statement to Excel converter reads camt.053, camt.052 and MT940 and gives you an .xlsx.

Which bank formats give you the most trouble?

Disclosure: I build the tool used in this post, and the article was written with AI assistance. If a code sample does not work for you, tell me in the comments and I will fix it.

Top comments (0)