From 435a2370496fb5fbf74844ab4755fc69796133e9 Mon Sep 17 00:00:00 2001 From: Shane Frischkorn Date: Mon, 10 Jan 2022 15:33:21 +1000 Subject: [PATCH] Code to convert hsbc qfx files to csv in the right format for money brilliant --- .vscode/launch.json | 16 ++++++++ Loan.qfx | 87 ++++++++++++++++++++++++++++++++++++++++++ converted_Loan.csv | 4 ++ converter.ipynb | 92 +++++++++++++++++++++++++++++++++++++++++++++ converter.py | 50 ++++++++++++++++++++++++ 5 files changed, 249 insertions(+) create mode 100644 .vscode/launch.json create mode 100644 Loan.qfx create mode 100644 converted_Loan.csv create mode 100644 converter.ipynb create mode 100644 converter.py diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..2b2502c --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Current File", + "type": "python", + "request": "launch", + "program": "${file}", + "console": "integratedTerminal", + "justMyCode": false + } + ] +} \ No newline at end of file diff --git a/Loan.qfx b/Loan.qfx new file mode 100644 index 0000000..78e0f61 --- /dev/null +++ b/Loan.qfx @@ -0,0 +1,87 @@ +OFXHEADER:100 +DATA:OFXSGML +VERSION:102 +SECURITY:NONE +ENCODING:USASCII +CHARSET:1252 +COMPRESSION:NONE +OLDFILEUID:NONE +NEWFILEUID:NONE + + + + + + 0 + INFO + + 20220110041351 + + ENG + + HSBC Bank Australia + 10624 + + 10624 + XXXXXXXXXXXXXXXXXXXXXXXXXX + + + + + 0 + + 0 + INFO + + + AUD + + 001610020 + 342101 576544258 + CREDITLINE + + + + + + CREDIT + 20220106 + 20220106 + 1173.71 + 202200600001 + TRANSFER + FROM 099-271041-090 AUD IB1305543 INTERNET BANKING + + + CREDIT + 20220106 + 20220106 + 628.81 + 202200600002 + TRANSFER + FROM 099-271041-090 AUD IB1004824 INTERNET BANKING + + + DEBIT + 20220104 + 20220104 + -399440.42 + 202200400001 + TRANSFER + PEXA217198219-B2895881 LOAN DD 1592204/099-271041 DBCU00064 BANKING TERMINAL (UBT + + + + -397637.90 + 20220110041351 + + + + 1802.52 + 20220110041351 + + + + + + diff --git a/converted_Loan.csv b/converted_Loan.csv new file mode 100644 index 0000000..c23949e --- /dev/null +++ b/converted_Loan.csv @@ -0,0 +1,4 @@ +Date,Original Description,Category,Amount,Account Name +06/01/2022,FROM 099-271041-090 AUD IB1305543 INTERNET BANKING,Uncategorised,1173.71,HSBC Everyday Global +06/01/2022,FROM 099-271041-090 AUD IB1004824 INTERNET BANKING,Uncategorised,628.81,HSBC Everyday Global +04/01/2022,PEXA217198219-B2895881 LOAN DD 1592204/099-271041 DBCU00064 BANKING TERMINAL (UBT,Uncategorised,-399440.42,HSBC Everyday Global diff --git a/converter.ipynb b/converter.ipynb new file mode 100644 index 0000000..6feaeae --- /dev/null +++ b/converter.ipynb @@ -0,0 +1,92 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "from ofxparse import OfxParser\n", + "import codecs" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "with codecs.open('qfx.qfx') as fileobj:\n", + " ofx = OfxParser.parse(fileobj)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'10624'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "account = ofx.account\n", + "\n", + "account.institution.fid" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Decimal('9570.86')" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "statement = account.statement\n", + "statement.available_balance" + ] + } + ], + "metadata": { + "interpreter": { + "hash": "8f1f8a1d9576cf73e2f91f0cdc8e61e6f80707111d98c220b357b8bb1eb61bce" + }, + "kernelspec": { + "display_name": "Python 3.8.12 64-bit ('hsbc': conda)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.12" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/converter.py b/converter.py new file mode 100644 index 0000000..83d2957 --- /dev/null +++ b/converter.py @@ -0,0 +1,50 @@ +#!/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) \ No newline at end of file