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.

The target audience are Data Analysts and Data Scientists, but it can also be used by Data Engineers or anyone trying to automate workflows with Google Sheets and Pandas.

Some key goals/features:

  • Be easy to use interactively, with good docstrings and auto-completion

  • Nicely handle headers and indexes (including multi-level headers and merged cells)

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

  • Allow storing different user credentials or using Service Accounts

  • Automatically handle token refreshes

  • Enable handling of frozen rows and columns

  • Enable filling in all merged cells when pulling data

  • Nicely handle large data sets and auto-retries

  • Enable creation of filters

  • Handle retries when exceeding 100 second user quota

  • When pushing DataFrames with MultiIndex columns, allow merging or flattening headers

  • Ability to handle Spreadsheet permissions

  • Ability to specify ValueInputOption and ValueRenderOption for specific columns

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 client ID (if you want to use your own account or enable the use of multiple accounts) or Service account key (if you prefer to have a service account interacting with spreadsheets).

  • If you select OAuth client ID:

    • Select Application type item as Other and give it a name.

    • Click on Create button.

    • Click on Download JSON icon on the right side of created OAuth client IDs and store the downloaded file on your file system.

  • If you select Service account key

    • Click on Service account dropdown and select New service account

    • Give it a Service account name and ignore the Role dropdown (unless you know you need this for something else, it’s not necessary for working with spreadsheets)

    • Note the Service account ID as you might need to give that user permission to interact with your spreadsheets

    • Leave Key type as JSON

    • Click Create 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; Move the 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.

You can read more about it in the configuration docs including how to change the default behavior.

Example

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 Spreadsheet')
# This will ask to authenticate if you haven't done so before

# 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('A1', 'A1', ['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()
# 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(sheet['id'], client=client))

Troubleshooting

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-3.0.0.tar.gz (27.9 kB view details)

Uploaded Source

Built Distribution

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

gspread_pandas-3.0.0-py2.py3-none-any.whl (26.4 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: gspread-pandas-3.0.0.tar.gz
  • Upload date:
  • Size: 27.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for gspread-pandas-3.0.0.tar.gz
Algorithm Hash digest
SHA256 d4bc20239245eb2bd6920512e941ab6a6ed12f6b030d3e0711175d69613a8ce7
MD5 366ee343f6f80ee72695729b380c1e4d
BLAKE2b-256 da6ea815ef40f1e2d82b49329f336c4ee08cbbd7840d148885222be377628afe

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gspread_pandas-3.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 26.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.0.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.9

File hashes

Hashes for gspread_pandas-3.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3758805e32cfd8d01247c9361ec0ab136d526d838e0129df64f2ab6ba0dc14e3
MD5 b97587841bf53d4d9fc84b3a54af146b
BLAKE2b-256 85e913cbf128b699c16b9205ade97c29c9264329c3b1122f423425997cdbe922

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