-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.py
65 lines (49 loc) · 1.74 KB
/
core.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
import pandas as pd
from datetime import datetime
from pathlib import Path
def convert_to_hbank_format(path_to_xlsx, save_path):
'''
this function is designed to convert ABA statement xlsx
to HomeBank app csv import format.
'''
df = pd.read_excel(path_to_xlsx)
# format data
df.columns = df.iloc[1]
df = df.drop([0, 1])
df = df.drop(columns=['Ccy', 'Balance'])
# clean data
# remove , from thousand number
df['Money In'] = df['Money In'].str.replace(',', '')
df['Money Out'] = df['Money Out'].str.replace(',', '')
# change column object type to float for negative calculation
df[['Money Out', 'Money In']] = df[['Money Out', 'Money In']].astype(float)
# format date
df['Date'] = pd.to_datetime(df['Date'], format='%b %d, %Y')
df['Date'] = df['Date'].dt.strftime('%m/%d/%Y')
# fix nan value to 0
df = df.fillna(0)
# convert to negative number
df['Money Out'] *= -1
# sum for amount column
df['amount'] = df['Money In'] + df['Money Out']
new_name = {
'Date': 'date',
'Transaction Details': 'info',
}
df.rename(columns=new_name, inplace=True)
df.drop(columns=['Money Out', 'Money In'], inplace=True)
# add empty columns to match with homebank requirement
df['category'] = ''
df['tags'] = ''
df['payee'] = ''
df['memo'] = ''
df['payment mode'] = 4
# re-arrange the columns
cols = ['date', 'payment mode', 'info', 'payee',
'memo', 'amount', 'category', 'tags']
df = df[cols]
# export the file
now = datetime.now()
file_name = f"homebank_{now.strftime('%d%m%Y')}"
df.to_csv(f'{save_path}\{file_name}.csv', index=False)
print(f'file save to: {save_path}\{file_name}.csv')