Skip to content

Instantly share code, notes, and snippets.

@jnalley
Created February 7, 2020 20:32
Show Gist options
  • Save jnalley/69acaf75d81127c2758370aaa169cf8c to your computer and use it in GitHub Desktop.
Save jnalley/69acaf75d81127c2758370aaa169cf8c to your computer and use it in GitHub Desktop.
import gspread
from gspread_dataframe import get_as_dataframe
from oauth2client.service_account import ServiceAccountCredentials
class GoogleSheets:
def __init__(self, credentials):
scope = ["https://www.googleapis.com/auth/drive"]
self.gc = gspread.authorize(
ServiceAccountCredentials.from_json_keyfile_dict(
credentials, scope
)
)
def open_by_key(self, key, wks=0, df=True):
sht = self.gc.open_by_key(key)
if wks is None:
return sht
if isinstance(wks, int):
worksheet = sht.get_worksheet(wks)
else:
worksheet = sht.worksheet(wks)
if df is not True:
return worksheet
return get_as_dataframe(worksheet)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment