Skip to main content

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

Project description

===============
Getting Started
===============

|PyPI version|

author: Diego Fernandez

Links:

- `Documentation <https://aiguofer.github.io/gspread-pandas/>`_
- `Source code <https://github.com/aiguofer/gspread-pandas>`_

Overview
========

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

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

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 <https://console.developers.google.com/>`__ 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. 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 <https://github.com/maybelinot/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``) 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.

Contributing
============

::

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

TBD

Example
=======

.. code:: python

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

file_name = "http://www.ats.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'>

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 `here <https://github.com/burnash/gspread/issues/223>`_
and `here <https://github.com/burnash/gspread/issues/354>`_ but, in short, the
solution is to either install a specific version of ``certifi`` that works for you,
or remove it altogether.

.. code:: bash

pip install certifi==2015.4.28

or

.. code:: bash

pip uninstall certifi


.. |PyPI version| image:: https://badge.fury.io/py/gspread-pandas.svg
:target: https://badge.fury.io/py/gspread-pandas

EOFError in Rodeo
-----------------

If you're trying to use ``gspread_pandas`` from within
`Rodeo <https://www.yhat.com/products/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:

.. code:: python

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

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-0.11.1.tar.gz (12.3 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-0.11.1-py3.6.egg (22.1 kB view details)

Uploaded Egg

gspread_pandas-0.11.1-py3.5.egg (19.0 kB view details)

Uploaded Egg

gspread_pandas-0.11.1-py2.py3-none-any.whl (15.0 kB view details)

Uploaded Python 2Python 3

gspread_pandas-0.11.1-py2.7.egg (18.6 kB view details)

Uploaded Egg

File details

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

File metadata

File hashes

Hashes for gspread-pandas-0.11.1.tar.gz
Algorithm Hash digest
SHA256 dd0c216b42538d9df2fe047279e7c7c066f22f8590c1da68cc3eb51b88f4791d
MD5 58e5a011c15be710fafeb69fdc2c6b64
BLAKE2b-256 da9ed4867848787cfc396e516ed3eedac2af6f570fae97807ea463dd1eb95006

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gspread_pandas-0.11.1-py3.6.egg
Algorithm Hash digest
SHA256 da93e6fb4de46fc875f62134db3bc80a666c7a955982aa0f667f04e4906b91a4
MD5 93f91e4736880e01f60abfc9764f1cdf
BLAKE2b-256 e27cab403141e8bf3500838cfa027a67bc9312c8677ed7ac9bd1c940a2d9c6a4

See more details on using hashes here.

File details

Details for the file gspread_pandas-0.11.1-py3.5.egg.

File metadata

File hashes

Hashes for gspread_pandas-0.11.1-py3.5.egg
Algorithm Hash digest
SHA256 141b16973bf412c1238746ead6e59407717739377d3082033ba42d11c551bc7d
MD5 cdedc2874ae61d3ae8a0c985ba4f2d30
BLAKE2b-256 2100e41849a8151ec022b1899ac9bacc139f587a1f9fb6392381e7fb519267d4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gspread_pandas-0.11.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f37291541b2928ca294c801f631d7cd4557c9476354bbb2bb5118b9a1b2a2ebb
MD5 587a587a3dd5b593298a170ba2a05be4
BLAKE2b-256 14dad3420628c18e9bbc8180920bf4f7fb0608a3f6c53647b2f05b0bc9eae84a

See more details on using hashes here.

File details

Details for the file gspread_pandas-0.11.1-py2.7.egg.

File metadata

File hashes

Hashes for gspread_pandas-0.11.1-py2.7.egg
Algorithm Hash digest
SHA256 329fa610a524cfdf5911a03eca4dc8a304f13f100f8646500add523f246c7008
MD5 ca13bdcb7296d20b64994b2a98d0df8a
BLAKE2b-256 0f3b041fc2521e40f12681460f2fa6bf5e9f6dd797207a6e631c44d7ff01d89a

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