Skip to content

Instantly share code, notes, and snippets.

@samueltc
Created April 6, 2022 09:59
Show Gist options
  • Save samueltc/1b8d14f6fb2ea71a8d0fad65321528dc to your computer and use it in GitHub Desktop.
Save samueltc/1b8d14f6fb2ea71a8d0fad65321528dc to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from ofxtools.Parser import OFXTree
from csv import DictWriter
from glob import glob
import sys
HEADER = ('date', 'amount', 'description', 'reference')
parser = OFXTree()
for filename in glob(sys.argv[1]):
print ('filename', filename)
parser.parse(open(filename, 'rb'))
ofx = parser.convert()
statements = ofx.statements
for statement in statements:
print ('>>>', statement.acctid)
filename = f'{statement.acctid}.csv'
csv = DictWriter(open(filename, 'w'), fieldnames=HEADER)
csv.writeheader()
for transaction in statement.transactions:
csv.writerow({
'date': transaction.dtposted.strftime("%Y-%m-%d"),
'amount': str(transaction.trnamt),
'description': f'{transaction.name} ({transaction.memo})',
'reference': transaction.fitid
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment