Search code examples
pythonpandasgoogle-sheetsgoogle-colaboratorygspread

How to get data from a google spreadsheet that I only have view access to into google colaboratory


I have a link to a google spreadsheet that I do not own, but have view access to. Is there a way to get the data in that spreadsheet into the google colab notebook that I am using?


Solution

  • You should be able to retrieve the content of a Sheet with View Only access in gspread by using open_by_key() or open_by_url().

    Example:

    Test Sheet:

    enter image description here

    Google Colab:

    !pip install --upgrade -q gspread
    
    import gspread
    import pandas as pd
    from google.colab import auth
    from google.auth import default
    auth.authenticate_user()
    creds, _ = default()
    
    gc = gspread.authorize(creds)
    # sh = gc.open_by_url("Insert Sheet URL here")
    sh = gc.open_by_key("Insert Sheet key here")
    worksheet = sh.worksheet("Sheet1")
    rows = worksheet.get_all_values()
    pd.DataFrame.from_records(rows)
    

    Result:

    enter image description here

    Reference: