gspread-pandas
gspread-pandas is a Python package that simplifies interaction between Pandas DataFrames and Google Spreadsheets. It leverages the `gspread` library for underlying API communication and adds extensive functionality for handling DataFrame-specific operations, multi-level headers, merged cells, and authentication. The library is actively maintained with regular updates and releases.
Warnings
- breaking Version 3.0.0 removed support for Python 2.7. Users must migrate to Python 3.x (minimum Python 3.5, though newer versions are recommended).
- breaking Version 3.0.0 upgraded the internal `gspread` dependency to version `5>=`. This might introduce breaking changes if your project relied on specific `gspread` APIs that changed between its major versions.
- deprecated The `raw_column_names` parameter was removed in version 3.0.2. Code using this parameter will break.
- gotcha When uploading large DataFrames, you might encounter Google Sheets API limits (e.g., max number of cells in a worksheet). If `df_to_sheet` adds rows/columns by default, it might exceed limits.
- gotcha Authentication requires setting up Google Cloud Project credentials (Service Account or OAuth Client ID) and placing the `google_secret.json` file in `~/.config/gspread_pandas/` (or `%APPDATA%\gspread_pandas` on Windows) by default. Without this, operations will fail.
- gotcha Internal `pandas` operations no longer use `inplace=True` as of version 3.2.1 to avoid `SettingWithCopyWarning`. If your code implicitly relied on previous in-place modifications (which were often ambiguous) or suppressed these warnings, behavior might change.
Install
-
pip install gspread-pandas
Imports
- Spread
from gspread_pandas import Spread
- Client
from gspread_pandas import Client
Quickstart
import pandas as pd
import os
from gspread_pandas import Spread
# IMPORTANT: Ensure your Google client credentials (e.g., service account JSON)
# are set up at ~/.config/gspread_pandas/google_secret.json or via environment variables.
# See documentation for detailed authentication setup.
# Replace with your actual spreadsheet name, or fetch from environment
spreadsheet_name = os.environ.get('GSPREAD_PANDAS_SHEET_NAME', 'My Example Spreadsheet')
# Create a dummy DataFrame to write to the spreadsheet
data = {'col1': [1, 2, 3], 'col2': ['A', 'B', 'C']}
df_to_write = pd.DataFrame(data)
try:
# Open the spreadsheet (or create if it doesn't exist and create_spread=True)
# By default, it looks for ~/.config/gspread_pandas/google_secret.json
spread = Spread(spreadsheet_name, create_spread=True)
# Get a list of existing worksheets
print(f"Worksheets in '{spreadsheet_name}': {spread.sheets}")
# Write DataFrame to a new worksheet or an existing one
# Using 'overwrite=True' to clear existing data, 'sheet=' to specify sheet name
sheet_name = 'DataFrame Data'
print(f"Writing DataFrame to sheet: '{sheet_name}'")
spread.df_to_sheet(df_to_write, sheet=sheet_name, index=False, overwrite=True)
# Read data back into a DataFrame
df_from_sheet = spread.sheet_to_df(sheet=sheet_name)
print(f"\nData read from '{sheet_name}':")
print(df_from_sheet.head())
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure your Google API credentials are correctly configured and the spreadsheet exists/is accessible.")