Download and upload pandas dataframes to the Google sheets
Project description
Google sheet & Pandas intergation
Package gheet-pandas
allows you to easily get Pandas dataframe from Google Sheets or upload dataframe to the Sheets.
Installation
Install using pip:
pip install gsheet-pandas
Set up environment
Enable the API
Before using Google APIs, you need to turn them on in a Google Cloud project. You can turn on one or more APIs in a single Google Cloud project. In the Google Cloud console, enable the Google Sheets API.
Enable the API
Authorize credentials for a desktop application
To authenticate as an end user and access user data in your app, you need to create one or more OAuth 2.0 Client IDs. A client ID is used to identify a single app to Google's OAuth servers. If your app runs on multiple platforms, you must create a separate client ID for each platform.
- In the Google Cloud console, go to Menu > APIs & Services > Credentials.
- Go to Credentials
- Click Create Credentials > OAuth client ID.
- Click Application type > Desktop app.
- In the Name field, type a name for the credential. This name is only shown in the Google Cloud console.
- Click Create. The OAuth client created screen appears, showing your new Client ID and Client secret.
- Click OK. The newly created credential appears under OAuth 2.0 Client IDs.
- Save the downloaded JSON file as
credentials.json
, and move the file to your working directory.
Usage
Pandas extension
First, call setup
method to register your credentials and initialize pandas extensions:
from pathlib import Path
import gsheet_pandas
secret_path = Path('/path/to/my/secrets/').resolve()
gsheet_pandas.setup(credentials_dir=secret_path / 'credentials.json',
token_dir=secret_path / 'token.json')
To download dataframe:
import pandas as pd
df = pd.from_gsheet(spreadsheet_id,
sheet_name=sheet_name,
range_name='!A1:C100') # Range in Sheets; Optional
Default range_name
is '!A1:ZZ900000'
.
To upload dataframe:
df.to_gsheet(spreadsheet_id,
sheet_name=sheet_name,
range_name='!B1:ZZ900000', # Range in Sheets; Optional
drop_columns=False) # Upload column names or not; Optional
DriveConnection instance
First, init DriveConnection instance:
from gsheet_pandas import DriveConnection
secret_path = Path('/path/to/my/secrets/').resolve()
drive = DriveConnection(credentials_dir=secret_path / 'credentials.json',
token_dir=secret_path / 'token.json')
To download dataframe:
df = drive.download(spreadsheet_id,
sheet_name=sheet_name,
range_name='!A1:C100', # Range in Sheets; Optional
header=0) # Column row
Default range_name
is '!A1:ZZ900000'
.
To upload dataframe:
df = drive.upload(df,
spreadsheet_id,
sheet_name=sheet_name,
range_name='!B1:ZZ900000', # Range in Sheets; Optional
drop_columns=False) # Upload column names or not; Optional
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for gsheet_pandas-0.2.8-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f69ba17b7795c7711c211b16a69a1d56a70daa200da43b2f00fe76550dd78b0 |
|
MD5 | 3bfaeacff32b7248ce99a9a9c51b6cf4 |
|
BLAKE2b-256 | 07d64a0ecc84fe22f92266d19bf4364455040d8fad882d2970c80ae064b6f6aa |