-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetf2cb.py
255 lines (229 loc) · 9.07 KB
/
etf2cb.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
# © flywire 2023, CC BY-SA
"""Get ETF Tax Contribution Splits from Annual Transaction Statement pdf."""
from dateutil import parser
import argparse
import csv
import datetime
import os
import re
import sys
import tabula
import tempfile
import time
def get_args():
parser = argparse.ArgumentParser(
description="""Extract ETF Annual Statement transaction component splits""",
epilog="""Filenames with .csv extension are processed directly without pdf extract""",
)
parser.add_argument("filename", help="Provide filename to extract")
parser.add_argument(
"area", nargs="?", help="Table area reference required for pdf file"
)
return parser.parse_args()
def get_tabular_area(filename):
"""return nested list of tabular table areas."""
area = []
with open(filename) as csv_file:
csv_list = list(csv.reader(csv_file))
for r in csv_list[1:]:
if r[0] != '':
e = r[0]
area.append([e])
ie = len(area)-1
if r[1] != '':
p = r[1]
area[ie].append([p])
ip = len(area[ie])-1
area[ie][ip].append([])
area[ie][ip][1].append([int(r[c]) for c in range(2, 6)])
return area
def csv_dictionary(filename):
"""return csv file as dictionary of dictionaries."""
with open(filename, "r") as f:
csv_list = list(csv.reader(f))
(_, *header), *data = csv_list
csv_dict = {}
for row in data:
key, *values = row
csv_dict[key] = {key: value for key, value in zip(header, values)}
return upper_dict_keys(csv_dict)
def upper_dict_keys(d):
new_dict = dict((k.upper(), v) for k, v in d.items())
return new_dict
def row_idx(list, str):
"""return row index list for lower case match of string and start of list."""
_ = []
for i, r in enumerate(list):
if r[0][: len(str)].lower() == str.lower():
_.append([i, r])
return _
def txn_split(key, account, amount, balance):
"""form transaction split list and balance"""
# Entity,Date,Description,Account,Deposit
# balance as cents
balance += int(amount.replace(".", ""))
row = key.copy()
row.append(account)
row.append(amount)
return row, balance
def part_a(acc_dict, ats_list, idx_b, key, balance, writer):
"""form transaction splits and balance - Part A"""
# Get amount and label columns
for i, r in enumerate(ats_list[4]):
if "amount" in r.lower():
c = i
if "label" in r.lower():
label_c = i
for r in ats_list[5:idx_b]:
if (
r[c]
and r[c] != "$0.00"
and re.compile(r"^\d{2}\D$").search(r[label_c]) != None
):
typ = acc_dict.get(r[label_c]).get("Type")
amount = r[c].replace(",", "")[1:]
if typ == "DB":
amount = "-" + amount
label = r[label_c]
account = acc_dict.get(label).get("Account")
if label == "18A":
ncg = amount
elif label == "18H":
amount = "-" + amount
c2d = lambda x: str(x)[:-2]+'.'+str(x)[-2:]
amount = c2d((int(amount.replace(".", "")) - int(ncg.replace(".", ""))))
account = "Income:Distribution:18H:GCTGrossUp"
if label != "20M":
row, balance = txn_split(key, account, amount, balance)
_ = writer.writerow(row)
return balance
def dictionary_key_fuzzy_match(dict, search):
"""return list of keys for search."""
return [k for k in [*dict] if k.lower() in search.lower()]
def part_b(acc_dict, ats_list, idx_b, key, balance, writer):
"""form transaction splits and balance - Part B"""
# Skip to non-tax items
skip = True
for i, r in enumerate(ats_list[idx_b:]):
if skip:
if "Non-assessable Amounts".upper() in r[0].upper():
skip = False
else:
for c in [1, 3]:
if (
r[c]
and r[c] != "$0.00"
and re.compile(r"^\$?[0-9,]*\.[0-9]{2}$").search(r[c]) != None
):
# Write non-zero amounts
# The tax-acc key (label) is a subset of r[c] ie ats_list[0]
label_list = dictionary_key_fuzzy_match(acc_dict, r[0])
if not len(label_list):
print(
"Label: ",
r[0],
"does not occur in Tax Accounts, processing failed.",
)
# raise exception
quit()
elif len(label_list) > 1:
# multiple matches so use exact match
label_list = acc_dict.get(r[0])
break
# raise exception
quit()
print(
r[0], "in multiple Tax Accounts labels, processing failed."
)
label = label_list[0]
typ = acc_dict.get(label.upper()).get("Type")
amount = r[c].replace(",", "")[1:]
if typ.lower() == "skip".lower():
break
if typ == "DB":
amount = "-" + amount
account = acc_dict.get(label).get("Account")
row, balance = txn_split(key, account, amount, balance)
_ = writer.writerow(row)
else:
pass
# print('Skip', c, r)
return balance
def close_txn(acc_dict, key, balance, writer):
if balance != 0:
account = acc_dict.get("Rounding".upper()).get("Account")
amount = str(-1 * balance / 100)
row, balance = txn_split(key, account, amount, balance)
_ = writer.writerow(row)
return
def main():
try:
args = get_args()
if args.filename[-4:].lower() == ".csv":
cdf = args.filename
elif args.area is None:
# parser.error('area argument required if no .csv filename extension')
raise TypeError("area argument required if no .csv filename extension")
else:
# Get table from pdf
area = get_tabular_area('tabula-area.csv')
# Catch exception when tabula_area not found
pdf = (
args.filename
if args.filename[-4:].lower() == ".pdf"
else args.filename + ".pdf"
)
tmpf = "tempfile.txt"
cdf = pdf[:-4] + ".csv"
# Get pdf area from tabula_area.py
idx_area = [i[0].lower() for i in area].index(args.area.lower())
for j in range(1, len(area[idx_area])):
tabula.convert_into(
pdf, tmpf, pages=area[idx_area][j][0], area=area[idx_area][j][1]
)
with open(tmpf, "r") as sfile:
mode = "w" if j == 1 else "a"
with open(cdf, mode) as tfile:
tfile.write(sfile.read())
os.remove(tmpf)
csvs = cdf[:-4] + "_split.csv"
acc_dict = csv_dictionary("tax-acc.csv")
# Catch exception when acc_dict not found
# Process annual tax statement
# To a list
with open(cdf, newline="") as ifile:
ats_list = list(csv.reader(ifile))
# Get statement structure
idx_b, _ = row_idx(ats_list, "Part B")[0]
# # Validate
# idx_item = row_idx(ats_list, "Item")
# if idx_item[0][0] != 4:
# print("Part A header - Invalid")
# if idx_item[1][0] != idx_b + 1:
# print("Part B header - Invalid")
# Row key
key = []
key.append(re.sub(r"\s+", '', ats_list[1][0])) # remove whitespace
key.append(parser.parse(ats_list[2][0]).date().strftime("%d/%m/%Y"))
key.append(ats_list[0][0])
# Do parts
with open(csvs, "w", newline="") as ofile:
writer = csv.writer(ofile, delimiter=",", quotechar='"')
balance = 0
balance = part_a(acc_dict, ats_list, idx_b, key, balance, writer)
balance = part_b(acc_dict, ats_list, idx_b, key, balance, writer)
_ = close_txn(acc_dict, key, balance, writer)
print("done.")
except IndexError:
print("Unknown statement structure, processing failed.")
# _ = [print(r[0]) for r in ats_list]
except AttributeError:
# print(r)
print(
"Line",
sys.exc_info()[-1].tb_lineno,
"- Item label does not occur in Tax Accounts, processing failed.",
)
# _ = [print(r[0]) for r in ats_list]
if __name__ == "__main__":
main()