How do i read a google spreadsheet in python?

For many data science/visualization projects I work on, setting up a SQL database (or something similar) is overkill. On the opposite side of the spectrum, using local Excel files makes things more difficult to share and replicate. Google Sheets are often an excellent middle-ground, providing an easy-to-use collaborative platform with a familiar Excel-like interface.

Accessing Google sheet data using OAuth and the Google Python API is a straightforward process, thanks to the (per usual) excellent Google documentation. First, we need to setup OAuth credentials on our Google Drive account in order to access the worksheet.

Next, we need to install the Google API client libraries for Python. We can do this in an (ideally, in an activated Python virtual environment) using pip.

Obviously, in a tutorial about accessing Google sheet data, you’re going to need a sheet to work with — I’ll be using my “Volcanic Wines” sheet (based on the awesome volcanic activity data available from the Smithsonian). For the next steps, you’re going to need the sheet ID, which you can get from the URL, and then the name of the worksheet.

Get the spreadsheet ID from the Google Docs URLGet the Google Sheet name of interest

Now, create a new Python script to retrieve the data (make sure the ‘client_secret.json’ file is saved in the same working directory as the script, or provide an explicit path). Update the spreadsheet ID and worksheet names in the code below with the relevant values for your spreadsheet.

Final Python code for accessing Google sheet data and converting to Pandas dataframe

Run the script, and you should get your sheet data returned as a dataframe — stay-tuned for an upcoming set of tutorials that will walk through the creation and deployment of a Plotly Dash web app using this Volcanic Wine data!

Final Pandas dataframe returned from Google Sheet

    This post is inspired by Patrick McKenzie’s reminder that sometimes you don’t need a database:

    So if you’re building out a quick CRUD app for e.g. internal use, Google Docs as a backend (consumed via JSON) is *surprisingly* powerful.

    — Patrick McKenzie (@patio11) July 5, 2014

    In this tutorial, we’ll use Anton Burnashev’s excellent gspread Python package to read, write, and delete data from a Google Spreadsheet with just a few lines of code.

    Google Drive API and Service Accounts

    At the risk of being Captain Obvious, you’re going to need a spreadsheet if you want to follow along with this post. If you don’t have one on hand that’s full of juicy data, might I suggest you make a copy of this spreadsheet with contact information for all United States legislators? (Side note: Ian Webster uses this data in conjunction with Twilio to make it easy for citizens to call congress).

    How do i read a google spreadsheet in python?

    To programmatically access your spreadsheet, you’ll need to create a service account and OAuth2 credentials from the Google API Console. If you’ve been traumatized by OAuth2 development before, don’t worry; service accounts are way easier to use.

    Follow along with the steps and GIF below. You’ll be in and out of the console in 60 seconds (much like Nic Cage in your favorite Nic Cage movie).

    1. Go to the Google APIs Console.
    2. Create a new project.
    3. Click Enable API. Search for and enable the Google Drive API.
    4. Create credentials for a Web Server to access Application Data.
    5. Name the service account and grant it a Project Role of Editor.
    6. Download the JSON file.
    7. Copy the JSON file to your code directory and rename it to client_secret.json

    How do i read a google spreadsheet in python?

    There is one last required step to authorize your app, and it’s easy to miss!

    Find the client_email inside client_secret.json. Back in your spreadsheet, click the Share button in the top right, and paste the client email into the People field to give it edit rights. Hit Send.

    If you skip this step, you’ll get a gspread.exceptions.SpreadsheetNotFound error when you try to access the spreadsheet from Python.

    How do i read a google spreadsheet in python?

    We’re done with the boring part! Now onto the code.

    Read Data from a Spreadsheet with Python

    With credentials in place (you did copy them to your code directory, right?) accessing a Google Spreadsheet in Python requires just two packages:

    1. oauth2client – to authorize with the Google Drive API using OAuth 2.0
    2. gspread – to interact with Google Spreadsheets

    Install these packages with:

    pip install gspread oauth2client
    

    Then paste this code into a new file called spreadsheet.py:

    import gspread
    from oauth2client.service_account import ServiceAccountCredentials
    
    
    # use creds to create a client to interact with the Google Drive API
    scope = ['https://spreadsheets.google.com/feeds']
    creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
    client = gspread.authorize(creds)
    
    # Find a workbook by name and open the first sheet
    # Make sure you use the right name here.
    sheet = client.open("Copy of Legislators 2017").sheet1
    
    # Extract and print all of the values
    list_of_hashes = sheet.get_all_records()
    print(list_of_hashes)
    

    Run python spreadsheet.pyand marvel at the glorious, well-formatted data.

    How do i read a google spreadsheet in python?

    Insert, Update, and Delete from a Spreadsheet with Python

    We’ve just scratched the surface of gspreads’ well documented and comprehensive functionality.

    For instance, we extracted the data into a list of hashes, but you can get a list of lists if you’d prefer:

    Or you could just pull the data from a single row, column, or cell:

    sheet.row_values(1)
    
    sheet.col_values(1)
    
    sheet.cell(1, 1).value
    

    You can write to the spreadsheet by changing a specific cell:

    sheet.update_cell(1, 1, "I just wrote to a spreadsheet using Python!")
    

    Or you can insert a row in the spreadsheet:

    row = ["I'm","inserting","a","row","into","a,","Spreadsheet","with","Python"]
    index = 1
    sheet.insert_row(row, index)
    

    You can also delete a row from the spreadsheet:

    And find out the total number of rows:

    Check the gspread API reference for the full details on these functions along with a few dozen others.

    Using Google Spreadsheets with Python opens possibilities like building a Flask app with a spreadsheet as the persistence layer, or importing data from a Google spreadsheet into Jupyter Notebooks and doing analysis in Pandas. If you want to start playing with Python and Twilio, check out our Python quickstarts.

    If you build something cool, please let me know. You can find me at or @greggyb. And if this post was helpful, please share it with someone else who might dig it.

    Many thanks to Devin and Sam for the reviews, to Google for Sheets, and most of all, to Anton for gspread.

How do I connect Google Sheets with Python?

Create an auth.py file and add the code below to the file..
# auth.py..
from __future__ import print_function..
from googleapiclient.discovery import build..
from google.oauth2 import service_account..
SCOPES = [.
'https://www.googleapis.com/auth/spreadsheets',.
'https://www.googleapis.com/auth/drive'.

Can you use Google Sheets with Python?

To work with Google Sheets in Python, first, we have to install gspread and oauth2client. In addition to that, we'll use Pandas to read our local data and update it to Google Sheets. Now let's import the libraries and connect to Google Sheets.

How do I read a Google Sheet in Pandas?

Here's a quick guide to how it's done..
Install the packages. First, open a Jupyter notebook and install the GSpread Python package by entering ! ... .
Authenticate with Google Sheets. ... .
Open the Google Sheet with Python. ... .
Select the specific worksheet. ... .
Bring it all together..

How does Python read data from Google Drive?

1) Install PyDrive. The first step is to install PyDrive. ... .
2) Authenticate. The second step is to authenticate and create a PyDrive client..
3) Authorizing. ... .
4) Generating a shareable link. ... .
5) Getting the file_id. ... .
6) Load the CSV. ... .
7) Showing the Results..