1from __future__ import print_function
2import os.path
3from googleapiclient.discovery import build
4from google_auth_oauthlib.flow import InstalledAppFlow
5from google.auth.transport.requests import Request
6from google.oauth2.credentials import Credentials
7
8# If modifying these scopes, delete the file token.json.
9SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
10
11# The ID and range of a sample spreadsheet.
12SAMPLE_SPREADSHEET_ID = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'
13SAMPLE_RANGE_NAME = 'Class Data!A2:E'
14
15def main():
16 """Shows basic usage of the Sheets API.
17 Prints values from a sample spreadsheet.
18 """
19 creds = None
20 # The file token.json stores the user's access and refresh tokens, and is
21 # created automatically when the authorization flow completes for the first
22 # time.
23 if os.path.exists('token.json'):
24 creds = Credentials.from_authorized_user_file('token.json', SCOPES)
25 # If there are no (valid) credentials available, let the user log in.
26 if not creds or not creds.valid:
27 if creds and creds.expired and creds.refresh_token:
28 creds.refresh(Request())
29 else:
30 flow = InstalledAppFlow.from_client_secrets_file(
31 'credentials.json', SCOPES)
32 creds = flow.run_local_server(port=0)
33 # Save the credentials for the next run
34 with open('token.json', 'w') as token:
35 token.write(creds.to_json())
36
37 service = build('sheets', 'v4', credentials=creds)
38
39 # Call the Sheets API
40 sheet = service.spreadsheets()
41 result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
42 range=SAMPLE_RANGE_NAME).execute()
43 values = result.get('values', [])
44
45 if not values:
46 print('No data found.')
47 else:
48 print('Name, Major:')
49 for row in values:
50 # Print columns A and E, which correspond to indices 0 and 4.
51 print('%s, %s' % (row[0], row[4]))
52
53if __name__ == '__main__':
54 main()
1from oauth2client.service_account import ServiceAccountCredentials
2import gspread
3
4scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive',
5 'https://www.googleapis.com/auth/drive.file', 'https://www.googleapis.com/auth/spreadsheets']
6
7#Generate a json file by using service account auth in google developer console
8'''
9Link: https://console.developers.google.com/
101) Enable API Access for a Project if you haven’t done it yet.
112) Go to “APIs & Services > Credentials” and choose “Create credentials > Service account key”.
123) Fill out the form
134) Click “Create” and “Done”.
145) Press “Manage service accounts” above Service Accounts.
156) Press on ⋮ near recently created service account and select “Manage keys” and then click on “ADD KEY > Create new key”.
167) Select JSON key type and press “Create”.
178) Go to the google sheet and share the sheet with the email from service accounts.
18'''
19creds = ServiceAccountCredentials.from_json_keyfile_name('mod.json', scope)
20client = gspread.authorize(creds)
21
22sheet = client.open_by_url("#Paste yout google sheet url here").sheet1
23
24data = sheet.get_all_records()
25
26sheet.update_cell(1, 1, "You made it") #Write this message in first row and first column
27
28print(data)