Last active
April 14, 2018 11:21
-
-
Save sidharthshah/a908f646fc115ba551e7b87a556d9071 to your computer and use it in GitHub Desktop.
Python-Google-Sheet-Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is example from https://developers.google.com/sheets/api/quickstart/python | |
""" | |
Shows basic usage of the Sheets API. Prints values from a Google Spreadsheet. | |
""" | |
from __future__ import print_function | |
from apiclient.discovery import build | |
from httplib2 import Http | |
from oauth2client import file, client, tools | |
# Setup the Sheets API | |
SCOPES = 'https://www.googleapis.com/auth/spreadsheets.readonly' | |
store = file.Storage('credentials.json') | |
creds = store.get() | |
if not creds or creds.invalid: | |
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES) | |
creds = tools.run_flow(flow, store) | |
service = build('sheets', 'v4', http=creds.authorize(Http())) | |
# Call the Sheets API | |
SPREADSHEET_ID = '1mFX6OTrKH5WMTRZfgjCnb7CltkylCK3HWNWq3sXD_Ro' | |
RANGE_NAME = 'Class Data!A2:E' | |
result = service.spreadsheets().values().get(spreadsheetId=SPREADSHEET_ID, | |
range=RANGE_NAME).execute() | |
values = result.get('values', []) | |
if not values: | |
print('No data found.') | |
else: | |
print('Name, Major:') | |
for row in values: | |
# Print columns A and E, which correspond to indices 0 and 4. | |
print('%s, %s' % (row[0], row[4])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment