Access and Modify Google Sheet using Python

30 / Jun / 2016 by Navjot Singh 4 comments


This would be my first blog where I am using Python. I am excited to share what I did using Python. I wanted to update google sheet using bash shell scripting but did not found anything I can play around using bash. Later, I opted for Python as it provides “gspread” library which could be used to work on google sheet.


Scenario: Modify google sheet using python.


  1. The system should have python, gcc and pip installed or run the following command to install them:

    [js]sudo apt-get install python -y
    apt-get install gcc -y[/js]

  2. We need to install “gspread” library of Python and install/upgrade google-api-python-client to authenticate and work on google sheets as follow:

    [js]pip install gspread
    pip install –upgrade google-api-python-client[/js]

Now, we are ready to start working with google sheets as below:

Authentication: The first step would be to authenticate to our google account in order to get access to google sheets present in our account.
Python’s gspread library provide an api to directly login into google account but it has stopped working since April 20, 2015 and when used it throws following exception:

[js]gc = gspread.login(‘’, ‘P@SSw0Rd’)
/usr/local/lib/python2.7/dist-packages/gspread/ Warning: 
           ClientLogin is deprecated:
           Authorization with email and password will stop working on April 20, 2015.            
           Please use oAuth2 authorization instead:
 """, Warning)
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "/usr/local/lib/python2.7/dist-packages/gspread/", line 318, in login   client.login()
 File "/usr/local/lib/python2.7/dist-packages/gspread/", line 127, in login
   "Unable to authenticate. %s" % ex.message)
<span style="font-weight: 400;">gspread.exceptions.AuthenticationError: Unable to authenticate. 404: </span><a href=""><span style="font-weight: 400;"></span></a><span style="font-weight: 400;"> [/js]

We can use OAuth token provided by google for authentication which is a better security practice as it does not expose sensitive information. Below are the steps to get OAuth token and download it as JSON file:

      1. Login into the google developer console with your google credentials and we will see the following page:


  • Click on “My Projects” highlighted above and create a new project if there is no any project, otherwise, the same project can be used:



  • Select the project created in step 1. We would need to enable Google API for google drive. Click “Drive API” as below and enable it:



  • Click “Credentials” on the left panel and then click “Create Credentials” as shown below:



  • Select “Service account key” from the list:
  • On the next page, select service account as “App Engine default service account”, key type as “JSONand click “Create” as below and a JSON file will be downloaded on our system:



We would use this JSON (say credentials.json) for authentication into our google account.

  1. Now, we would need to share the google sheet  with the client email(the same way we share it with others)  which could be found inside the JSON file against “client_email” field.
  2. Below code will help us to authenticate into the google account using credentials.json file:

    [js]import gspread
    from oauth2client.service_account import ServiceAccountCredentials
    scope = ""
    credentials = ServiceAccountCredentials.from_json_keyfile_name(‘credential.json’, scope)
    gs = gspread.authorize(credentials)[/js]

  3. We are now authenticated into google account and can use “gs” object to access any google sheet (say “Demo Sheet”) present in our account as follows:

    [js]gsheet ="Demo Sheet")[/js]

  4. Select one worksheet (say “sheet1”) to work on as follows:

    [js]wsheet = gsheet.worksheet("sheet1") [/js]

Now, we got an object using which we can edit the worksheet. Below are few use cases which I used frequently.

  1. Read data from one cell (say “B5” cell) as below:


  2. Read data from a cell range (say ‘B3:B13’) as below:


  3. Update a single cell (say ‘A4’ with 24) as below:

    [js]update_acell(‘A4’), ‘24’)[/js]

  4.  Copy data from one cell range to another(say ‘B3:B13’ to ‘D2:D12’) as below:

    [js]range1 = wks.range(‘B3:B13’)
    range2 = wks.range(‘C3:C13’)
    for range1_cell,range2_cell in zip(range1,range2):
    range2_cell.value = range1_cell.value

Python’s gspread becomes very handy when it comes to automating tasks which involve updating google sheets.


comments (4)

  1. azdin

    must be without “)” by (3.)
    true: update_acell(‘A4’, ‘24’)
    false: update_acell(‘A4’), ‘24’)

  2. Jake

    This article seems out of date several steps are different on the actual google developer site. Site has a different layout than screenshots show “App Engine default service account” is not a drop down option. Trying to figure out the work around but I’m kinda new at this.


Leave a Reply

Your email address will not be published. Required fields are marked *