-
Notifications
You must be signed in to change notification settings - Fork 0
/
qfx2Beans.py
executable file
·59 lines (44 loc) · 1.42 KB
/
qfx2Beans.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3
"""First National Bank Transaction Converter
This script converts transactions from a First National Bank QFX export into
beancount language syntax (string output)
"""
import argparse
from ofxtools import OFXTree
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('input',
help="input qfx file")
parser.add_argument('--account',
help="name of posting account")
parser.add_argument('--flag',
default='*',
help="name of posting account")
return parser.parse_args()
def parse_ofx(file):
parser = OFXTree()
parser.parse(file)
return parser.convert()
bean_template = """\
{date} {flag} "{payee}"
{account} {amount} USD
memo: "{memo}"
"""
def get_bean_str(txn, account, flag):
date = txn.dtuser.strftime("%Y-%m-%d")
return bean_template.format(
account=account,
amount=txn.trnamt,
date=date,
flag=flag,
memo=txn.memo,
payee=txn.name)
def get_bal_str(ledgerbal, account):
date = ledgerbal.dtasof.strftime("%Y-%m-%d")
return "{} balance {} {} USD".format(date, account, ledgerbal.balamt)
args = parse_args()
ofx = parse_ofx(args.input)
stmt = ofx.statements[0]
for txn in reversed(stmt.transactions):
print(get_bean_str(txn, args.account, args.flag))
print(get_bal_str(stmt.ledgerbal, args.account))