50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from csv import DictWriter
|
|
from glob import glob
|
|
from ofxparse import OfxParser
|
|
|
|
DATE_FORMAT = "%d/%m/%Y"
|
|
|
|
def write_csv(statement, out_file):
|
|
print("Writing: " + out_file)
|
|
fields = ['date', 'memo', 'category', 'amount', 'name']
|
|
with open(out_file, 'w') as f:
|
|
f.write("Date,Original Description,Category,Amount,Account Name")
|
|
f.write("\r\n")
|
|
writer = DictWriter(f, fieldnames=fields)
|
|
for line in statement:
|
|
writer.writerow(line)
|
|
|
|
|
|
def get_statement_from_qfx(qfx):
|
|
balance = qfx.account.statement.balance
|
|
|
|
statement = []
|
|
credit_transactions = ['credit', 'dep', 'int']
|
|
debit_transactions = ['debit', 'atm', 'pos', 'xfer', 'check']
|
|
for transaction in qfx.account.statement.transactions:
|
|
amount = ""
|
|
balance = balance + transaction.amount
|
|
|
|
if transaction.payee.startswith("PENDING:"):
|
|
continue
|
|
|
|
line = {
|
|
'date': transaction.date.strftime(DATE_FORMAT),
|
|
'memo' : transaction.memo,
|
|
'category': 'Uncategorised',
|
|
'amount': transaction.amount,
|
|
'name': 'HSBC Everyday Global'
|
|
}
|
|
statement.append(line)
|
|
return statement
|
|
|
|
|
|
files = glob("*.qfx")
|
|
for qfx_file in files:
|
|
qfx = OfxParser.parse(open(qfx_file, encoding="latin-1"), fail_fast=False)
|
|
statement = get_statement_from_qfx(qfx)
|
|
out_file = "converted_" + qfx_file.replace(".qfx",".csv")
|
|
write_csv(statement, out_file) |