-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsheets-import.py
More file actions
64 lines (51 loc) · 2.31 KB
/
sheets-import.py
File metadata and controls
64 lines (51 loc) · 2.31 KB
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
"""
This will take an Excel or OpenOffice spreadsheet file and import it into your Google Drive
auto-magically via the Google Sheets API. Maintains format & layout which is rad, eh? Eh?!
"""
from __future__ import print_function
import datetime
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from httplib2 import Http
# noinspection SpellCheckingInspection
from oauth2client import file as oafile, client, tools
# If modifying these scopes, delete the file 'token.json'
SCOPES = 'https://www.googleapis.com/auth/drive'
# Update report source accordingly
# REPORT_MASTER = 'data/master_report_sample.csv'
REPORT_MASTER = 'data/master_report_sample.ods'
# Gussy up these curtains a bit
timeStamp = datetime.datetime.now()
formattedTimeStamp = timeStamp.strftime("%Y-%m-%d (%A)")
formattedReportName = u'Auto-Imported Report: {0}'.format(formattedTimeStamp)
def main():
# The file token.json stores the user's access and refresh tokens, and is
# auto-generated when the authorization flow completes for the first time.
store = oafile.Storage('token.json')
tokens = store.get()
if not tokens or tokens.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
tokens = tools.run_flow(flow, store)
service = build('drive', 'v3', http=tokens.authorize(Http()))
# Call the Drive v3 API
results = service.files().list(pageSize=10, fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])
if not items:
print('\nDirectory is empty...')
else:
print('\nExisting Files:')
for item in items:
print(u'{0} ({1})'.format(item['name'], item['id']))
file_metadata = {
'name': formattedReportName,
'mimeType': 'application/vnd.google-apps.spreadsheet'
}
media = MediaFileUpload(REPORT_MASTER,
mimetype='application/vnd.oasis.opendocument.spreadsheet',
resumable=True)
file = service.files().create(body=file_metadata,
media_body=media,
fields='id').execute()
print('\n\nImport complete!\nImported File ID: %s' % file.get('id'))
if __name__ == '__main__':
main()