-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathynabifier.py
144 lines (116 loc) · 4.64 KB
/
ynabifier.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
#!/usr/bin/env python3
"""
Module Docstring.
"""
import argparse
import csv
import os
import sys
from datetime import datetime
from enum import Enum
class AccountType(Enum):
GIROKONTO = "Girokonto"
VISA = "VISA"
GIROKONTO_NEU = "Girokonto (Neu)"
def open_file(filename: str, offset: int) -> csv.DictReader:
"""Open a CSV file and return a DictReader."""
csvfile = open(filename, mode="r", encoding="utf-8")
dialect = csv.Sniffer().sniff(csvfile.read(1024))
csvfile.seek(0)
for _ in range(offset):
csvfile.readline()
return csv.DictReader(csvfile, dialect=dialect)
def convert_german_to_american(number_string):
"""Convert a number from German format to American format."""
# Entfernen der deutschen Tausendertrennzeichen
number_string = number_string.replace(".", "")
# Konvertieren des deutschen Dezimaltrennzeichens in das amerikanische
number_string = number_string.replace(",", ".")
try:
# Konvertieren in float und Zurückgeben des Werts
return float(number_string)
except ValueError:
# Falls die Eingabe nicht in eine Zahl umgewandelt werden kann
return None
def convert_date_format(date_str: str) -> str:
"""Convert date from DD.MM.YY to DD/MM/YY format."""
try:
date_obj = datetime.strptime(date_str, "%d.%m.%y")
return date_obj.strftime("%d/%m/%y")
except ValueError:
return date_str
def convert(filename: str, filetype: AccountType) -> None:
"""Convert the file given by filename according to the given type. Export to the same directory."""
if filetype == AccountType.GIROKONTO or filetype == AccountType.VISA:
reader = open_file(filename, 6)
elif filetype == AccountType.GIROKONTO_NEU:
reader = open_file(filename, 4)
basename_without_ext = os.path.splitext(
os.path.basename(os.path.abspath(filename))
)[0]
export_filename = f"{basename_without_ext}-ynab.csv"
export_filename = os.path.join(os.path.dirname(filename), export_filename)
with open(export_filename, mode="w", encoding="utf-8") as csvfile:
fieldnames = ["Date", "Payee", "Memo", "Amount"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for row in reader:
date = convert_date_format(row["Wertstellung"])
if filetype == AccountType.GIROKONTO:
writer.writerow(
{
"Date": date,
"Payee": row["Auftraggeber / Begünstigter"],
"Memo": row["Verwendungszweck"],
"Amount": row["Betrag (EUR)"],
}
)
elif filetype == AccountType.VISA:
writer.writerow(
{
"Date": date,
"Payee": row["Beschreibung"],
"Memo": row[""],
"Amount": row["Betrag (EUR)"],
}
)
elif filetype == AccountType.GIROKONTO_NEU:
value = convert_german_to_american(row["Betrag (€)"])
if value > 0:
writer.writerow(
{
"Date": date,
"Payee": row["Zahlungspflichtige*r"],
"Memo": row["Verwendungszweck"],
"Amount": value,
}
)
else:
writer.writerow(
{
"Date": date,
"Payee": row["Zahlungsempfänger*in"],
"Memo": row["Verwendungszweck"],
"Amount": value,
}
)
def main() -> None:
"""Convert .csv files into YNAB4 compatible .csv files."""
parser = argparse.ArgumentParser(
usage="%(prog)s [OPTION] [FILE]...",
description="Convert DKB CSV export files to YNAB4 compatible CSV files.",
)
parser.add_argument(
"-v", "--version", action="version", version=f"{parser.prog} 0.1.0"
)
parser.add_argument("account_type", type=AccountType, choices=list(AccountType))
parser.add_argument("file", help="Filename")
args = parser.parse_args()
try:
convert(args.file, args.account_type)
except FileNotFoundError as err:
print(f"{sys.argv[0]}: {args.file}: {err.strerror}", file=sys.stderr)
except IsADirectoryError as err:
print(f"{sys.argv[0]}: {args.file}: {err.strerror}", file=sys.stderr)
if __name__ == "__main__":
main()