Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python google sheets api packages

  pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
Comment

google sheet api python

from __future__ import print_function
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials

# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']

# The ID and range of a sample spreadsheet.
SAMPLE_SPREADSHEET_ID = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'
SAMPLE_RANGE_NAME = 'Class Data!A2:E'

def main():
    """Shows basic usage of the Sheets API.
    Prints values from a sample spreadsheet.
    """
    creds = None
    # The file token.json stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.json'):
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.json', 'w') as token:
            token.write(creds.to_json())

    service = build('sheets', 'v4', credentials=creds)

    # Call the Sheets API
    sheet = service.spreadsheets()
    result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
                                range=SAMPLE_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]))

if __name__ == '__main__':
    main()
Comment

PREVIOUS NEXT
Code Example
Python :: create python dataframe 
Python :: print function args python 
Python :: create new dataframe from existing data frame python 
Python :: python decision tree classifier 
Python :: create empty numpy array 
Python :: groupby get last group 
Python :: Add two numbers as a linked list 
Python :: check if key exists in sesison python 
Python :: how to set global variable in python function 
Python :: selenium python tkinter 
Python :: binary search recursive python 
Python :: Python Regex Backslash “” 
Python :: how to get input from user in pyqt5 
Python :: concatenate list in python 
Python :: Access the Response Methods and Attributes in python Show HTTP header 
Python :: function in python 
Python :: insert into 2d array 
Python :: to divide or not to divide solution 
Python :: python logging variables extra 
Python :: django queryset and operator 
Python :: long in python 
Python :: how to remove last 2 rows in a dataframe 
Python :: python data type conversion 
Python :: Python How to convert a string to the name of a function? 
Python :: django content type for model 
Python :: item[0]: (i + 1) * 2 for i, item in (sort_loc) 
Python :: complete dates pandas 
Python :: Reading Custom Delimited 
Python :: matplotlib object oriented 
Python :: django form label in template 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =