Metadata-Version: 2.4
Name: mt940-camt053
Version: 0.1.0
Summary: Convert bank statements between SWIFT MT940 and ISO 20022 CAMT.053 (both ways), or to CSV. Offline, with a balance check.
License: MIT
Project-URL: Homepage, https://radarbg.com/instant/mt940-to-camt053?ref=pypi-mt940
Project-URL: Online converter (no install), https://radarbg.com/instant/mt940-to-camt053?ref=pypi-mt940
Project-URL: CAMT.053 to MT940 online, https://radarbg.com/instant/camt053-to-mt940?ref=pypi-mt940
Project-URL: SEPA return reason codes, https://radarbg.com/sepa-return-codes?ref=pypi-mt940
Keywords: mt940,camt053,camt.053,camt.052,iso20022,swift,bank-statement,sepa,converter
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Topic :: Office/Business :: Financial :: Accounting
Classifier: Intended Audience :: Financial and Insurance Industry
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: lxml>=4.6
Dynamic: license-file

# mt940-camt053

Convert bank statements between **SWIFT MT940** and **ISO 20022 CAMT.053**, in both directions, or flatten either one to **CSV**.
Pure Python + lxml. Runs on your machine: no network, no upload.

Banks are retiring MT940 in favour of CAMT.053, while many ERP and accounting systems still read only one of the two.
This library reads both into one statement model and writes either format back, and it checks that
opening balance + entries = closing balance for every statement.

No install? The same engine runs in the browser: [MT940 to CAMT.053 online converter](https://radarbg.com/instant/mt940-to-camt053?ref=pypi-mt940).

## Install

```
pip install mt940-camt053
```

## Command line

```
mt940-camt053 statement.sta                 # MT940 -> statement-camt053.xml (camt.053.001.08)
mt940-camt053 statement.xml                 # CAMT.053 -> statement.sta (MT940)
mt940-camt053 statement.sta --to csv        # one row per transaction
mt940-camt053 *.sta --version 001.02        # older CAMT version for older systems
mt940-camt053 in.xml --to mt940 -o out.sta
```

Each file prints one line: transactions, statements and the balance check result. Exit code 1 if a file could not be read.

## Python

```python
import mt940_camt053 as mc

stmts = mc.load("statement.sta")      # MT940, CAMT.053 or camt.052 - found out by itself
xml   = mc.to_camt053(stmts)          # bytes, camt.053.001.08 (or version="001.02")
text  = mc.to_mt940(stmts)            # str, CRLF lines, SWIFT character set
table = mc.to_csv(stmts)              # str, columns in mc.CSV_COLUMNS

for st in stmts:
    state, diff = mc.balance_check(st)        # "ok" | "bad" | "none"
    for t in st["txs"]:
        print(t["bdate"], t["amt"], t["name"], t["iban"], t["remit"])
```

`mc.loads(data, name)` takes bytes. `mc.convert_file(src, dst, to="auto")` converts a file on disk and returns a small summary.

## What it reads

| Format | Details |
|---|---|
| MT940 | Tags `:20: :25: :28C: :60F/M: :61: :86: :62F/M: :64:`, several statements per file, `{1:}{2:}{4: -}` envelopes, CRLF or LF |
| `:86:` text | German structured fields (`?20..?29` purpose, `?30` BIC, `?31` IBAN, `?32/?33` name), `/EREF/../NAME/../REMI/..` and `EREF+` / `SVWZ+` / `MREF+` keywords |
| Encodings | UTF-8 (with or without BOM), cp1252, latin-1, UTF-16 |
| CAMT | camt.053 and camt.052, any version (the namespace is ignored), `Stmt` or `Rpt` |

## What it writes

| Format | Details |
|---|---|
| CAMT.053 | `camt.053.001.08` (default) or `camt.053.001.02`, elements in schema order, reversals as `RvslInd=true` |
| MT940 | `:61:` and `:86:` with SWIFT line lengths; umlauts and Cyrillic are transliterated to the SWIFT character set |
| CSV | Signed amounts (negative = money out), booking and value date, counterparty name, IBAN, BIC, remittance text, end-to-end id, mandate id, references |

## Transaction fields

`bdate`, `vdate` (dates), `amt` (Decimal, signed), `rev` (reversal), `name`, `iban`, `bic`, `remit` (remittance text),
`e2e` (end-to-end id), `mndt` (mandate id), `bref` (bank reference), `cref` (customer reference), `type`, `gvc`, `text`, `supp`.

A statement has `ref`, `acct`, `seq`, `cur`, `open`, `close`, `avail` (balances with `amt`, `date`, `cur`) and `txs`.

## Limits, said plainly

- MT940 carries less than CAMT.053. Going CAMT -> MT940 cuts long texts at 6 lines of 65 characters and drops what MT940 has no place for; `notes` tells you when that happened.
- Going MT940 -> CAMT.053 can only fill what the MT940 file holds. Structured party data exists only if the bank wrote it in `:86:`.
- PDF or scanned statements are not read (that needs OCR).
- Bank dialects of `:86:` differ. If a file from your bank is read wrongly, the numbers are still copied as they are and the balance check tells you whether the statement adds up.

## Related

- [CAMT.053 to MT940 online](https://radarbg.com/instant/camt053-to-mt940?ref=pypi-mt940)
- [SEPA return reason codes, all of them with plain meanings](https://radarbg.com/sepa-return-codes?ref=pypi-mt940)
- [SEPA XML check (pain.001 / pain.008)](https://radarbg.com/instant/sepa-check?ref=pypi-mt940)

MIT licence. Made by [Radar](https://radarbg.com/?ref=pypi-mt940).
