-
Notifications
You must be signed in to change notification settings - Fork 1
/
csv_to_sheets.py
166 lines (132 loc) · 4.41 KB
/
csv_to_sheets.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
from __future__ import print_function
import os.path
import glob
import sys
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
global service
global spreadsheetId
def get_sheet_id(spreadsheet, title):
# gets the sheet_id for given name
for _sheet in spreadsheet['sheets']:
if _sheet['properties']['title'] == title:
return _sheet['properties']['sheetId']
def delete_sheet(sheet_id):
# deletes a sheet with the given name
body = {
'requests': [{
'deleteSheet': {
'sheetId': sheet_id,
}
}]
}
try:
service.spreadsheets().batchUpdate(
spreadsheetId=spreadsheetId,
body=body).execute()
except HttpError as err:
print(err)
def add_sheet(title):
# add a new sheet with the given name
body = {
'requests': [{
'addSheet': {
'properties': {
'title': title,
}
}
}]
}
service.spreadsheets().batchUpdate(
spreadsheetId=spreadsheetId,
body=body
).execute()
def upload_csv(csv_path, sheet_id):
with open(csv_path, 'r') as csv_file:
csvContents = csv_file.read()
body = {
'requests': [{
'pasteData': {
"coordinate": {
"sheetId": sheet_id,
"rowIndex": "0",
"columnIndex": "0",
},
"data": csvContents,
"type": 'PASTE_NORMAL',
"delimiter": ',',
}
}]
}
response = service.spreadsheets().batchUpdate(
spreadsheetId=spreadsheetId,
body=body
).execute()
return response
def list_files(folder):
return glob.glob(folder + "/*.csv")
def main(argv):
global folder
global spreadsheetId
if len(argv) < 2:
print("Usage: csv_to_sheets.py <folder> <spreadsheetId>")
sys.exit(2)
folder = argv[0]
spreadsheetId = argv[1]
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
try:
global service
service = build('sheets', 'v4', credentials=creds)
spreadsheet = service.spreadsheets().get(
spreadsheetId=spreadsheetId
).execute()
# retrieve the list of CSV files
files = list_files(
folder
)
if len(files) == 0:
print("Folder does not include any CSV files.")
sys.exit(2)
for path in files:
# get the sheet name from the file name
sheet_name = os.path.splitext(os.path.basename(path))[0]
print("Preparing Sheets: " + sheet_name)
sheet_id = get_sheet_id(spreadsheet, sheet_name)
if sheet_id is not None:
delete_sheet(sheet_id)
add_sheet(title=sheet_name)
spreadsheet = service.spreadsheets().get(
spreadsheetId=spreadsheetId
).execute()
for path in files:
# get the sheet name from the file name
sheet_name = os.path.splitext(os.path.basename(path))[0]
print("Uploading: " + sheet_name)
sheet_id = get_sheet_id(spreadsheet, sheet_name)
if sheet_id is not None:
upload_csv(path, sheet_id)
except HttpError as err:
print(err)
if __name__ == "__main__":
main(sys.argv[1:])