Google Sheets was released over 16 years ago in March 2006 and is one of the core applications comprising Google Workspace today. While Google Sheets does not offer all the bells-and-whistles available with Microsoft Excel, it is still a very powerful spreadsheet application. With Google Workspace enjoying greater adoption as a result of the pandemic-fueled work-at-home trend, it is likely that more and more people are using or will be using Google Sheets.
Like any spreadsheet application, Google Sheets allows users to organize, edit, analyze, and visualize data. Given that Google Sheets has been a web-based application from its inception, its collaborative features are arguably its greatest strength where multiple users can view and edit a given spreadsheet simultaneously. Additional strengths and weaknesses (as compared to Microsoft Excel) include:
The Google Sheets API provides a programmatic way to interact with a Google Sheets spreadsheet and supports a wide variety of languages including Python, Java, and Node.js. An application developer can read data from and write data to a Google Sheets spreadsheet via the API, as well as complete more complex interactions such as creating new worksheets, formatting data, and creating charts. To a certain extent, the Google Sheets API provides developers with a set of methods that can be used to manipulate a given Google Sheets spreadsheet in a nearly unlimited way.
In this tutorial you will learn how to set up the underlying dependencies needed to use the Google Sheets API with a Python application. In particular, you will create a new Google Cloud Platform project, enable the Google Sheets API, create credentials to securely access the API, install the required Python libraries to interact with the API from a Python application, and build a simple test application.
To complete this tutorial, you will need:
pip
installed.
Google Cloud Platform projects allow developers to work with Google Workspace APIs, including the Google Sheets API. Keep in mind that Google sets a quota on the number of projects a given user can create. If you are using the Google Cloud Platform for the first time, then this obviously shouldn't be a problem. However, if you need a higher project quota in the future, you can always request one from Google.
IAM & Admin > Create a Project
from the drop-down menu.Edit
to change the Project ID. However, keep in mind that once the new project is created, the Project ID cannot be changed.No organization
and does not need to be changed. If you used a Google account that is managed, by your employer for example, when logging in, the Location field may display a different value.
Now that you have created a new Google Cloud Platform project, you need to enable the Google Sheets API for the project.
APIs & Services > Library
.Google Sheets API
.Enable
.
A Python application that wants to interact with the Google Sheets API must be authenticated and authorized to access the resources that the API supports. Google provides different options for the authentication and authorization mechanisms depending on the needs of the developer. In this step, we will choose to configure a new Service Account for authentication and authorization purposes.
APIs & Services > Credentials
.Manage Service Accounts
under Service Accounts.Create Service Account
.Service account ID
will be automatically generated in the Service account ID field. Additionally, an e-mail address matching the service account ID will also be automatically generated. For example, if you entered some new service account
in the name field, you might see a strange looking e-mail address ending in .iam.gserviceaccount.com
like some-new-service-account@young-home-460928.iam.gserviceaccount.com
as the service account e-mail. Copy this e-mail address since you will use it later when connecting a Python application to a Google Sheets spreadsheet via the Google Sheets API.Create And Continue
.Select a role
dropdown menu from the Grant this service account access to project section. Look for the Basic
option on the left side of the menu and hover over it. Then, click on Editor
from the right-side of the menu. The Editor
role will allow a Python application using this service account to read and write to a Google Sheets spreadsheet that it is interacting with via the Google Sheets API.Done
to create the new service account.
Manage Keys
.Add Key
and then select Create new key
.Create
. A new private key will be generated and a JSON file download with the new key should automatically start. Note the location of the JSON file with the private key value as it will be needed later when connecting to the Google Sheets API from a Python application.
A Python application needs to import certain Google libraries for Python to successfully authenticate with and interact with the Google Sheets API.
The Google Client Libraries provide access to Google Cloud APIs for a variety of languages. In this tutorial, we will install the Google API Python Client Library.
You can install the Google API Python Client Library using the following command:
pip install google-cloud
The Google Authentication libraries are used for authentication with Google Workspace APIs, and in this particular case, the Google Sheets API.
First, install the Google Authentication Library for Python using the following command:
pip install google-auth
Second, install the Google Authentication OAuth Library for Python using the following command:
pip install google-auth-oauthlib
The required Python libraries have now been installed.
With everything setup in Steps 1 - 4, you can now start programmatically interacting with Google Sheets spreadsheets via the Google Sheets API. This steps builds a simple test application to retrieve the title of a Google Sheets spreadsheet.
My New Google Sheets Spreadsheet
.
https://docs.google.com/spreadsheets/d/8VaaiCuZ2q09IVndzU54s1RtxQreAxgFNaUPf9su5hK0/edit#gid=0
d/
and ending before /edit
.8VaaiCuZ2q09IVndzU54s1RtxQreAxgFNaUPf9su5hK0
using this example.key.json
file generated in Step 3b in the same directory where you will create the test application in Python.
from google.oauth2 import service_account
from googleapiclient.discovery import build
spreadsheet_id = ENTER_YOUR_SPREADSHEET_ID FROM_STEP_5c_HERE_WITH_QUOTES
# For example:
# spreadsheet_id = "8VaaiCuZ2q09IVndzU54s1RtxQreAxgFNaUPf9su5hK0"
credentials = service_account.Credentials.from_service_account_file("key.json", scopes=["https://www.googleapis.com/auth/spreadsheets"])
service = build("sheets", "v4", credentials=credentials)
request = service.spreadsheets().get(spreadsheetId=spreadsheet_id, ranges=[], includeGridData=False)
sheet_props = request.execute()
print(sheet_props["properties"]["title"])
# Output:
# My New Google Sheets Spreadsheet
In this tutorial, you created a new Google Cloud Platform project, enabled the Google Sheets API for that project, created a new service account to authenticate with the Google Sheets API, installed all required Google libraries for Python, and built a simple test application. As mentioned in the introduction, the possibilities are almost endless when programmatically interfacing with Google Sheets. See Google Sheets for Developers documentation for more information.