Skip to main content

Stage PyPI gspread Documentation Status

Gspread2

A wrapper around gspread for easier usage. Intended to provide features and syntax similar to OpenPyXL.

DISCLAIMER: This library is still under development!
Until v1.0.0 is released, assume everything is subject to change.

Features

  • Cell Formatting such as Fonts, Colors and Borders
  • OpenPyXL functions such as iter_rows() and iter_cols()
  • Values are automatically applied to the sheet when updated See Issue #1

Roadmap/TODO

  • Documentation (WIP)
  • Formulas
  • Filters and Pivot Tables

Installation

Requirements:

  • Python3.6+

Install via Pip

$ pip install gspread2

Basic Usage

Getting Started

Create API credentials

Before using this library, you must log into Google Developers page and set up a Service Account, allowing read/write access to your Google Sheets.

  1. Head to Google Developers Console and create a new project (or select the one you have.)

  2. Navigate to "API & Services", "Credentials".

  3. Click on "CREATE CREDENTIALS", "Service account" and follow through the prompts. On the last page, create a JSON key and save it locally. You will need to import this into the library to authenticate to the API.

  4. Once you hit "Done", you will see the email address under "Service Accounts", make note of that email.

  5. On your Google Sheet, hit "Share" and add the email above.

  6. You should now have the credentials and permissions to view and edit your Google Sheet.

Load Workbook

To access a Workbook, you'll need the Google Sheet URL and the credentials file as shown above. The following code example will return a Workbook object:

import gspread2

URL = 'https://docs.google.com/spreadsheets/d/spreadsheetID'
CREDENTIALS = 'path/to/json.file'

workbook = gspread2.load_workbook(URL, CREDENTIALS)

You can also import the Workbook class and initialise it with the same parameters:

from gspread2.models import Workbook

URL = 'https://docs.google.com/spreadsheets/d/spreadsheetID'
CREDENTIALS = 'path/to/json.file'

workbook = Workbook(URL, CREDENTIALS)

Load Worksheet

Once you have a Workbook loaded, you can access worksheets in a number of ways:

workbook = gspread2.load_workbook(URL, CREDENTIALS)
worksheet = workbook['Sheet 1']

OR

workbook = gspread2.load_workbook(URL, CREDENTIALS)
worksheet = workbook.get_sheet_by_name('Sheet 1')

To get the first sheet (usually the active one):

workbook = gspread2.load_workbook(URL, CREDENTIALS)
worksheet = workbook.active

Select Cell

You can select cells individually or iterate through columns and rows (other gspread functions are still available such as worksheet.range())

Worksheet.cell()

Select an individual cell in the worksheet

cell = worksheet.cell(1, 2)  # 1st row, 2nd column

OR

cell = worksheet.cell('B1')
Worksheet.iter_rows()

Returns a list of lists of cells for each row. This function is the same as found on OpenPyXL.
Arguments are as follows:

  • start_row (default: First row)
  • end_row (default: Last row)
  • start_col (default: First column)
  • end_col (default: Last column)
worksheet.iter_rows(2, 4, 3, 5)

The example above will return:

[
    [Cell(C2), Cell(D2), Cell(E2)],
    [Cell(C3), Cell(D3), Cell(E3)],
    [Cell(C4), Cell(D4), Cell(E4)],
]
Worksheet.iter_cols()

Returns a list of lists of cells for each column. This function is the same as found on OpenPyXL.
Arguments are as follows:

  • start_row (default: First row)
  • end_row (default: Last row)
  • start_col (default: First column)
  • end_col (default: Last column)
worksheet.iter_cols(2, 4, 3, 5)

The example above will return:

[
    [Cell(C2), Cell(C3), Cell(C4)],
    [Cell(D2), Cell(D3), Cell(D4)],
    [Cell(E2), Cell(E3), Cell(E4)],
]

Edit Cells

Cell Values

Once you have retrieved your desired cells as shown above, you'll want to update the value. On the original gspread library, you have to keep track of all the cells you modified and pass them on to worksheet.update_cells().
In gspread2 you do not have to pass on the cells to the function, the library will know what you modified.

cell = worksheet.cell(1, 1)
cell.value = 'New Value'
worksheet.update_cells()
Cell Fonts

gspread does not provide any formatting features. To apply formatting to a cell, you must initialise a Font instance (gspread2.styles.Font)

from gspread2.styles import Font

cell = worksheet.cell(1, 1)
new_font = Font(
    name='Arial',
    size=12,
    bold=True,
    italic=True,
    strikethrough=True,
    underline=True,
    color='#FF000000',
)
cell.font = new_font
worksheet.update_cells()

All arguments for Font are optional and default to None.
Arguments with None as a value will be untouched on update.

Cell Fill

To apply a background color to a cell, you must initialise gspread2.styles.colors.Color and set it to cell.fill

from gspread2.styles.colors import Color
cell = worksheet.cell(1, 1)
bg_color Color('#FF000000')
cell.fill = bg_color
worksheet.update_cells()

Metadata

Release files for gspread2 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gspread2 0.2.0
File Size Uploaded
gspread2-0.2.0.tar.gz 7.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for gspread2 0.2.0
File Interpreter ABI Platform
gspread2-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.2 kB

Release files / gspread2-0.2.0.tar.gz

Download URL gspread2-0.2.0.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
4ae7cc7959397e7ceb5b98528a8634adc3064757a28598cba8f061068a32d255
BLAKE2b-256 checksum
How to use checksums
0af4373a3793de9a13c165c9284ce4235b10175ca309fad9a286a453abfe1bb9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.4

Release files / gspread2-0.2.0-py3-none-any.whl

Download URL gspread2-0.2.0-py3-none-any.whl
Size 10.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7a0410ead565e275386c4213ae04ccae031ab4c2ca61454bce7f05a7a012ad67
BLAKE2b-256 checksum
How to use checksums
17fedafbdf8a7710733680672dd882d1fe4c781052e00fb39b384f2e0b30cfe6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.4

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.3

3 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page