Skip to main content

A package to easily open an instance of a Google spreadsheet and interact with worksheets through Pandas DataFrames.

Project description

PyPI Version Travis-CI Build Status Documentation Status

author: Diego Fernandez

Links:

Overview

A package to easily open an instance of a Google spreadsheet and interact with worksheets through Pandas DataFrames. It enables you to easily pull data from Google spreadsheets into DataFrames as well as push data into spreadsheets from DataFrames. It leverages gspread in the backend for most of the heavylifting, but it has a lot of added functionality to handle things specific to working with DataFrames as well as some extra nice to have features.

Some key goals/features:

  • Nicely handle headers and indexes

  • Run on Jupyter, headless server, and/or scripts

  • Allow storing different user credentials

  • Automatically handle token refreshes

  • Enable handling of frozen rows and columns

  • Enable handling of merged cells

  • Nicely handle large data sets and retries

  • Enable creation of filters

Installation / Usage

To install use pip:

$ pip install gspread-pandas

Or clone the repo:

$ git clone https://github.com/aiguofer/gspread-pandas.git
$ python setup.py install

Before using, you will need to download Google client credentials for your app.

Client Credentials

To allow a script to use Google Drive API we need to authenticate our self towards Google. To do so, we need to create a project, describing the tool and generate credentials. Please use your web browser and go to Google console and :

  • Choose Create Project in popup menu on the top.

  • A dialog box appears, so give your project a name and click on Create button.

  • On the left-side menu click on API Manager.

  • A table of available APIs is shown. Switch Drive API and click on Enable API button. Do the same for Sheets API. Other APIs might be switched off, for our purpose.

  • On the left-side menu click on Credentials.

  • In section OAuth consent screen select your email address and give your product a name. Then click on Save button.

  • In section Credentials click on Add credentials and switch OAuth 2.0 client ID.

  • A dialog box Create Cliend ID appears. Select Application type item as Other.

  • Click on Create button.

  • Click on Download JSON icon on the right side of created OAuth 2.0 client IDs and store the downloaded file on your file system. Please be aware, the file contains your private credentials, so take care of the file in the same way you care of your private SSH key; i.e. move downloaded JSON to ~/.config/gspread_pandas/google_secret.json (or you can configure the directory and file name by directly calling gspread_pandas.conf.get_config

Thanks to similar project df2gspread for this great description of how to get the client credentials.

User Credentials

Once you have your client credentials, you can have multiple user credentials stored in the same machine. This can be useful when you have a shared server (for example with a Jupyter notebook server) with multiple people that may want to use the library. The first parameter to Spread must be the key identifying a user’s credentials. The first time this is called for a specific key, you will have to authenticate through a text based OAuth prompt; this makes it possible to run on a headless server through ssh or through a Jupyter notebook. After this, the credentials for that user will be stored (by default in ~/.config/gspread_pandas/creds or you can manually set it in GSPREAD_PANDAS_CONFIG_DIR env var) and the tokens will berefreshed automatically any time the tool is used.

Users will only be able to interact with Spreadsheets that they have access to.

Handling Authentication

In the backend, the library is leveraging Google’s oauth2client to handle authentication. It conveniently stores everything as described above so that you don’t have to worry about boiler plate code to handle auth. However, if you need to customize how you handle authentication you can do so in a few different ways. You can change the directory where everything is stored using the GSPREAD_PANDAS_CONFIG_DIR env var. You can also generate your own oauth2client.client.OAuth2Credentials and pass them in when instanciating a Client or Spread object. For other ways to customize authentication, see gspread_pandas.conf.get_config and gspread_pandas.conf.get_creds

Contributing

Code should be run through black, isort, and flake8 before being merged. Pre-commit takes care of it for you, but you need to have Python 3 installed to be able to run black. To contribute, please fork the repo, create a feature branch, push it to your repo, then create a pull request.

To install and set up the environment after you fork it (replace aiguofer with your username):

$ git clone https://github.com/aiguofer/gspread-pandas.git && cd gspread-pandas
$ pip install -e ".[dev]"
$ pre-commit install

Example

from __future__ import print_function
import pandas as pd
from gspread_pandas import Spread, Client

file_name = "http://stats.idre.ucla.edu/stat/data/binary.csv"
df = pd.read_csv(file_name)

# 'Example Spreadsheet' needs to already exist and your user must have access to it
spread = Spread('example_user', 'Example Spreadsheet')
# This will ask to authenticate if you haven't done so before for 'example_user'

# Display available worksheets
spread.sheets

# Save DataFrame to worksheet 'New Test Sheet', create it first if it doesn't exist
spread.df_to_sheet(df, index=False, sheet='New Test Sheet', start='A2', replace=True)
spread.update_cells((1,1), (1,2), ['Created by:', spread.email])
print(spread)
# <gspread_pandas.client.Spread - User: '<example_user>@gmail.com', Spread: 'Example Spreadsheet', Sheet: 'New Test Sheet'>

# You can now first instanciate a Client separately and query folders and
# instanciate other Spread objects by passing in the Client
client = Client('example_user')
# Assumming you have a dir called 'example dir' with sheets in it
available_sheets = client.find_spreadsheet_files_in_folders('example dir')
spreads = []
for sheet in available_sheets.get('example dir', []):
    spreads.append(Spread(client, sheet['id']))

Troubleshooting

SSL Error

If you’re getting an SSL related error or can’t seem to be able to open existing spreadsheets that you have access to, you might be running into an issue caused by certifi. This has mainly been experienced on RHEL and CentOS running Python 2.7. You can read more about it in issue 223 and issue 354 but, in short, the solution is to either install a specific version of certifi that works for you, or remove it altogether.

pip install certifi==2015.4.28

or

pip uninstall certifi

EOFError in Rodeo

If you’re trying to use gspread_pandas from within Rodeo you might get an EOFError: EOF when reading a line error when trying to pass in the verification code. The workaround for this is to first verify your account in a regular shell. Since you’re just doing this to get your Oauth token, the spreadsheet doesn’t need to be valid. Just run this in shell:

python -c "from gspread_pandas import Spread; Spread('<user_key>','')"

Then follow the instructions to create and store the OAuth creds.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gspread-pandas-1.2.1.tar.gz (19.9 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

gspread_pandas-1.2.1-py3.6.egg (31.9 kB view details)

Uploaded Egg

gspread_pandas-1.2.1-py2.py3-none-any.whl (17.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file gspread-pandas-1.2.1.tar.gz.

File metadata

  • Download URL: gspread-pandas-1.2.1.tar.gz
  • Upload date:
  • Size: 19.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.19.8 CPython/3.6.6

File hashes

Hashes for gspread-pandas-1.2.1.tar.gz
Algorithm Hash digest
SHA256 056706858fafb25da540040849fb21072621078112950295e42f8693715cc723
MD5 4ca009f965c9afc2bf7b69032ad2ec36
BLAKE2b-256 8a303aec481d852baf982be7622333b182fcd019523de8c33472751844eae832

See more details on using hashes here.

File details

Details for the file gspread_pandas-1.2.1-py3.6.egg.

File metadata

  • Download URL: gspread_pandas-1.2.1-py3.6.egg
  • Upload date:
  • Size: 31.9 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for gspread_pandas-1.2.1-py3.6.egg
Algorithm Hash digest
SHA256 cd1971c1b16ae688e0a8ff753630788e7e324edc225d995a70af19963cb42c30
MD5 8411e26bd608eba1e0d6c2ec6feb760a
BLAKE2b-256 195195c99408e0027d54601d0948d0606feeef876637c764126039adf8725c3f

See more details on using hashes here.

File details

Details for the file gspread_pandas-1.2.1-py2.py3-none-any.whl.

File metadata

  • Download URL: gspread_pandas-1.2.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 17.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for gspread_pandas-1.2.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f7b49efd6215f03d53619fac4e8d78162e8a431ad9e5b5bc015a45eb9b470c40
MD5 75bd3154b33b1d923d4337c652b44546
BLAKE2b-256 e2f90627b4c0fee64979e21a1e7ba24e46fe9f0158d6a2304494c94f2c58af4f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page