Skip to content

Instantly share code, notes, and snippets.

@im-alexandre
Forked from whistler/ofx2csv.py
Created January 2, 2020 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save im-alexandre/875c7cb0c3d51cb46faeb72699c7a96b to your computer and use it in GitHub Desktop.
Save im-alexandre/875c7cb0c3d51cb46faeb72699c7a96b to your computer and use it in GitHub Desktop.
Convert QFX/OFX to CSV
from csv import DictWriter
from glob import glob
from ofxparse import OfxParser
DATE_FORMAT = "%m/%d/%Y"
def write_csv(statement, out_file):
print "Writing: " + out_file
fields = ['date', 'payee', 'debit', 'credit', 'balance']
with open(out_file, 'w') as f:
writer = DictWriter(f, fieldnames=fields)
for line in statement:
writer.writerow(line)
def get_statement_from_qfx(qfx):
balance = qfx.account.statement.balance
statement = []
for transaction in qfx.account.statement.transactions:
credit = ""
debit = ""
balance = balance + transaction.amount
if transaction.type == 'credit':
credit = transaction.amount
elif transaction.type == 'debit':
debit = -transaction.amount
else:
raise Error("Unknown transaction type")
line = {
'date': transaction.date.strftime(DATE_FORMAT),
'payee': transaction.payee,
'debit': debit,
'credit': credit,
'balance': balance
}
statement.append(line)
return statement
files = glob("*.qfx")
for qfx_file in files:
qfx = OfxParser.parse(file(qfx_file))
statement = get_statement_from_qfx(qfx)
out_file = "converted_" + qfx_file.replace(".qfx",".csv")
write_csv(statement, out_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment