API Reference¶
Collection of spreadsheets available from given OAuth 2.0 credentials or API key. |
|
Fetched collection of worksheets. |
|
Read-only view on the list of worksheets in a spreadsheet. |
|
Two-dimensional table with cells accessible via A1 notation. |
|
Make OAuth 2.0 credentials for scopes from |
|
Return a service endpoint for interacting with a Google API. |
Sheets¶
- class gsheets.Sheets(credentials=None, developer_key=None)¶
Collection of spreadsheets available from given OAuth 2.0 credentials or API key.
- __getitem__(id)¶
Fetch and return the spreadsheet with the given id.
- __iter__()¶
Fetch and yield all available spreadsheets.
- Yields:
new SpreadSheet spreadsheet instances
- __len__() int¶
Return the number of available spreadsheets.
- Returns:
number of spreadsheets
- Return type:
- find(title)¶
Fetch and return the first spreadsheet with the given title.
- findall(title=None)¶
Fetch and return a list of spreadsheets with the given title.
- classmethod from_developer_key(developer_key)¶
Return a spreadsheet collection using an API key.
- classmethod from_files(secrets=None, storage=None, scopes=None, *, no_webserver=False)¶
Return a spreadsheet collection making OAauth 2.0 credentials.
- Parameters:
- Returns:
new Sheets instance with OAauth 2.0 credentials
- Return type:
- get(id_or_url, default=None)¶
Fetch and return the spreadsheet with the given id or url.
- Parameters:
id_or_url (str) – unique alphanumeric id or URL of the spreadsheet
- Returns:
New SpreadSheet instance or given default if none is found
- Raises:
ValueError – if an URL is given from which no id could be extracted
- ids() list[str]¶
Return a list of all available spreadsheet ids.
- Returns:
list of unique alphanumeric id strings
- Return type:
SpreadSheet¶
- class gsheets.models.SpreadSheet(id, title, sheets, service)¶
Fetched collection of worksheets.
- __getitem__(id)¶
Return the worksheet with the given id.
- __iter__()¶
Yield all contained worksheets.
- Yields:
WorkSheet objects (spreadsheet members)
- __len__() int¶
Return the number of contained worksheets.
- Returns:
number of worksheets
- Return type:
- find(title)¶
Return the first worksheet with the given title.
- findall(title=None)¶
Return a list of worksheets with the given title.
- property first_sheet¶
The first worksheet of the spreadsheet.
- get(id, default=None)¶
Return the worksheet with the given id or the given default.
- Parameters:
id – numeric id of the worksheet
- Returns:
contained worksheet object or given default
- Return type:
- Raises:
ValueError – if
idis not anint
- property id: str¶
Unique alphanumeric id of the spreadsheet (
str).see https://developers.google.com/sheets/guides/concepts#spreadsheet_id
- property sheets¶
List view of the worksheets in the spreadsheet (positional access).
- to_csv(*, encoding='utf-8', dialect='excel', make_filename='%(title)s - %(sheet)s.csv') None¶
Dump all worksheets of the spreadsheet to individual CSV files.
- Parameters:
If
make_filenameis a string, it is string-interpolated with an infos-dictionary with the fieldsid(spreadhseet id),title(spreadsheet title),sheet(worksheet title),gid(worksheet id),index(worksheet index), anddialectCSV dialect to generate the filename:filename = make_filename % infos.If
make_filenameis a callable, it will be called with the infos-dictionary to generate the filename:filename = make_filename(infos).
SheetsView¶
- class gsheets.models.SheetsView(items)¶
Read-only view on the list of worksheets in a spreadsheet.
- __getitem__(index)¶
Return the worksheet at the given index (position).
- Parameters:
index – zero-based position or slice
- Raises:
IndexError – if
indexis out of range
WorkSheet¶
- class gsheets.models.WorkSheet(id, title, index, values)¶
Two-dimensional table with cells accessible via A1 notation.
- __getitem__(index)¶
Return the value(s) of the given cell(s).
- Parameters:
index (str) – cell/row/col index (‘A1’, ‘2’, ‘B’) or slice (‘A1’:’C3’)
- Returns:
value (cell), list(col, row), or nested list (two-dimentional slice)
- Raises:
TypeError – if
indexis not a string or slice of stringsValueError – if
indexcanot be parsedIndexError – if
indexis out of range
- at(row, col)¶
Return the value at the given cell position.
- Parameters:
- Returns:
cell value
- Raises:
TypeError – if
roworcolis not anintIndexError – if the position is out of range
- property id: str¶
Stable numeric worksheet id (
int), unique within its spreadsheet.see https://developers.google.com/sheets/guides/concepts#sheet_id
- property index¶
Zero-based position of the worksheet.
- property spreadsheet¶
Containing spreadsheet of the worksheet.
- to_csv(filename=None, *, encoding='utf-8', dialect='excel', make_filename='%(title)s - %(sheet)s.csv') None¶
Dump the worksheet to a CSV file.
- Parameters:
If
make_filenameis a string, it is string-interpolated with an infos-dictionary with the fieldsid(spreadhseet id),title(spreadsheet title),sheet(worksheet title),gid(worksheet id),index(worksheet index), anddialectCSV dialect to generate the filename:filename = make_filename % infos.If
make_filenameis a callable, it will be called with the infos-dictionary to generate the filename:filename = make_filename(infos).
- to_frame(*, assign_name=False, **kwargs)¶
Return a pandas DataFrame loaded from the worksheet data.
- Parameters:
assign_name (bool) – set name attribute on the DataFrame to sheet title.
**kwargs – passed to
pandas.read_csv()(e.g.header,index_col)
- Returns:
new
DataFrameinstance- Return type:
pandas.DataFrame
Low-level functions¶
- gsheets.get_credentials(scopes=None, secrets=None, storage=None, *, no_webserver=False)¶
Make OAuth 2.0 credentials for scopes from
secretsandstoragefiles.- Parameters:
scopes – scope URL(s) or
'read','write'(default:'read')secrets – location of secrets file (default:
'~/client_secrets.json')storage – location of storage file (default:
'~/storage.json')no_webserver – url/code prompt instead of webbrowser based auth
see https://developers.google.com/sheets/quickstart/python see https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
- gsheets.build_service(name=None, **kwargs)¶
Return a service endpoint for interacting with a Google API.