/pygsheets

An python wrapper for google spread sheet api v4

Primary LanguagePythonMIT LicenseMIT

Google Spreadsheets Python API v4

Downloads

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

Requirements

Python 2.6+

Installation

From GitHub

pip install --upgrade google-api-python-client
git clone https://github.com/nithinmurali/pygsheets.git
cd pygsheets
python setup.py install

From PyPI (TBD)

Basic Usage

  1. 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

  2. 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')

More Examples

Opening a Spreadsheet

# 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')

Selecting a Worksheet

# 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()

Getting a Cell Value

# With label
val = worksheet.acell('B1').value

# With coords
val = worksheet.cell(1, 2).value

Getting All Values From a Row or a Column

# 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)

Cell Object

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

Updating Cells

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')

How to Contribute

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

Report Issues

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.

Contribute code

NB

Most of the outline code of this library is copied form the gspread library