Skip to content

Instantly share code, notes, and snippets.

@samueltc
Created Apr 6, 2022
Embed
What would you like to do?
#!/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