-
Notifications
You must be signed in to change notification settings - Fork 0
/
sheet_connect.py
47 lines (33 loc) · 1.31 KB
/
sheet_connect.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
"""This python file sets up a secured connection
to the script"""
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import df2gspread as d2g
from gspread_dataframe import set_with_dataframe
import os
key_file = os.getenv('key_file')
# defining the scope of the application
scope_app = ['https://www.googleapis.com/auth/drive',
'https://spreadsheets.google.com/feeds']
def get_connect_sheet():
cred = ServiceAccountCredentials.from_json_keyfile_name(
key_file, scope_app)
client = gspread.authorize(cred)
return client
def push_to_sheets(sheet, df):
'''This function takes in the sheet and the
dataframe'''
sheet.clear()
set_with_dataframe(worksheet=sheet, dataframe=df, include_index=False,
include_column_header=True, resize=True)
def append_new_data(df, sheet_name, sheet):
"""This function takes in the dataframe and the name of the sheet you wish
to append the data to """
values = df.values.tolist()
push_sheet = get_connect_sheet().open(sheet)
push_sheet.values_append(sheet_name, {'valueInputOption': 'USER_ENTERED'},
{'values': values})
if __name__ == '__main__':
client = get_connect_sheet()
sheet = client.open('test_sheet')
print(sheet.worksheets())