Skip to content

Instantly share code, notes, and snippets.

@sidharthshah
Last active April 14, 2018 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sidharthshah/a908f646fc115ba551e7b87a556d9071 to your computer and use it in GitHub Desktop.
Save sidharthshah/a908f646fc115ba551e7b87a556d9071 to your computer and use it in GitHub Desktop.
Python-Google-Sheet-Example
# 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