bai2-to-excel 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- bai2_to_excel-0.1.0/LICENSE +21 -0
- bai2_to_excel-0.1.0/PKG-INFO +77 -0
- bai2_to_excel-0.1.0/README.md +57 -0
- bai2_to_excel-0.1.0/pyproject.toml +32 -0
- bai2_to_excel-0.1.0/setup.cfg +4 -0
- bai2_to_excel-0.1.0/src/bai2_to_excel/__init__.py +259 -0
- bai2_to_excel-0.1.0/src/bai2_to_excel/__main__.py +23 -0
- bai2_to_excel-0.1.0/src/bai2_to_excel.egg-info/PKG-INFO +77 -0
- bai2_to_excel-0.1.0/src/bai2_to_excel.egg-info/SOURCES.txt +10 -0
- bai2_to_excel-0.1.0/src/bai2_to_excel.egg-info/dependency_links.txt +1 -0
- bai2_to_excel-0.1.0/src/bai2_to_excel.egg-info/entry_points.txt +2 -0
- bai2_to_excel-0.1.0/src/bai2_to_excel.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 RadarBG
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: bai2-to-excel
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Parse corporate BAI2 bank statement files (Chase, BofA, Wells Fargo, Citi, HSBC) offline into Python dicts, CSV, Xero CSV, QuickBooks CSV or JSON with balance checks.
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Project-URL: Homepage, https://radarbg.com/instant/bai2-to-excel?ref=pypi-bai2
|
|
7
|
+
Project-URL: BAI2 to Excel online, https://radarbg.com/instant/bai2-to-excel?ref=pypi-bai2
|
|
8
|
+
Project-URL: PDF Bank Statement to Excel, https://radarbg.com/instant/bank-statement-pdf-to-excel?ref=pypi-bai2
|
|
9
|
+
Project-URL: CAMT.053 & MT940 Converter, https://radarbg.com/instant/camt053-to-excel?ref=pypi-bai2
|
|
10
|
+
Keywords: bai2,bai,bank-statement,treasury,accounting,csv,excel,xero,quickbooks,parser
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Topic :: Office/Business :: Financial :: Accounting
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
16
|
+
Requires-Python: >=3.8
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
# bai2-to-excel
|
|
22
|
+
|
|
23
|
+
Parse **corporate BAI2 bank statement files** (`.bai`, `.bai2`, `.txt`) offline into clean Python dicts, CSV, Xero CSV, QuickBooks/Sage CSV, or JSON — with automatic running balance and opening/closing balance verification.
|
|
24
|
+
|
|
25
|
+
Works with corporate treasury files from **JPMorgan Chase, Bank of America, Wells Fargo, Citibank, HSBC, Barclays**, and any bank following the Bank Administration Institute (BAI2) specification.
|
|
26
|
+
|
|
27
|
+
- **Pure Python, standard library only** (Python 3.8+, zero dependencies)
|
|
28
|
+
- **100% offline** — nothing uploaded, no network calls
|
|
29
|
+
- Handles records `01`, `02`, `03`, `16`, `88` (multi-line continuations), `49`, `98`, `99`
|
|
30
|
+
- Maps official BAI2 transaction type codes (`165` ACH Credit, `475` Check Paid, `195`/`495` Wire, `115` Lockbox, `698` Bank Fee, etc.) with automatic credit/debit sign detection
|
|
31
|
+
- Verifies `opening_balance + sum(transactions) == closing_balance` per account
|
|
32
|
+
|
|
33
|
+
> **Need Excel (`.xlsx`) without installing Python?**
|
|
34
|
+
> Convert BAI2 files directly in your browser (including multi-file `.zip` batches and Excel `.xlsx` with a Summary sheet):
|
|
35
|
+
> **[https://radarbg.com/instant/bai2-to-excel](https://radarbg.com/instant/bai2-to-excel?ref=pypi-bai2)**
|
|
36
|
+
|
|
37
|
+
## Install
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install bai2-to-excel
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Python usage
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
import bai2_to_excel as b2
|
|
47
|
+
|
|
48
|
+
accounts = b2.parse_file("statement.bai2")
|
|
49
|
+
for acct in accounts:
|
|
50
|
+
print(acct["account"], acct["currency"], acct["balance_ok"])
|
|
51
|
+
for tx in acct["transactions"]:
|
|
52
|
+
print(tx["date"], tx["amount"], tx["description"], tx["balance"])
|
|
53
|
+
|
|
54
|
+
# Export directly to CSV, Xero CSV, QuickBooks CSV, or JSON
|
|
55
|
+
csv_text = b2.to_csv(accounts)
|
|
56
|
+
xero_csv = b2.to_xero_csv(accounts)
|
|
57
|
+
json_text = b2.to_json(accounts)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Command line
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
# Convert a BAI2 file to standard CSV (Excel-ready)
|
|
64
|
+
bai2-to-excel statement.bai2 -o statement.csv
|
|
65
|
+
|
|
66
|
+
# Export for Xero, QuickBooks Online / Sage, or JSON
|
|
67
|
+
bai2-to-excel statement.bai2 --format xero -o statement-xero.csv
|
|
68
|
+
bai2-to-excel statement.bai2 --format qbo -o statement-qbo.csv
|
|
69
|
+
bai2-to-excel statement.bai2 --format json -o statement.json
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Related online accounting tools
|
|
73
|
+
|
|
74
|
+
- [BAI2 to Excel (.xlsx) online](https://radarbg.com/instant/bai2-to-excel?ref=pypi-bai2)
|
|
75
|
+
- [PDF Bank Statement to Excel / Xero / QBO / OFX](https://radarbg.com/instant/bank-statement-pdf-to-excel?ref=pypi-bai2)
|
|
76
|
+
- [CAMT.053 & MT940 Bank Statement Converter](https://radarbg.com/instant/camt053-to-excel?ref=pypi-bai2)
|
|
77
|
+
- [SEPA XML Generator & Checker](https://radarbg.com/instant/sepa-xml?ref=pypi-bai2)
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# bai2-to-excel
|
|
2
|
+
|
|
3
|
+
Parse **corporate BAI2 bank statement files** (`.bai`, `.bai2`, `.txt`) offline into clean Python dicts, CSV, Xero CSV, QuickBooks/Sage CSV, or JSON — with automatic running balance and opening/closing balance verification.
|
|
4
|
+
|
|
5
|
+
Works with corporate treasury files from **JPMorgan Chase, Bank of America, Wells Fargo, Citibank, HSBC, Barclays**, and any bank following the Bank Administration Institute (BAI2) specification.
|
|
6
|
+
|
|
7
|
+
- **Pure Python, standard library only** (Python 3.8+, zero dependencies)
|
|
8
|
+
- **100% offline** — nothing uploaded, no network calls
|
|
9
|
+
- Handles records `01`, `02`, `03`, `16`, `88` (multi-line continuations), `49`, `98`, `99`
|
|
10
|
+
- Maps official BAI2 transaction type codes (`165` ACH Credit, `475` Check Paid, `195`/`495` Wire, `115` Lockbox, `698` Bank Fee, etc.) with automatic credit/debit sign detection
|
|
11
|
+
- Verifies `opening_balance + sum(transactions) == closing_balance` per account
|
|
12
|
+
|
|
13
|
+
> **Need Excel (`.xlsx`) without installing Python?**
|
|
14
|
+
> Convert BAI2 files directly in your browser (including multi-file `.zip` batches and Excel `.xlsx` with a Summary sheet):
|
|
15
|
+
> **[https://radarbg.com/instant/bai2-to-excel](https://radarbg.com/instant/bai2-to-excel?ref=pypi-bai2)**
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pip install bai2-to-excel
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Python usage
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
import bai2_to_excel as b2
|
|
27
|
+
|
|
28
|
+
accounts = b2.parse_file("statement.bai2")
|
|
29
|
+
for acct in accounts:
|
|
30
|
+
print(acct["account"], acct["currency"], acct["balance_ok"])
|
|
31
|
+
for tx in acct["transactions"]:
|
|
32
|
+
print(tx["date"], tx["amount"], tx["description"], tx["balance"])
|
|
33
|
+
|
|
34
|
+
# Export directly to CSV, Xero CSV, QuickBooks CSV, or JSON
|
|
35
|
+
csv_text = b2.to_csv(accounts)
|
|
36
|
+
xero_csv = b2.to_xero_csv(accounts)
|
|
37
|
+
json_text = b2.to_json(accounts)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Command line
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
# Convert a BAI2 file to standard CSV (Excel-ready)
|
|
44
|
+
bai2-to-excel statement.bai2 -o statement.csv
|
|
45
|
+
|
|
46
|
+
# Export for Xero, QuickBooks Online / Sage, or JSON
|
|
47
|
+
bai2-to-excel statement.bai2 --format xero -o statement-xero.csv
|
|
48
|
+
bai2-to-excel statement.bai2 --format qbo -o statement-qbo.csv
|
|
49
|
+
bai2-to-excel statement.bai2 --format json -o statement.json
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Related online accounting tools
|
|
53
|
+
|
|
54
|
+
- [BAI2 to Excel (.xlsx) online](https://radarbg.com/instant/bai2-to-excel?ref=pypi-bai2)
|
|
55
|
+
- [PDF Bank Statement to Excel / Xero / QBO / OFX](https://radarbg.com/instant/bank-statement-pdf-to-excel?ref=pypi-bai2)
|
|
56
|
+
- [CAMT.053 & MT940 Bank Statement Converter](https://radarbg.com/instant/camt053-to-excel?ref=pypi-bai2)
|
|
57
|
+
- [SEPA XML Generator & Checker](https://radarbg.com/instant/sepa-xml?ref=pypi-bai2)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "bai2-to-excel"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Parse corporate BAI2 bank statement files (Chase, BofA, Wells Fargo, Citi, HSBC) offline into Python dicts, CSV, Xero CSV, QuickBooks CSV or JSON with balance checks."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
dependencies = []
|
|
13
|
+
keywords = ["bai2", "bai", "bank-statement", "treasury", "accounting", "csv", "excel", "xero", "quickbooks", "parser"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"Operating System :: OS Independent",
|
|
17
|
+
"Topic :: Office/Business :: Financial :: Accounting",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"Intended Audience :: Financial and Insurance Industry",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
[project.scripts]
|
|
23
|
+
bai2-to-excel = "bai2_to_excel.__main__:main"
|
|
24
|
+
|
|
25
|
+
[project.urls]
|
|
26
|
+
Homepage = "https://radarbg.com/instant/bai2-to-excel?ref=pypi-bai2"
|
|
27
|
+
"BAI2 to Excel online" = "https://radarbg.com/instant/bai2-to-excel?ref=pypi-bai2"
|
|
28
|
+
"PDF Bank Statement to Excel" = "https://radarbg.com/instant/bank-statement-pdf-to-excel?ref=pypi-bai2"
|
|
29
|
+
"CAMT.053 & MT940 Converter" = "https://radarbg.com/instant/camt053-to-excel?ref=pypi-bai2"
|
|
30
|
+
|
|
31
|
+
[tool.setuptools.packages.find]
|
|
32
|
+
where = ["src"]
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
"""Pure-Python offline parser for corporate BAI2 bank statement files."""
|
|
2
|
+
import csv
|
|
3
|
+
import datetime as dt
|
|
4
|
+
import io
|
|
5
|
+
import json
|
|
6
|
+
import re
|
|
7
|
+
from decimal import Decimal
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
__version__ = "0.1.0"
|
|
11
|
+
__all__ = ["BAI2Error", "parse", "parse_file", "to_csv", "to_xero_csv", "to_qbo_csv", "to_json"]
|
|
12
|
+
|
|
13
|
+
_BAI2_CODES = {
|
|
14
|
+
"115": ("Lockbox Deposit", 1),
|
|
15
|
+
"142": ("ACH Credit Received", 1),
|
|
16
|
+
"165": ("Preauthorized ACH Credit", 1),
|
|
17
|
+
"166": ("ACH Settlement Credit", 1),
|
|
18
|
+
"168": ("ACH Return Item Credit", 1),
|
|
19
|
+
"175": ("Check Deposit Package", 1),
|
|
20
|
+
"195": ("Incoming Money Transfer (Wire)", 1),
|
|
21
|
+
"201": ("Individual Automatic Transfer Credit", 1),
|
|
22
|
+
"208": ("Incoming International Wire", 1),
|
|
23
|
+
"252": ("Debit Reversal", 1),
|
|
24
|
+
"275": ("ZBA Credit", 1),
|
|
25
|
+
"301": ("Commercial Deposit", 1),
|
|
26
|
+
"354": ("Interest Credit", 1),
|
|
27
|
+
"399": ("Miscellaneous Credit", 1),
|
|
28
|
+
"451": ("ACH Debit Received", -1),
|
|
29
|
+
"455": ("Preauthorized ACH Debit", -1),
|
|
30
|
+
"466": ("ACH Settlement Debit", -1),
|
|
31
|
+
"468": ("ACH Return Item Debit", -1),
|
|
32
|
+
"475": ("Check Paid", -1),
|
|
33
|
+
"481": ("Individual Loan Payment", -1),
|
|
34
|
+
"495": ("Outgoing Money Transfer (Wire)", -1),
|
|
35
|
+
"501": ("Individual Automatic Transfer Debit", -1),
|
|
36
|
+
"508": ("Outgoing International Wire", -1),
|
|
37
|
+
"555": ("Deposited Item Returned", -1),
|
|
38
|
+
"575": ("ZBA Debit", -1),
|
|
39
|
+
"631": ("Debit Adjustment", -1),
|
|
40
|
+
"698": ("Miscellaneous Bank Fee", -1),
|
|
41
|
+
"699": ("Miscellaneous Debit", -1),
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
_CUR_DEC = {"JPY": 0, "KRW": 0, "BHD": 3, "KWD": 3, "OMR": 3}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class BAI2Error(ValueError):
|
|
48
|
+
"""Raised when a BAI2 file cannot be parsed."""
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _yymmdd(s):
|
|
52
|
+
s = (s or "").strip()
|
|
53
|
+
if not re.fullmatch(r"\d{6}", s):
|
|
54
|
+
return None
|
|
55
|
+
yy, mm, dd = int(s[:2]), int(s[2:4]), int(s[4:6])
|
|
56
|
+
year = 2000 + yy if yy < 70 else 1900 + yy
|
|
57
|
+
try:
|
|
58
|
+
return dt.date(year, mm, dd)
|
|
59
|
+
except ValueError:
|
|
60
|
+
return None
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _amt(raw, cur="USD"):
|
|
64
|
+
s = (raw or "").strip()
|
|
65
|
+
if not s or not re.fullmatch(r"[+-]?\d+", s):
|
|
66
|
+
return None
|
|
67
|
+
dec = _CUR_DEC.get(cur, 2)
|
|
68
|
+
return (Decimal(s) / (Decimal(10) ** dec)).quantize(Decimal("0.01"))
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _sign(code):
|
|
72
|
+
if code in _BAI2_CODES:
|
|
73
|
+
return _BAI2_CODES[code][1]
|
|
74
|
+
try:
|
|
75
|
+
n = int(code)
|
|
76
|
+
except ValueError:
|
|
77
|
+
return 1
|
|
78
|
+
return -1 if 400 <= n <= 699 else 1
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def parse(data):
|
|
82
|
+
"""Parse BAI2 bytes or str -> list of account dicts with verified running balances."""
|
|
83
|
+
if isinstance(data, (bytes, bytearray)):
|
|
84
|
+
for enc in ("utf-8-sig", "cp1252", "latin-1"):
|
|
85
|
+
try:
|
|
86
|
+
text = data.decode(enc)
|
|
87
|
+
break
|
|
88
|
+
except UnicodeDecodeError:
|
|
89
|
+
continue
|
|
90
|
+
else:
|
|
91
|
+
text = data.decode("latin-1", errors="replace")
|
|
92
|
+
else:
|
|
93
|
+
text = str(data or "")
|
|
94
|
+
|
|
95
|
+
raw_lines = [ln.strip() for ln in text.replace("\r\n", "\n").replace("\r", "\n").split("\n") if ln.strip()]
|
|
96
|
+
if not raw_lines:
|
|
97
|
+
raise BAI2Error("Empty BAI2 input.")
|
|
98
|
+
|
|
99
|
+
logical = []
|
|
100
|
+
for ln in raw_lines:
|
|
101
|
+
if ln.startswith("88,"):
|
|
102
|
+
if logical:
|
|
103
|
+
cont = ln[3:-1] if ln.endswith("/") else ln[3:]
|
|
104
|
+
prev = logical[-1][:-1] if logical[-1].endswith("/") else logical[-1]
|
|
105
|
+
logical[-1] = (prev + " " + cont.strip()) if prev.startswith("16,") else (prev + "," + cont)
|
|
106
|
+
else:
|
|
107
|
+
logical.append(ln)
|
|
108
|
+
|
|
109
|
+
accounts = []
|
|
110
|
+
cur_acct = None
|
|
111
|
+
group_date = None
|
|
112
|
+
file_date = None
|
|
113
|
+
group_cur = "USD"
|
|
114
|
+
|
|
115
|
+
for rec in logical:
|
|
116
|
+
parts = (rec[:-1] if rec.endswith("/") else rec).split(",")
|
|
117
|
+
rtype = parts[0].strip()
|
|
118
|
+
|
|
119
|
+
if rtype == "01" and len(parts) > 3:
|
|
120
|
+
file_date = _yymmdd(parts[3])
|
|
121
|
+
elif rtype == "02":
|
|
122
|
+
if len(parts) > 4:
|
|
123
|
+
group_date = _yymmdd(parts[4]) or file_date
|
|
124
|
+
if len(parts) > 6 and parts[6].strip():
|
|
125
|
+
group_cur = parts[6].strip().upper()
|
|
126
|
+
elif rtype == "03":
|
|
127
|
+
acct_num = parts[1].strip() if len(parts) > 1 else "Account"
|
|
128
|
+
cur = (parts[2].strip().upper() if len(parts) > 2 and parts[2].strip() else group_cur) or "USD"
|
|
129
|
+
open_bal = close_bal = None
|
|
130
|
+
i = 3
|
|
131
|
+
while i + 1 < len(parts):
|
|
132
|
+
tcode = parts[i].strip()
|
|
133
|
+
a = _amt(parts[i + 1], cur)
|
|
134
|
+
if tcode == "010" and a is not None:
|
|
135
|
+
open_bal = a
|
|
136
|
+
elif tcode == "015" and a is not None:
|
|
137
|
+
close_bal = a
|
|
138
|
+
step = 4
|
|
139
|
+
if i + 3 < len(parts):
|
|
140
|
+
ft = parts[i + 3].strip().upper()
|
|
141
|
+
if ft == "S":
|
|
142
|
+
step = 7
|
|
143
|
+
elif ft == "V":
|
|
144
|
+
step = 6
|
|
145
|
+
elif ft == "D":
|
|
146
|
+
try:
|
|
147
|
+
step = 5 + 2 * int(parts[i + 4].strip())
|
|
148
|
+
except (ValueError, IndexError):
|
|
149
|
+
step = 4
|
|
150
|
+
i += step
|
|
151
|
+
cur_acct = {
|
|
152
|
+
"account": acct_num,
|
|
153
|
+
"currency": cur,
|
|
154
|
+
"opening_balance": float(open_bal) if open_bal is not None else 0.0,
|
|
155
|
+
"closing_balance": float(close_bal) if close_bal is not None else None,
|
|
156
|
+
"_open_d": open_bal,
|
|
157
|
+
"_close_d": close_bal,
|
|
158
|
+
"transactions": [],
|
|
159
|
+
}
|
|
160
|
+
accounts.append(cur_acct)
|
|
161
|
+
elif rtype == "16":
|
|
162
|
+
if cur_acct is None:
|
|
163
|
+
cur_acct = {"account": "Account", "currency": group_cur, "opening_balance": 0.0,
|
|
164
|
+
"closing_balance": None, "_open_d": None, "_close_d": None, "transactions": []}
|
|
165
|
+
accounts.append(cur_acct)
|
|
166
|
+
tcode = parts[1].strip() if len(parts) > 1 else ""
|
|
167
|
+
raw_amt = _amt(parts[2] if len(parts) > 2 else "", cur_acct["currency"])
|
|
168
|
+
if raw_amt is None:
|
|
169
|
+
continue
|
|
170
|
+
sign = _sign(tcode)
|
|
171
|
+
amt = raw_amt if raw_amt < 0 else (raw_amt * sign)
|
|
172
|
+
ft = parts[3].strip().upper() if len(parts) > 3 else ""
|
|
173
|
+
idx = 4
|
|
174
|
+
if ft == "S":
|
|
175
|
+
idx = 7
|
|
176
|
+
elif ft == "V":
|
|
177
|
+
idx = 6
|
|
178
|
+
elif ft == "D":
|
|
179
|
+
try:
|
|
180
|
+
idx = 5 + 2 * int(parts[4].strip())
|
|
181
|
+
except (ValueError, IndexError):
|
|
182
|
+
idx = 4
|
|
183
|
+
bank_ref = parts[idx].strip() if len(parts) > idx else ""
|
|
184
|
+
cust_ref = parts[idx + 1].strip() if len(parts) > idx + 1 else ""
|
|
185
|
+
txt = ",".join(parts[idx + 2:]).strip() if len(parts) > idx + 2 else ""
|
|
186
|
+
label = _BAI2_CODES.get(tcode, (f"BAI2 {tcode}" if tcode else "Transaction", sign))[0]
|
|
187
|
+
d_obj = group_date or file_date
|
|
188
|
+
cur_acct["transactions"].append({
|
|
189
|
+
"date": d_obj.isoformat() if d_obj else "",
|
|
190
|
+
"type_code": tcode,
|
|
191
|
+
"type_label": label,
|
|
192
|
+
"amount": float(amt),
|
|
193
|
+
"_amt_d": amt,
|
|
194
|
+
"reference": cust_ref or bank_ref,
|
|
195
|
+
"bank_reference": bank_ref,
|
|
196
|
+
"description": f"{label}: {txt}" if txt else label,
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
if not accounts or not any(a["transactions"] for a in accounts):
|
|
200
|
+
raise BAI2Error("No BAI2 transactions found (expected 01/02/03/16 records).")
|
|
201
|
+
|
|
202
|
+
for a in accounts:
|
|
203
|
+
running = a["_open_d"] if a["_open_d"] is not None else Decimal("0.00")
|
|
204
|
+
tx_sum = Decimal("0.00")
|
|
205
|
+
for tx in a["transactions"]:
|
|
206
|
+
running += tx["_amt_d"]
|
|
207
|
+
tx_sum += tx["_amt_d"]
|
|
208
|
+
tx["balance"] = float(running)
|
|
209
|
+
del tx["_amt_d"]
|
|
210
|
+
if a["_open_d"] is not None and a["_close_d"] is not None:
|
|
211
|
+
a["balance_ok"] = ((a["_open_d"] + tx_sum) == a["_close_d"])
|
|
212
|
+
else:
|
|
213
|
+
a["balance_ok"] = None
|
|
214
|
+
del a["_open_d"], a["_close_d"]
|
|
215
|
+
return accounts
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def parse_file(path):
|
|
219
|
+
"""Parse a BAI2 file from disk."""
|
|
220
|
+
return parse(Path(path).read_bytes())
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def to_csv(accounts):
|
|
224
|
+
"""Return standard Excel-ready CSV text."""
|
|
225
|
+
buf = io.StringIO()
|
|
226
|
+
w = csv.writer(buf)
|
|
227
|
+
w.writerow(["Date", "Account", "Currency", "Type Code", "Description", "Reference", "Amount", "Balance"])
|
|
228
|
+
for a in accounts:
|
|
229
|
+
for tx in a["transactions"]:
|
|
230
|
+
w.writerow([tx["date"], a["account"], a["currency"], tx["type_code"],
|
|
231
|
+
tx["description"], tx["reference"], f"{tx['amount']:.2f}", f"{tx['balance']:.2f}"])
|
|
232
|
+
return buf.getvalue()
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
def to_xero_csv(accounts):
|
|
236
|
+
"""Return Xero-compatible bank statement CSV text."""
|
|
237
|
+
buf = io.StringIO()
|
|
238
|
+
w = csv.writer(buf)
|
|
239
|
+
w.writerow(["Date", "Amount", "Payee", "Description", "Reference"])
|
|
240
|
+
for a in accounts:
|
|
241
|
+
for tx in a["transactions"]:
|
|
242
|
+
w.writerow([tx["date"], f"{tx['amount']:.2f}", "", tx["description"], tx["reference"]])
|
|
243
|
+
return buf.getvalue()
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def to_qbo_csv(accounts):
|
|
247
|
+
"""Return QuickBooks Online / Sage 3-column CSV text."""
|
|
248
|
+
buf = io.StringIO()
|
|
249
|
+
w = csv.writer(buf)
|
|
250
|
+
w.writerow(["Date", "Description", "Amount"])
|
|
251
|
+
for a in accounts:
|
|
252
|
+
for tx in a["transactions"]:
|
|
253
|
+
w.writerow([tx["date"], tx["description"], f"{tx['amount']:.2f}"])
|
|
254
|
+
return buf.getvalue()
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def to_json(accounts):
|
|
258
|
+
"""Return formatted JSON string."""
|
|
259
|
+
return json.dumps(accounts, indent=2, ensure_ascii=False)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""CLI for bai2-to-excel."""
|
|
2
|
+
import argparse
|
|
3
|
+
import sys
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from . import parse_file, to_csv, to_xero_csv, to_qbo_csv, to_json
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def main(argv=None):
|
|
9
|
+
ap = argparse.ArgumentParser(prog="bai2-to-excel", description="Convert corporate BAI2 bank statements to CSV, Xero, QBO or JSON.")
|
|
10
|
+
ap.add_argument("input", help="Path to .bai / .bai2 / .txt file")
|
|
11
|
+
ap.add_argument("-o", "--output", help="Output file path (default: stdout)")
|
|
12
|
+
ap.add_argument("--format", choices=("csv", "xero", "qbo", "json"), default="csv")
|
|
13
|
+
args = ap.parse_args(argv)
|
|
14
|
+
accounts = parse_file(args.input)
|
|
15
|
+
out = {"csv": to_csv, "xero": to_xero_csv, "qbo": to_qbo_csv, "json": to_json}[args.format](accounts)
|
|
16
|
+
if args.output:
|
|
17
|
+
Path(args.output).write_text(out, encoding="utf-8")
|
|
18
|
+
else:
|
|
19
|
+
sys.stdout.write(out)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
if __name__ == "__main__":
|
|
23
|
+
main()
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: bai2-to-excel
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Parse corporate BAI2 bank statement files (Chase, BofA, Wells Fargo, Citi, HSBC) offline into Python dicts, CSV, Xero CSV, QuickBooks CSV or JSON with balance checks.
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Project-URL: Homepage, https://radarbg.com/instant/bai2-to-excel?ref=pypi-bai2
|
|
7
|
+
Project-URL: BAI2 to Excel online, https://radarbg.com/instant/bai2-to-excel?ref=pypi-bai2
|
|
8
|
+
Project-URL: PDF Bank Statement to Excel, https://radarbg.com/instant/bank-statement-pdf-to-excel?ref=pypi-bai2
|
|
9
|
+
Project-URL: CAMT.053 & MT940 Converter, https://radarbg.com/instant/camt053-to-excel?ref=pypi-bai2
|
|
10
|
+
Keywords: bai2,bai,bank-statement,treasury,accounting,csv,excel,xero,quickbooks,parser
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Topic :: Office/Business :: Financial :: Accounting
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
16
|
+
Requires-Python: >=3.8
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
# bai2-to-excel
|
|
22
|
+
|
|
23
|
+
Parse **corporate BAI2 bank statement files** (`.bai`, `.bai2`, `.txt`) offline into clean Python dicts, CSV, Xero CSV, QuickBooks/Sage CSV, or JSON — with automatic running balance and opening/closing balance verification.
|
|
24
|
+
|
|
25
|
+
Works with corporate treasury files from **JPMorgan Chase, Bank of America, Wells Fargo, Citibank, HSBC, Barclays**, and any bank following the Bank Administration Institute (BAI2) specification.
|
|
26
|
+
|
|
27
|
+
- **Pure Python, standard library only** (Python 3.8+, zero dependencies)
|
|
28
|
+
- **100% offline** — nothing uploaded, no network calls
|
|
29
|
+
- Handles records `01`, `02`, `03`, `16`, `88` (multi-line continuations), `49`, `98`, `99`
|
|
30
|
+
- Maps official BAI2 transaction type codes (`165` ACH Credit, `475` Check Paid, `195`/`495` Wire, `115` Lockbox, `698` Bank Fee, etc.) with automatic credit/debit sign detection
|
|
31
|
+
- Verifies `opening_balance + sum(transactions) == closing_balance` per account
|
|
32
|
+
|
|
33
|
+
> **Need Excel (`.xlsx`) without installing Python?**
|
|
34
|
+
> Convert BAI2 files directly in your browser (including multi-file `.zip` batches and Excel `.xlsx` with a Summary sheet):
|
|
35
|
+
> **[https://radarbg.com/instant/bai2-to-excel](https://radarbg.com/instant/bai2-to-excel?ref=pypi-bai2)**
|
|
36
|
+
|
|
37
|
+
## Install
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install bai2-to-excel
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Python usage
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
import bai2_to_excel as b2
|
|
47
|
+
|
|
48
|
+
accounts = b2.parse_file("statement.bai2")
|
|
49
|
+
for acct in accounts:
|
|
50
|
+
print(acct["account"], acct["currency"], acct["balance_ok"])
|
|
51
|
+
for tx in acct["transactions"]:
|
|
52
|
+
print(tx["date"], tx["amount"], tx["description"], tx["balance"])
|
|
53
|
+
|
|
54
|
+
# Export directly to CSV, Xero CSV, QuickBooks CSV, or JSON
|
|
55
|
+
csv_text = b2.to_csv(accounts)
|
|
56
|
+
xero_csv = b2.to_xero_csv(accounts)
|
|
57
|
+
json_text = b2.to_json(accounts)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Command line
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
# Convert a BAI2 file to standard CSV (Excel-ready)
|
|
64
|
+
bai2-to-excel statement.bai2 -o statement.csv
|
|
65
|
+
|
|
66
|
+
# Export for Xero, QuickBooks Online / Sage, or JSON
|
|
67
|
+
bai2-to-excel statement.bai2 --format xero -o statement-xero.csv
|
|
68
|
+
bai2-to-excel statement.bai2 --format qbo -o statement-qbo.csv
|
|
69
|
+
bai2-to-excel statement.bai2 --format json -o statement.json
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Related online accounting tools
|
|
73
|
+
|
|
74
|
+
- [BAI2 to Excel (.xlsx) online](https://radarbg.com/instant/bai2-to-excel?ref=pypi-bai2)
|
|
75
|
+
- [PDF Bank Statement to Excel / Xero / QBO / OFX](https://radarbg.com/instant/bank-statement-pdf-to-excel?ref=pypi-bai2)
|
|
76
|
+
- [CAMT.053 & MT940 Bank Statement Converter](https://radarbg.com/instant/camt053-to-excel?ref=pypi-bai2)
|
|
77
|
+
- [SEPA XML Generator & Checker](https://radarbg.com/instant/sepa-xml?ref=pypi-bai2)
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/bai2_to_excel/__init__.py
|
|
5
|
+
src/bai2_to_excel/__main__.py
|
|
6
|
+
src/bai2_to_excel.egg-info/PKG-INFO
|
|
7
|
+
src/bai2_to_excel.egg-info/SOURCES.txt
|
|
8
|
+
src/bai2_to_excel.egg-info/dependency_links.txt
|
|
9
|
+
src/bai2_to_excel.egg-info/entry_points.txt
|
|
10
|
+
src/bai2_to_excel.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
bai2_to_excel
|