A Python library for automating Google Sheets operations with pandas DataFrame integration. Easily read from and write to Google Sheets tables using familiar DataFrame syntax.
- Simple DataFrame Integration: Read Google Sheets data directly into pandas DataFrames
- Easy Data Writing: Write DataFrames to Google Sheets with automatic formatting
- Automatic Sheet Management: Create new sheets automatically when needed
- Multiple Credentials Support: Handle multiple service account credentials with automatic rotation
- Robust Error Handling: Comprehensive logging and exception handling for reliable operations
pip install .- Python 3.8+
- Google Cloud Project with Sheets API enabled
- Service account credentials (JSON format)
Create a .credentials folder in your project root and place your Google service account JSON files there:
your_project/
├── .credentials/
│ ├── service-account-1.json
│ └── service-account-2.json
└── your_script.py
Alternatively, set the GOOGLE_SHEETS_CREDENTIALS_PATH environment variable:
export GOOGLE_SHEETS_CREDENTIALS_PATH=/path/to/your/credentials/folder- Go to Google Cloud Console
- Create a new service account
- Download the JSON key file
- Place it in your
.credentialsfolder
from au_sheets import get_df, update_sheetimport pandas as pd
from au_sheets import get_df
# Read data from an existing sheet
df = get_df(table_name="My Spreadsheet", sheet_name="Sheet1")
if df is not None:
print(df.head())
# Create sheet if it doesn't exist
df = get_df(table_name="My Spreadsheet", sheet_name="New Sheet", create_sheet=True)import pandas as pd
from au_sheets import update_sheet
# Create sample DataFrame
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'London', 'Tokyo']
}
df = pd.DataFrame(data)
# Update existing sheet
update_sheet(df, table_name="My Spreadsheet", sheet_name="Sheet1")
# Create new sheet and update
update_sheet(df, table_name="My Spreadsheet", sheet_name="New Data", create_sheet=True)Retrieves data from a Google Sheet and returns it as a pandas DataFrame.
Parameters:
table_name(str): Name of the Google Spreadsheetsheet_name(str): Name of the worksheet within the spreadsheetcreate_sheet(bool, optional): Create the sheet if it doesn't exist. Default: False
Returns:
DataFrame | None: DataFrame containing the sheet data, or None if an error occurs
Example:
df = get_df("Sales Data", "Q4 2024")Updates a Google Sheet with DataFrame values, replacing all existing content.
Parameters:
df(DataFrame): pandas DataFrame to write to the sheettable_name(str): Name of the Google Spreadsheetsheet_name(str): Name of the worksheet within the spreadsheetcreate_sheet(bool, optional): Create the sheet if it doesn't exist. Default: False
Example:
update_sheet(my_dataframe, "Sales Data", "Q4 2024", create_sheet=True)The library includes comprehensive error handling and logging:
- Connection Issues: Handles network connectivity problems
- Authentication Errors: Manages credential and permission issues
- Sheet Not Found: Gracefully handles missing spreadsheets or worksheets
- Data Validation: Validates DataFrame input and handles empty datasets
All errors are logged using Python's logging module. Enable debug logging to see detailed operation information:
import logging
logging.basicConfig(level=logging.DEBUG)If you have multiple service account files in your credentials folder, the library will automatically rotate between them to handle API rate limits and quota restrictions.
pandas: DataFrame operationsgspread: Google Sheets API interactionoauth2client: Authentication handlingnumpy: Data processing support
This project is open source. Please check the license file for details.
Contributions are welcome! Please feel free to submit pull requests or open issues for bugs and feature requests.