How to Automate Google Sheets with Python

How to use pygsheets python package to play around google sheets and to automate.

Dayal Chand Aichara
Game of Data
4 min readJun 14, 2019

--

Google spreadsheets are easy to maintain, edit, and share with people with python package pygsheets. I have been using pygsheets for long time to automate my daily work in google spreadsheets.

pygsheets is a simple intuitive python library to access google spreadsheets through the Google Sheets API v4.

Automating Google Sheets with python is not as hard as climbing Mount Fuji. 😉

A picture from my Mount Fuji trekking.

Everyone knows what google spreadsheets are and how to use them. In this article , we will learn how to play around google spreadsheets with python. So, without further ado, let’s start.

Installation

Get client secret

Obtain OAuth2 credentials from Google Developers Console for google spreadsheet api and drive api and save the file as client_secret.json in same directory as project. See complete guide here.

Authorization

Open spreadsheets and worksheets

Google spreadsheets can be opened by name, id , and link. Worksheets can be accessed by name or index.

How to open a spreadsheet and worksheet with pygsheets.

Playing around spreadsheet

Authorize and open a spreadsheet

Get spreadsheet title

Get spreadsheet id

Get spreadsheet url

Check last update

Delete spreadsheet

Get worksheets info

Share spreadsheet

Remove permissions

Add new worksheet

Delete worksheet

Playing around Worksheet

Open a worksheet

Get title, id, and url of worksheet

Get rows and cols count

Get cell object and cell value

Get value/values/records

Example of get_all_records

Update value/values

Get rows or columns

Add/delete rows and columns

Insert rows and columns

Update row and column

Adjust width of column and height of row

Resize and clear worksheet

Add pandas dataframe to worksheet

Get worksheet values as pandas dataframe

Example of get_of_df()

Add chart to worksheet

Chart added to worksheet.

I guess, you have learned enough to play around google spreadsheets with python. Many more operations can be performed with pygsheets package. Please, see documentation of pygsheets to learn more operations.

Thank you for reading this article. Read my other Medium Articles here.

Reach out to me on LinkedIn, if you have query.

Reference: https://pygsheets.readthedocs.io/en/latest/index.html

--

--

Dayal Chand Aichara
Game of Data

Data Scientist at KPMG Ignition Tokyo , Blockchain Enthusiast, Traveller, Trekker — https://www.linkedin.com/in/dcaichara/