forked from sochotnicky/bank2qif
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbank2qif.py
executable file
·281 lines (233 loc) · 10.3 KB
/
bank2qif.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# mbank2qif.py - mBank CSV output convertor to QIF (quicken) file
# Copyright (C) 2011 Stanislav Ochotnicky <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses.
import codecs
import csv
import argparse
import re
from datetime import date
class BadRecordTypeException(Exception):
def __init__(self, line_no):
self._line_no = line_no
def __str__(self):
return "Bad record type on line: %s" % (self._line_no,)
def unicode_csv_reader(unicode_csv_data, dialect=csv.excel, **kwargs):
# csv.py doesn't do Unicode; encode temporarily as UTF-8:
csv_reader = csv.reader(utf_8_encoder(unicode_csv_data),
dialect=dialect, **kwargs)
for row in csv_reader:
# decode UTF-8 back to Unicode, cell by cell:
yield [unicode(cell, 'utf-8') for cell in row]
def utf_8_encoder(unicode_csv_data):
for line in unicode_csv_data:
yield line.encode('utf-8')
def normalize_field(text):
ret = re.sub(BankImporter.multispace_re, " ", text)
return ret.replace('"', '').replace("'", "").strip()
def normalize_num(text):
return text.replace(',', '.').replace(' ', '').strip()
class TransactionData(object):
"""Simple class to hold information about a transaction"""
def __init__(self, date, amount, destination=None, message=None, ident=None):
self.date = date
self.amount = amount
self.destination = destination
self.message = message
self.ident = ident
class BankImporter(object):
"""Base class for statement import"""
multispace_re = re.compile('\s+')
def __init__(self, infile):
self.infile = open(infile, 'r')
self.reader = codecs.getreader("utf-8")
self.inputreader = self.reader(self.infile)
self.transactions = []
def bank_import(self):
"""Run import from file and return list of transactions
bank_import() -> [TransactionData, ...]"""
pass
class MBankImport(BankImporter):
source = "mbank"
def __init__(self, infile):
BankImporter.__init__(self, infile)
self.reader = codecs.getreader("cp1250")
self.inputreader = self.reader(self.infile)
def bank_import(self):
items = False
for row in unicode_csv_reader(self.inputreader.readlines(),
delimiter=';'):
if not items and len(row) > 0 and ( \
row[0] == u"#Datum uskutečnění transakce" or \
row[0] == u"#Dátum uskutočnenia transakcie" \
):
items = True
continue
if items:
if len(row) == 0:
break
d, m, y = row[1].split('-')
tdate = date(int(y), int(m), int(d))
tamount = float(normalize_num(row[9]))
trans_type = normalize_field(row[2])
trans_desc = normalize_field(row[3])
trans_target = normalize_field(row[4])
trans_acc = normalize_field(row[5])
tmessage = u"%s %s %s %s" % (trans_type,
trans_desc,
trans_target,
trans_acc)
self.transactions.append(TransactionData(tdate,
tamount,
message=tmessage))
return self.transactions
class UnicreditImport(BankImporter):
source = "unicredit"
def bank_import(self):
items = False
for row in unicode_csv_reader(self.inputreader.readlines(),
delimiter=';'):
if not items and len(row) > 0 and row[0] == u"Účet":
items = True
continue
if items:
if len(row) == 0:
break
y, m, d = row[3].split('-')
tdate = date(int(y), int(m), int(d))
tamount = float(normalize_num(row[1]))
bank_no = row[5]
bank_name = "%s %s" % (normalize_field(row[6]),
normalize_field(row[7]))
bank_name = bank_name.strip()
account_number = normalize_field(row[8])
account_name = normalize_field(row[9])
tdest = None
if account_number != "":
tdest = "%s: %s/%s %s" % (bank_name,
account_number,
bank_no,
account_name)
t_type = row[13].strip()
if t_type == u"PLATBA PLATEBNÍ KARTOU" and \
tdest == None:
# when paid by card the description of place is in
# last of "transaction details"
for i in reversed(range(13,19)):
if normalize_field(row[i]) != "":
tdest = "%s" % (normalize_field(row[i]))
break
tmessage = "%s %s %s %s %s %s" % (row[13],
row[14],
row[15],
row[16],
row[17],
row[18])
tmessage = normalize_field(tmessage)
self.transactions.append(TransactionData(tdate,
tamount,
message=tmessage,
destination=tdest))
return self.transactions
class FioImport(BankImporter):
source = "fio"
def __init__(self, infile):
BankImporter.__init__(self, infile)
self.reader = codecs.getreader("cp1250")
self.inputreader = self.reader(self.infile)
def bank_import(self):
# For GPC format documentation see here:
# http://www.fio.cz/docs/cz/struktura-gpc.pdf
line_no = 1
# The first line contains info about account
line = self.inputreader.readline()
record_type = line[0:3]
if record_type != '074':
raise BadRecordTypeException(line_no)
# The following lines contain transactions
line = self.inputreader.readline()
while line:
line_no += 1
# Record type must be '075' (transaction)
record_type = line[0:3]
if record_type != '075':
raise BadRecordTypeException(line_no)
# Transaction type; 1 = debet, 2 = credit, 4 = storno of debet,
# 5 = storno of credit
ttype = int(line[60])
# Transaction amount
tamount = float(line[48:60]) / 100.0
if ttype in (1, 5):
tamount = -tamount
# Transaction date (DDMMYY)
d = int(line[122:124])
m = int(line[124:126])
y = 2000 + int(line[126:128])
tdate = date(y, m, d)
# Destination account
tbankcode = line[71:81].lstrip('0')[0:4]
tdestacc = line[19:35].lstrip('0')
tdest = tdestacc and ('%s/%s' % (tdestacc, tbankcode)) or None
# Message
tmessage = line[97:117].strip()
# Transaction identifier
tident = line[35:48]
# Append transaction to the list
self.transactions.append(TransactionData(tdate, tamount,
destination=tdest, message=tmessage, ident=tident))
# Read next line
line = self.inputreader.readline()
return self.transactions
def write_qif(outfile, transactions):
with open(outfile, 'w') as output:
writer = codecs.getwriter("utf-8")
outputwriter = writer(output)
outputwriter.write("!Type:Bank\n")
for transaction in transactions:
d, m, y = (transaction.date.day,
transaction.date.month,
transaction.date.year)
outputwriter.write(u"D%s/%s/%s\n" % (m, d, y))
outputwriter.write(u"T%s\n" % transaction.amount)
if transaction.ident:
outputwriter.write(u"#%s\n" % transaction.ident)
if transaction.message:
outputwriter.write(u"M%s\n" % transaction.message)
if transaction.destination:
outputwriter.write(u"P%s\n" % transaction.destination)
outputwriter.write(u'^\n')
if __name__ == "__main__":
importers = [MBankImport, UnicreditImport, FioImport]
sources = []
for importer in importers:
sources.append(importer.source)
parser = argparse.ArgumentParser(description='Bank statement to QIF file converter')
parser.add_argument('-i', '--input',
help='input file to process [default:stdin]',
default='/dev/stdin')
parser.add_argument('-o', '--output',
help='output file [default:stdout]',
default='/dev/stdout')
parser.add_argument('-t', '--type',
help='Type of input file [default:mbank]'
' Possible values: %s' % ', '.join(sources),
default='mbank')
args = parser.parse_args()
transactions = []
for importer in importers:
if args.type == importer.source:
inst = importer(args.input)
transactions = inst.bank_import()
write_qif(args.output, transactions)