How to read a google sheet with Python

2 min

Reading a Google sheet is a bit more complex than reading an Excel spreadsheet.

Read Excel spreadsheet using python
I bet that your daily work require work with spreadsheet. You might want to read an excel spreadsheet using Python. The best library when dealing with spreadsheet is by far the pandas[] library So here is the code : import pandas as pddf = pd.read_excel…

It requires a bit more authentication

We will need to setup

  1. The script that will get the data from a google spreadsheet
  2. Give Google Robots access and permission to see our google spreadsheet
  3. Install the correct libraries to deal with google authentication

We will need to

  • Create a project on google console
  • Create credentials (Service account)
  • Create an API key (download json file)
  • Get the service account email and share google sheet with it (role : editor)
  • Copy the Google Spreadsheet Id
  • Put the worksheet name as well
  • Execute your script

Where should I put my file ?

What usually matter when dealing with path is

  1. Where do you run your script
  2. Where is your excel file

Usually if you keep them in the same directory like so‌

├── my_file.xslx

The following code should work‌

import pandas as pd
df = pd.read_excel("./my_file.xslx") # Path ./ means current directory

In case you don't have the pandas library

Open your terminal and type in :‌

pip3 install pandas

It will use pip3 package manager to install the pandas library

Going further

If you want to go to the next level with your Python skill, I have a course that covers what most of the people use Python for.

The best part is that it's free.

Turbocharge your workflow with Python TODAY!

A free short-course that covers all the Python that most practitioners use. Straight to the point, no BS.