Last active
July 16, 2017 03:28
-
-
Save xaethos/93a2b6539cc340a69f28 to your computer and use it in GitHub Desktop.
UOB activity spreadsheet to CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import xlrd | |
import csv | |
from datetime import datetime | |
skip = [ | |
'Funds Trf - FAST', | |
'Inward CR - GIRO', | |
'Inward Credit-FAST', | |
'Inward Debit-GIRO', | |
'Inward DR - GIRO', | |
'Misc CR-Debit Card', | |
'Misc DR-Debit Card', | |
'NETS Debit-Consumer', | |
] | |
keep = [ | |
'Bill Payment', | |
'Cash Withdrawal-ATM', | |
'Cash Withdrawal-SATM', | |
'CashCard Reval-ATM', | |
'Cheque Deposit', | |
'Funds Transfer', | |
'Interest Credit', | |
] | |
def parse_date(row): | |
return datetime.strptime(row[0].value, '%d %b %Y').strftime('%Y-%m-%d') | |
def build_description(descriptors): | |
if descriptors[0] in skip: | |
desc = descriptors[1:] | |
else: | |
desc = descriptors[:] | |
if descriptors[0] not in keep: | |
print("-*-*- Unhandled transaction type:", descriptors[0]) | |
return ' '.join(desc) | |
class TransactionRow: | |
def __init__(self, row): | |
self.date = parse_date(row) | |
descriptors = row[1].value.split("\n") | |
self.transfer_type = descriptors[0] | |
self.description = build_description(descriptors) | |
self.withdrawal = row[2].value | |
self.deposit = row[3].value | |
self.balance = row[4].value | |
filenames = sys.argv[1:] | |
for infile in filenames: | |
print() | |
outfile = infile[0:-4] + '.csv' | |
print(infile, '=>', outfile) | |
book = xlrd.open_workbook(infile) | |
print("The number of worksheets is", book.nsheets) | |
print("Worksheet name(s):", book.sheet_names()) | |
sh = book.sheet_by_index(0) | |
print(sh.name, sh.nrows, sh.ncols) | |
with open(outfile, 'w', newline='') as out: | |
writer = csv.writer(out) | |
writer.writerow(['Date', 'Payee', 'Memo', 'Outflow', 'Inflow']) | |
for rx in range(9, sh.nrows): | |
row = TransactionRow(sh.row(rx)) | |
writer.writerow([ | |
row.date, | |
row.description, | |
'', | |
row.withdrawal, | |
row.deposit, | |
]) | |
print([ | |
row.date, | |
row.transfer_type, | |
row.description, | |
row.withdrawal, | |
row.deposit, | |
row.balance, | |
]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment