DEV Community

Cover image for How to Access Google Sheets from a Jupyter Notebook
Eduardo Blancas
Eduardo Blancas

Posted on • Originally published at ploomber.io

How to Access Google Sheets from a Jupyter Notebook

Data is the starting point for all the projects and products in data science. The first lines of a script are dedicated to reading the data. This is a fact and does not change based on the project. What changes is the source of data. There are a variety of places where we store the data such as databases, S3 bucket, BigQuery, external files, spreadsheets, and so on.

Google sheets are quite common for storing small to medium size data. One of the nice things about Google sheets is that you can directly connect to them from a Jupyter notebook. You don’t have to download the data in your local directory and read it from there. Another advantage of connecting from within a notebook is that you can update the sheet directly.

Consider you have a data manipulating task to do. You write your script that connects to Google sheets, read the data, do the manipulation, and write the updated file back to the same sheet. You can schedule this script so that the data in the sheet is always up to date.

In this article, we will learn how to access Google sheets from a Jupyter notebook.

Create a project

The first step is to create a project in the Google Cloud Console, which can be done by clicking on create project in the console. You will have a quota of 12 free projects with your account.

Image description

Enable Google Drive API and Google Sheets API

The next step is to enable the Google Drive API and Google Sheets API. In the Google Cloud Console menu, select APIs & Services and then Enabled APIs & Services as shown below:

Image description

If you have multiple projects, you will need to select the one you want to enable the API for. If you only have one project, it will automatically be selected.

Click on “ENABLE APIS and SERVICES” and then search for “Google Drive API” and then “Google Sheets API”. In the search results, click on the related icons as shown below. Then click on ENABLE API in the page that opens up.

Image description
Image description

Create credentials

Now that we have the Google Drive API enabled, we need to create credentials. At the top right corner of the page that opened up after enabling the API, you will see the CREATE CREDENTIALS icon and then select service account credentials. The page shown below opens up.

Image description

Give it a name and click CREATE AND CONTINUE. Then the grant pages open up, just hit CONTINUE and DONE. You will then see the following screen. Click on the link in the email part.

Image description

It will take you to the service account details page. Copy the email address shown here. This email will be used for connecting to your account.

Image description

We also need to generate a key. Go back to the service accounts page. Click on the three dots under the actions.

Image description

Click on MANAGE KEYS and then ADD a KEY. It will ask you to choose the format, select JSON and hit CREATE. The json file will automatically be downloaded.

Connecting to the Google Sheet

You, of course, need a Google sheet to connect to. I created one that contains some sample sales data. You will need to share the Google sheet with the email address copied in the previous step. Click on the share button at the top right corner of the Google sheet, paste this address and hit SEND.

Image description

We are now ready to connect to this sheet. Open up a Jupyter notebook. We will use a Python library called gspread, which can be installed with pip.

!pip install gspread
Enter fullscreen mode Exit fullscreen mode

The next step is to import the library.

import gspread
Enter fullscreen mode Exit fullscreen mode

We need to give the service account details to gspread, which can be done using the service_account method.

sa = gspread.service_account(filename="project-1-357814-ba841f7c3630.json")
Enter fullscreen mode Exit fullscreen mode

The path to the json file that contains the service credentials is passed to the filename parameter. If the file is in the same working directory as the notebook, you can just write the name of the file.

The sa is a gspread client, which can be used for connecting to the sheets by using the open method and the sheet name.

sheet = sa.open("sample_sales")
Enter fullscreen mode Exit fullscreen mode

A Google sheet document might have multiple pages (i.e. worksheets) so we also need to specify the page name before getting the data. Ours has one page that is called “Sheet1”.

work_sheet = sheet.worksheet("Sheet1")
Enter fullscreen mode Exit fullscreen mode

We have the data in a gspread worksheet object. We can extract the data using the get_all_records method and create a Pandas DataFrame as follows:

import pandas as pd

df = pd.DataFrame(work_sheet.get_all_records())
Enter fullscreen mode Exit fullscreen mode

Let's take a look at the first 5 rows of the data using the head method.

df.head()
Enter fullscreen mode Exit fullscreen mode

Image description

We have successfully connected to a Google sheet and retrieved the data it contains. Most of the steps we have completed need to be done only for once. After that, it is just a matter of writing the sheet name, which is definitely more practical than downloading the data as a CSV file and then reading it.

Organizing your data analysis code can become a challenging task, check out our open-source framework which allows you to build modular data analysis pipelines so you can extract those insights from the spreadsheets!

Top comments (0)