Google spreadsheet Python API Abstractor
Project description
Google sheets API For Python3 v1
Pandas-integrated, Auto-positioning library for Google Sheet API
Features:
- Auto-positioning for value updates on sheets
- Designed to minimize API calls
- Auto-parse values on sheet to get tables with positions and pd.DataFrame interpretations
- Multi-level indexes and columns supported
Installation
pip install gsheeter
Requires Python 3.9+.
Basic Usage
- Authentication (Only service account usage is allowed so far, I will develop functions to use other auth methods in the future)
# using a service account will create a global api client for the project to use throughout
import gsheeter
filename = 'service-account.json' # add a path of your service account json file
gsheeter.service_account(filename)
- Load spreadsheet
from gsheeter import Drive
# 1. using fileId
fileId = 'spreadsheetFileId' # use the file id of the spreadsheet you want to access
spreadsheet = Drive.get_spreadsheet(fileId)
# 2. using filename
filename = 'spreadsheetFileName' # use the name of the spreadsheet you want to access
parentId = 'folderId' # use id of the parent folder of the spreadsheet above, I recommend using folderId when using filename, not fileId
spreadsheet = Drive.get_spreadsheet(
target=filename,
folderId=parentId)
- Add(Create) spreadsheet
from gsheeter import Drive
filename = 'yourspeadsheet' # use the name you want
sheetname = 'default' # default value is None, use only if you want to create the first sheet with a specific name
parentId = 'anyFolderId' # if empty, spreadsheet will be replaced in your root
spreadsheet = Drive.create_spreadsheet(
filename=filename,
sheetname=sheetname,
parentId=parentId)
- Load sheet
from gsheeter import Drive
spreadsheet = Drive.get_spreadsheet(
target='test',
folderId='parentFolderId')
# 1. using sheetId
sheetId = 0
sheet = spreadsheet.get_sheet(sheetId)
# 2. using sheetname
sheetname = 'Sheet1'
sheet = spreadsheet.get_sheet(sheetname)
You can also use kwargs as follows when using Spreadsheet.get_sheet:
- delete_exist: bool = False: if the sheet being searched already exists and delete_exist is set to True, delete the existing sheet and create another one, otherwise, return the existing sheet
# example
sheet = spreadsheet.get_sheet(sheetname, delete_exist=True)
- add: bool = True: if the sheet being searched does not exist, add one
# example, throws exception if the sheet does not exist or is not added
sheet = spreadsheet.get_sheet(sheetname, add=False)
- Add(Create) sheet
from gsheeter import Drive
spreadsheet = Drive.get_spreadsheet(
target='test',
folderId='parentFolderId'
)
# default function behavior, Create a sheet with sheetname "Sheet1", 1000 rowCount and 26 columnCount and index of 0
sheet = spreadsheet.add_sheet()
# with a sheetname
sheetname = 'new_sheet'
sheet = spreadsheet.add_sheet(sheetname)
# create a smaller sheet with 100 rows and 10 columns
sheet = spreadsheet.add_sheet(
sheetname=sheetname,
rowCount=100,
columnCount=10,
)
- Read sheet values: entire sheet
from gsheeter import Drive
fileId = 'fileId'
spreadsheet = Drive.get_spreadsheet(fileId)
sheetname = 'Sheet1'
sheet = spreadsheet.get_sheet(sheetname)
values = sheet.values # returns 2D np.ndarray filled with Values.
# If the sheet is empty, An empty np.ndarray with size of (sheet.rowCount, sheet.columnCount)
- Read sheet values: tables
from gsheeter import Drive
fileId = 'fileId'
spreadsheet = Drive.get_spreadsheet(fileId)
sheetname = 'Sheet1'
sheet = spreadsheet.get_sheet(sheetname)
tables = sheet.tables
for t in tables:
print(t)
- Read sheet values: a table
from gsheeter import Drive
fileId = 'fileId'
spreadsheet = Drive.get_spreadsheet(fileId)
sheetname = 'Sheet1'
sheet = spreadsheet.get_sheet(sheetname)
table = sheet.tables
- Update values: using sheet
from gsheeter import Drive
fileId = 'fileId'
spreadsheet = Drive.get_spreadsheet(fileId)
sheetname = 'Sheet1'
sheet = spreadsheet.get_sheet(sheetname)
import numpy as np
# all coordinates follow array indexing convention, starting from 0
# gsheeter adds 1 to each coordinate to match cell address on sheet
# 1. set values of 2D np.ndarray with x, y coordinate on sheet
arr = np.zeros(shape=(4, 3))
y_offset = 0 # row 1
x_offset = 0 # column 1(A)
sheet.set_values(
data=arr,
y_offset=y_offset,
x_offset=x_offset)
# 2. set values of 1D np.ndarray with x and y coordinate on sheet
arr = np.array([0,1,2,3,4])
y_offset = 1 # row 2
x_offset = 2 # column 3(C)
sheet.set_values(
data=arr,
y_offset=y_offset,
x_offset=x_offset)
# 3. set values of pd.DataFrame with x and y coordinate on sheet
df = pd.DataFrame(...)
y_offset = 0
x_offset = 0
sheet.set_values(
data=df,
y_offset=y_offset,
x_offset=x_offset)
# 4. set values of pd.Series
row = pd.Series(...)
# default values of y_offset and x_offset are 0
# if the input to .set_values() is of type pd.Series, the output value is transposed.
sheet.set_values(row)
# 5. append values at the next row after the last non-empty row
# x_offset determines the index at which the search for non-empty row starts
arr = np.zeros(shape=(4, 3))
sheet.set_values(
data=arr,
x_offset=1, # searches for the next empty row after the last non-empty row starting from x coordinate of 1 to x coordinate of 1 + width of input data
append=True)
- Update values: replace values in a row of a table
from gsheeter import Drive
fileId = 'fileId'
spreadsheet = Drive.get_spreadsheet(fileId)
sheet = spreadsheet.get_sheet('Sheet1')
table = sheet.table
row = table.df.iloc[2] # select third row from this table
row['x'] = 'test'
row['y'] = 'test'
table.update_row(row) # then update the values of the row
- Update values: delete a row of a table
from gsheeter import Drive
fileId = 'fileId'
spreadsheet = Drive.get_spreadsheet(fileId)
sheet = spreadsheet.get_sheet('Sheet1')
table = sheet.table
# delete second row
row = table.df.iloc[1]
table.delete_row(row)
- Update values: delete a table
from gsheeter import Drive
fileId = 'fileId'
spreadsheet = Drive.get_spreadsheet(fileId)
sheet = spreadsheet.get_sheet('Sheet1')
table = sheet.tables[2] # select third table in the sheet
sheet.delete_table(table)
sheet.delete_table(1) # you can also use table index
Advanced Usage and Object structures
- Drive
- Spreadsheet
- Sheet
- Table
Future updates
- Chart
- Other authentication methods
- Cell format
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
gsheeter-0.1.2.tar.gz
(23.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
gsheeter-0.1.2-py3-none-any.whl
(28.0 kB
view details)
File details
Details for the file gsheeter-0.1.2.tar.gz.
File metadata
- Download URL: gsheeter-0.1.2.tar.gz
- Upload date:
- Size: 23.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
48fe90376ab372b5ab90ca6e26a0a2fa46dfda2cca631a60bec4c920dc34e7ec
|
|
| MD5 |
04cb6963e25a64e6ab3398e192ae3824
|
|
| BLAKE2b-256 |
7d12f1e854cd77c69fa8f8ffeab541f415107bfa07cf3bada9575685d88b15a1
|
File details
Details for the file gsheeter-0.1.2-py3-none-any.whl.
File metadata
- Download URL: gsheeter-0.1.2-py3-none-any.whl
- Upload date:
- Size: 28.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b7274f7e5fcdc1bf782494c13b77fd10790c418b868f9191eaac99a7f78579f2
|
|
| MD5 |
5ab323ad02ce4032e79c78da5ea5726b
|
|
| BLAKE2b-256 |
2ebfba772e82f1c32bd07f6304e7870ea6b11d2f6e31e4821238fb319b4c5dff
|