-
Notifications
You must be signed in to change notification settings - Fork 0
/
df_to_sheet.py
305 lines (258 loc) · 9.49 KB
/
df_to_sheet.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import os
import re
import ast
import pandas as pd
import numpy as np
import datetime
from typing import List, Union, Any, Tuple
import httplib2
import apiclient.discovery
from oauth2client.service_account import ServiceAccountCredentials
CREDENTIALS_FILE = os.getenv("CREDENTIALS_FILE")
gsheetId = os.getenv("gsheetId")
API_SERVICE_NAME = os.getenv("API_SERVICE_NAME")
API_VERSION = os.getenv("API_VERSION")
SCOPES = ast.literal_eval(os.environ.get("SCOPES"))
PERIOD_DAYS = int(os.environ.get("PERIOD_DAYS"))
TODAY = datetime.datetime.today().strftime("%d%m")
RAW_DATA = f"raw_tansactions{PERIOD_DAYS}_{TODAY}.csv"
credentials = ServiceAccountCredentials.from_json_keyfile_name(CREDENTIALS_FILE, SCOPES)
httpAuth = credentials.authorize(httplib2.Http())
service = apiclient.discovery.build(API_SERVICE_NAME, API_VERSION, http=httpAuth)
def export_data_to_sheet(df: pd.DataFrame, listName: str) -> None:
response_date = (
service.spreadsheets()
.values()
.update(
spreadsheetId=gsheetId,
valueInputOption="USER_ENTERED",
range=f"{listName}!a1",
body=dict(
majorDimension="ROWS", values=df.T.reset_index().T.values.tolist()
),
)
.execute()
)
def add_sheet(title: str, nrows: int, ncols: int) -> None:
results = (
service.spreadsheets()
.batchUpdate(
spreadsheetId=gsheetId,
body={
"requests": [
{
"addSheet": {
"properties": {
"title": title,
"gridProperties": {
"rowCount": nrows,
"columnCount": ncols,
},
}
}
}
]
},
)
.execute()
)
def clear_sheet(sheetName: str) -> None:
rangeAll = "{0}!A1:Z".format(sheetName)
resultClear = (
service.spreadsheets()
.values()
.clear(spreadsheetId=gsheetId, range=rangeAll, body={})
.execute()
)
def get_sheets_properties() -> dict:
spreadsheet = service.spreadsheets().get(spreadsheetId=gsheetId).execute()
sheetList = spreadsheet.get("sheets")
dct_sheets = {}
for sheet in sheetList:
dct_sheets[sheet["properties"]["title"]] = sheet["properties"]["sheetId"]
return dct_sheets
def clean_categories1(row: str) -> str:
return re.sub(r"[^a-zA-Z0-9_\s,]+", "", row).split(", ")[0]
def clean_categories2(row: Union[str, list]) -> Union[str, list]:
res = re.sub(r"[^a-zA-Z0-9_\s,]+", "", row).split(", ")[1:]
category2 = res if res else "null"
return category2
def clean_data_trans() -> pd.DataFrame:
df = pd.read_csv(
RAW_DATA, parse_dates=["date"], usecols=["amount", "category", "date"]
)
split_categories = lambda x: x.split(",")
df["category 1"] = df.category.apply(clean_categories1)
df["category 2"] = df.category.apply(clean_categories2)
df.drop("category", axis=1, inplace=True)
df = df.explode("category 2")
df["Income/Expense"] = df.amount.map(lambda x: "Income" if x > 0 else "Expense")
df.replace(np.nan, "", inplace=True)
global month_count
month_count = df.date.nunique()
global dct_cols
dct_cols = dict(zip(df.columns.tolist(), range(len(df.columns))))
df["date"] = df.date.apply(lambda x: x.strftime("%m/%Y")).astype(str)
return df
def make_pivotTbl(
source_sheet_id: int, target_sheet_id: int, end_row: int, end_col: int
) -> None:
requests = []
requests.append(
{
"updateCells": {
"rows": {
"values": [
{
"pivotTable": {
"source": {
"sheetId": source_sheet_id,
"startRowIndex": 0,
"startColumnIndex": 0,
"endRowIndex": end_row,
"endColumnIndex": end_col,
},
"rows": [
{
"sourceColumnOffset": dct_cols[
"Income/Expense"
],
"showTotals": True,
"sortOrder": "DESCENDING",
},
{
"sourceColumnOffset": dct_cols["category 1"],
"showTotals": True,
"sortOrder": "ASCENDING",
},
{
"sourceColumnOffset": dct_cols["category 2"],
"showTotals": True,
"sortOrder": "ASCENDING",
},
],
"columns": [
{
"sourceColumnOffset": dct_cols["date"],
"sortOrder": "ASCENDING",
"showTotals": True,
}
],
"values": [
{
"summarizeFunction": "SUM",
"sourceColumnOffset": dct_cols["amount"],
}
],
"valueLayout": "HORIZONTAL",
}
}
]
},
"start": {"sheetId": target_sheet_id, "rowIndex": 0, "columnIndex": 0},
"fields": "pivotTable",
}
}
)
# setting autoresize columns
requests.append(
{
"autoResizeDimensions": {
"dimensions": {
"sheetId": target_sheet_id,
"dimension": "COLUMNS",
"startIndex": 0,
"endIndex": 4,
}
}
}
)
# setting number formart
requests.append(
{
"repeatCell": {
"range": {
"sheetId": target_sheet_id,
"startRowIndex": 2,
"startColumnIndex": 3,
"endRowIndex": 26,
"endColumnIndex": month_count + 4,
},
"cell": {
"userEnteredFormat": {
"numberFormat": {"type": "CURRENCY", "pattern": '""#,###.##'}
}
},
"fields": "userEnteredFormat.numberFormat",
}
}
)
# bold total
requests.append(
{
"repeatCell": {
"range": {
"sheetId": target_sheet_id,
"startColumnIndex": month_count + 3,
"endColumnIndex": month_count + 4,
},
"cell": {"userEnteredFormat": {"textFormat": {"bold": True}}},
"fields": "userEnteredFormat.textFormat.bold",
}
}
)
body = {"requests": requests}
response = (
service.spreadsheets().batchUpdate(spreadsheetId=gsheetId, body=body).execute()
)
def update_properties(request: Tuple[dict, ...]) -> None:
body = {"requests": request}
response = (
service.spreadsheets().batchUpdate(spreadsheetId=gsheetId, body=body).execute()
)
def del_sheet(sheet_id):
result = (
service.spreadsheets()
.batchUpdate(
spreadsheetId=gsheetId,
body={"requests": [{"deleteSheet": {"sheetId": sheet_id}}]},
)
.execute()
)
if __name__ == "__main__":
raw_data = pd.read_csv(RAW_DATA)
raw_data.replace(np.nan, "", inplace=True)
export_data_to_sheet(raw_data, "raw_data")
df = clean_data_trans()
# dim clean_data
nrows, ncols = df.shape
# dim of cashflow_statement nr x nc
nr = df.groupby(["category 1", "category 2"]).size().shape[0] * 3
nc = len(df) + month_count
add_sheet("cleaned_data", nrows, ncols)
add_sheet("cashflow_statement", nr, nc)
export_data_to_sheet(df, "cleaned_data")
dct_sheets = get_sheets_properties()
source_sheet_id = dct_sheets["cleaned_data"]
target_sheet_id = dct_sheets["cashflow_statement"]
date_format = [
{
"repeatCell": {
"range": {
"sheetId": source_sheet_id,
"startRowIndex": 1,
"startColumnIndex": 1,
"endColumnIndex": 2,
},
"cell": {
"userEnteredFormat": {
"numberFormat": {"type": "DATE", "pattern": "mmm yyyy"}
}
},
"fields": "userEnteredFormat.numberFormat",
}
}
]
# update date format in cleaned_data
update_properties(date_format)
make_pivotTbl(source_sheet_id, target_sheet_id, nrows, ncols)