Xavier Olive research teaching python blog til cli

How to read a Google Sheets document with pandas

30 August 2022

The most direct way would be to build the URL:

import pandas as pd

url = (
    "https://docs.google.com/spreadsheets/d/{sheet_id}"
    "/gviz/tq?tqx=out:csv&sheet={sheet_name}"
)

# Take the sheet ID from the URL
df = pd.read_csv(url.format(sheet_id="", sheet_name=""))

If you are behind a proxy and things do not work smoothly, you need to go manual:

import requests
from io import StringIO

s = requests.Session()
s.proxies.update({'https': ""})
c = s.get(url)
c.raise_for_status()

txt = StringIO(c.text)
txt.seek(0)

df = pd.read_csv(txt)