-
Notifications
You must be signed in to change notification settings - Fork 1
/
Export-To-Homebank.py
98 lines (90 loc) · 4.88 KB
/
Export-To-Homebank.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
#Import libraries
import tkinter as tk
from tkinter import filedialog as fd
from datetime import datetime
import csv
import xlrd
#Export function
def export_file(selected_bank,filename):
#Take the date to hava timestamp as part of the file name
date = datetime.today().strftime('%Y%m%d')
line_count = 0
#For each bank the format of the input file is different
match selected_bank:
#Deutsche Postbank AG
case "Postbank":
with open(date + ' - Postbank_output.csv', mode='w', newline='') as output_file: #Open input file
#Create CSV file
output_writer = csv.writer(output_file, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL)
#Add header row to the export file
output_writer.writerow(['date','paymode','info','payee','memo','amount','category','tags'])
with open(filename) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=';')
#Go through each row of the input file
for row in csv_reader:
if line_count >7:
#For each row with content, add a new row to the export CSV file ordering the value as appropriate
if row[0]=="Kontostand": break
output_writer.writerow([f'{row[0]}', 6, '', '', f'{row[3]}'+' ('+f'{row[4]}'+')', f'{row[11]}'.replace('.','').replace(' €',''), '', ''])
line_count += 1
#Go throug ignoring header
else:
line_count += 1
#Pop-up message with information about the export done
tk.messagebox.showinfo(title="File processed", message='Fichero ' + filename + ' procesado como ' + selected_bank + ', '+ f'{line_count-9}' +' transacciones procesadas.')
#Deutsche ING-DiBa AG
case "ING DiBa":
with open(date + ' - INGDiBa_output.csv', mode='w', newline='') as output_file:
output_writer = csv.writer(output_file, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL)
output_writer.writerow(['date','paymode','info','payee','memo','amount','category','tags'])
with open(filename) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=';')
line_count = 0
for row in csv_reader:
if line_count >12:
output_writer.writerow([f'{row[0]}', 6, '', '', f'{row[2]}'+' ('+f'{row[5]}'+')', f'{row[7]}'.replace('.',''), '', ''])
line_count += 1
else:
line_count += 1
tk.messagebox.showinfo(title="File processed", message='Fichero ' + filename + ' procesado como ' + selected_bank + ', '+ f'{line_count-15}' +' transacciones procesadas.')
#Spanish ING Direct
case "ING Direct":
with open(date + ' - INGDirect_output.csv', mode='w', newline='') as output_file:
output_writer = csv.writer(output_file, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL)
output_writer.writerow(['date','paymode','info','payee','memo','amount','category','tags'])
with open(filename) as excel_file:
book = xlrd.open_workbook(filename)
sheet = book.sheet_by_index(0)
line_count = 0
for rx in range(sheet.nrows):
if line_count >5:
output_writer.writerow([sheet.row(rx)[0].value, 6, '', '', sheet.row(rx)[3].value.replace(';','.'), sheet.row(rx)[6].value, '', ''])
line_count += 1
else:
line_count += 1
tk.messagebox.showinfo(title="File processed", message='Fichero ' + filename + ' procesado como ' + selected_bank + ', '+ f'{line_count-5}' +' transacciones procesadas.')
window.quit()
#Creation of the main window
window = tk.Tk()
window.title("Export to Homebank")
window.geometry('640x480+50+50')
#Definition of the list of the banks for selection
banks = [
"Postbank",
"ING DiBa",
"ING Direct"
]
#Creation of the bank drop down selector
selected_bank = tk.StringVar()
selected_bank.set("Postbank")
bank = tk.OptionMenu( window , selected_bank , *banks )
bank.pack()
#Creation of the export button
export_button = tk.Button(window,text ="Export to Homebank",command=lambda: export_file(selected_bank.get(),filename))
export_button.pack()
#Creation of the exit button
exit_button = tk.Button(window,text ="Exit",command=lambda: window.quit())
exit_button.pack()
#Request to select the input file
filename = fd.askopenfilename(title="Select input file", filetypes =[('Excel Files', ('*.csv', '*.xlsx', '*.xls'))])
window.mainloop()