Skip to main content

gspreadsheet

A wrapper around a wrapper to get Google spreadsheets to look like csv.DictReader.

If you’re used to working with CSVs or a human, you’ll find that working with Google’s Python API for spreadsheets is so frustrating. With gspreadsheet, you can adapt your existing csv code to work with Google Spreadsheets with just two line changes. As an added bonus, if you alter the dict, those changes get saved back to the original spreadsheet.

Installation

pip install gspreadsheet

Usage

If your old CSV code looked like this:

from csv import DictReader
reader = DictReader(open('myspreadsheet.csv'))
for row in reader:
    process(row)

It would look like this with gspreadsheet:

from gspreadsheet import GSpreadsheet
reader = GSpreadsheet("https://docs.google.com/myspreadsheet")
for row in reader:
    process(row)

So looking at more examples…

Get a spreadsheet if you know the key and worksheet:

sheet = GSpreadsheet(key='tuTazWC8sZ_r0cddKj8qfFg', worksheet="od6")

Get a spreadsheet if you just know the url:

sheet = GSpreadsheet(url="https://docs.google.com/spreadsheet/"
                         "ccc?key=0AqSs84LBQ21-dFZfblMwUlBPOVpFSmpLd3FGVmFtRVE")

Since just knowing the url is the most common use case, specifying it as a kwarg is optional. Just pass whatever url is in your browser as the first argument.:

sheet = GSpreadsheet("https://docs.google.com/spreadsheet/"
                     "ccc?key=0AqSs84LBQ21-dFZfblMwUlBPOVpFSmpLd3FGVmFtRVE")

Get the JSON representation of the spreadsheet:

sheet.to_JSON()

Authenticating

Get a spreadsheet as a certain user:

sheet = GSpreadsheet(email="foo@example.com", password="12345",
                     key='tuTazWC8sZ_r0cddKj8qfFg', worksheet="od6")

You can also specify the email and password using environment variables: GOOGLE_ACCOUNT_EMAIL and GOOGLE_ACCOUNT_PASSWORD.

And as an authenticated user, you can modify the spreadsheet.:

for row in sheet:
    print row
    if row['deleteme']:
        row.delete()  # delete the row from the worksheet
        continue
    row['hash'] = md5(row['name']).hexdigest()  # compute the hash and save it back

data = row.copy()   # get the last row as a plain dict
sheet.add_row(data)  # copy the last row and append it back to the sheet

Advanced Usage: Saving data back to the spreadsheet

If you modify the dict that represents a row, those changes will get pushed back to the spreadsheet:

>>> row['value']
'foo'
>>> row['value'] = 'bar'  # Change this value
>>> row['value']
'bar'

Advanced Usage: Deferring Saves

If you do multiple changes to a row, the script can get very slow because it has to make a syncronous request back to the server with every change. To avoid this, you can turn on deferred saves by setting deferred_save=True when instantiating a GSpreadsheet. Just remember to .save():

sheet = GSpreadsheet(email="foo@example.com", password="12345",
                     key='tuTazWC8sZ_r0cddKj8qfFg', worksheet="od6",
                     deferred_save=True)

row = sheet.next()
for key in row.keys():
    row['key'] = ''
row.save()

Scary Warnings

I really want to say this is alpha software, but we’ve been using bits and pieces of this for over a year now. Everything is subject to change, even the names. This also relies on google’s relatively ancient gdata package, which does not have support for Python 3.

Changelog

  • v0.4.0 - Added .to_JSON method. Added tox coverage.

Similar Python packages

Metadata

Release files for gspreadsheet 0.4.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 gspreadsheet 0.4.0
File Size Uploaded Provenance
gspreadsheet-0.4.0.tar.gz 7.5 kB Without provenance Details

Release files / gspreadsheet-0.4.0.tar.gz

Download URL gspreadsheet-0.4.0.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
4b6b4bb2384da8b2fe173f8a906d688ae02c8942204be5c94323cb56cc781fbe
BLAKE2b-256 checksum
How to use checksums
20678ef89fa48e503dc14dbce711efdbd4ae95792d203afb34459934ee3eceb4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for gspreadsheet 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

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