Created
November 17, 2021 17:11
-
-
Save GabrielSGoncalves/efae985af163e3ecf83253936041f205 to your computer and use it in GitHub Desktop.
Function for reading private Google Spreadsheets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import requests | |
import gspread | |
def read_private_spreadsheets( | |
credentials_json: str, sheet_key: str, worksheet: int = 0 | |
) -> pd.DataFrame: | |
"""Read a private available Google Spreadsheet as a Pandas Dataframe. | |
Parameters | |
---------- | |
credentials_json : str | |
Path to JSON file with GCloud Credentials. | |
sheet_key : str | |
Key associated to the target spreadsheet. | |
worksheet : int (default=0) | |
Index or name for the target worksheet. | |
Returns | |
------- | |
pd.DataFrame | |
Dataframe loaded from the spreadsheet. | |
""" | |
gcloud = gspread.service_account(filename=credentials_json) | |
sheet = gcloud.open_by_key(sheet_key) | |
worksheet = sheet.get_worksheet(worksheet) | |
list_rows_worksheet = worksheet.get_all_values() | |
return pd.DataFrame( | |
list_rows_worksheet[1:], columns=list_rows_worksheet[0] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment