Manage your spreadsheets with pygsheets in Python.
Features:
- Simple to use
- Open spreadsheets using title or key
- Extract range, entire row or column values.
- Google spreadsheet api v4 support
Python 2.6+
pip install --upgrade google-api-python-client
git clone https://github.com/nithinmurali/pygsheets.git
cd pygsheets
python setup.py install
-
Obtain OAuth2 credentials from Google Developers Console for google spreadsheet api and drive api and save the file as client_secret.json in same directory as project
-
Start using pygsheets:
import pygsheets
gc = pygsheets.authorize()
# Open a worksheet from spreadsheet with one shot
wks = gc.open('my new ssheet').sheet1
wks.update_acell('B2', "it's down there somewhere, let me take another look.")
# Fetch a cell range
cell_list = wks.range('A1:B7')
# You can open a spreadsheet by its title as it appears in Google Docs
sh = gc.open("My poor gym results") # <-- Look ma, no keys!
# If you want to be specific, use a key (which can be extracted from
# the spreadsheet's url)
sht1 = gc.open_by_key('0BmgG6nO_6dprdS1MN3d3MkdPa142WFRrdnRRUWl1UFE')
# Or, if you feel really lazy to extract that key, paste the entire url
sht2 = gc.open_by_url('https://docs.google.com/spreadsheet/ccc?key=0Bm...FE&hl')
# Select worksheet by index. Worksheet indexes start from zero
worksheet = sh.get_worksheet('index',0)
# By title
worksheet = sh.worksheet('title',"January")
# Most common case: Sheet1
worksheet = sh.sheet1
# Get a list of all worksheets
worksheet_list = sh.worksheets()
# With label
val = worksheet.acell('B1').value
# With coords
val = worksheet.cell(1, 2).value
# Get all values from the first row
values_list = worksheet.row_values(1)
# Get all values from the first column
values_list = worksheet.col_values(1)
Each cell has a value and coordinates (row, col, label) properties.
value = cell.value
row_number = cell.row
column_number = cell.col
cell_label = cell.label
each cell is directly linked with its cell in spreadsheet. hence to changing the value of cell object will update the corresponding cell in spreadsheet
c1 = worksheet.acell('B1')
c1.value = 'hehe'
# Or
worksheet.update_acell('B1', 'hehe')
# Select a range
cell_list = worksheet.range('A1:C7')
for cell in cell_list:
cell.value = 'O_0'
#Or
cell_list = worksheet.row_values(2,'cell')
This library is Still in development phase. I have only implimented the basic features that i required. So there is a lot of work to be done. The models.py is the file which defines the models used in this library. There are mainly 3 models - spreadsheet, worksheet, cell. Fuctions which are yet to be implimented are left out empty with an @TODO comment. you can start by implimenting them. The communication with google api using google-python-client is implimented in client.py and the exceptions in exceptions.py
Please report bugs and suggest features via the GitHub Issues.
Before opening an issue, search the tracker for possible duplicates. If you find a duplicate, please add a comment saying that you encountered the problem as well.
- Check the GitHub Issues for open issues that need attention.
- Follow the Contributing to Open Source Guide.
Most of the outline code of this library is copied form the gspread library