Registry / gcp / gspread-formatting

gspread-formatting

JSON →
library1.2.1pypypi✓ verified 27d ago

gspread-formatting provides complete Google Sheets formatting support for gspread worksheets, allowing programmatic control over cell styles, conditional formatting, data validation, and more. The current version is 1.2.1. Releases occur periodically to add new features, fix bugs, and maintain compatibility with the underlying gspread library and Google Sheets API.

pip install gspread-formatting
INSTALL
IMPORT
SIG · GSPREAD-FORMATTING
G
gspread-formatting
gcppythonv1.2.1
Install
3.8s avg
Import
1184ms
Disk
44MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v1.2.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 1.234s · 45.5MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 3.8s · import 1.134s · 46MB
44MB installed
● package 44MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

format_cell_range
✓ from gspread_formatting import format_cell_range
CellFormat
✓ from gspread_formatting import CellFormat
Color
✓ from gspread_formatting import Color
DataValidationRule
✓ from gspread_formatting import DataValidationRule
BooleanCondition
✓ from gspread_formatting import BooleanCondition

This quickstart demonstrates how to initialize a gspread client (required for gspread-formatting), create or open a spreadsheet, define a custom `CellFormat` object, and apply it to a specific cell range using `format_cell_range`.

import gspread from gspread_formatting import format_cell_range, CellFormat, Color import os # --- gspread client setup (outside gspread-formatting scope) --- # Replace with your actual service account file path or other authentication method SERVICE_ACCOUNT_FILE = os.environ.get('GSPREAD_SERVICE_ACCOUNT_FILE', 'path/to/your/service_account.json') try: # Initialize gspread client (service account recommended for automation) gc = gspread.service_account(filename=SERVICE_ACCOUNT_FILE) except Exception as e: print(f"Error initializing gspread client: {e}") print("Please ensure GSPREAD_SERVICE_ACCOUNT_FILE environment variable is set or path is correct.") exit(1) # Open a sheet try: spreadsheet = gc.open('My Formatted Sheet') except gspread.exceptions.SpreadsheetNotFound: spreadsheet = gc.create('My Formatted Sheet') print(f"Created new spreadsheet: {spreadsheet.url}") worksheet = spreadsheet.sheet1 # --- gspread-formatting usage --- # Define a cell format cell_format = CellFormat( backgroundColor=Color(1, 0.9, 0.9), # Light red background textFormat=CellFormat.textFormat( bold=True, foregroundColor=Color(1, 0, 0) ) ) # Apply the format to a range of cells (e.g., A1:C5) format_cell_range(worksheet, 'A1:C5', cell_format) print("Formatted cells A1:C5 with a light red background and bold red text.") print("Check your spreadsheet to see the changes.")
Debug
Known issues
breakinggspread-formatting versions prior to 1.0.0 are not compatible with gspread 5.0.0 and newer. The underlying gspread API changed significantly.
fix
Upgrade gspread-formatting to version 1.0.0 or higher: `pip install --upgrade gspread-formatting`.
affects: <1.0.0
gotchaApplying new formats to a cell range will overwrite any existing formatting for those cells. To preserve specific aspects of existing formatting, you must explicitly include them in the new `CellFormat` object or retrieve the existing format first.
fix
Be mindful that formatting operations are generally 'set' operations. If you need to incrementally update formats, retrieve the current format first, modify it, then reapply. E.g., `get_user_entered_format(worksheet, 'A1')`.
affects: All
gotchaComplex formatting objects (like `CellFormat` or `DataValidationRule`) can be intricate to construct. Missing fields or incorrect values may lead to API errors or unexpected behavior.
fix
Refer to the official Google Sheets API documentation for `CellFormat`, `TextFormat`, `Color`, etc., to understand all available properties and their valid values. Experiment with small examples to verify complex formats before applying them broadly.
affects: All
gotchaThis library relies on `gspread` for authentication and interaction with Google Sheets. Any issues with `gspread` client initialization, permissions, or API quotas will manifest when using `gspread-formatting`.
fix
Ensure your `gspread` client is correctly authenticated and has the necessary permissions for the target spreadsheet. Handle `gspread.exceptions` appropriately for network or permission-related errors. Be aware of Google Sheets API rate limits for extensive formatting operations.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'gspread_formatting'
The 'gspread-formatting' library is not installed in the Python environment where the script is being executed.
fix
Run `pip install gspread-formatting` in your terminal to install the package.
ValueError: values parameter must always be list/tuple of values, even for a single element
When defining a `BooleanCondition` for conditional formatting, the 'values' argument was provided as a single scalar value instead of an iterable (list or tuple) containing the value.
fix
Ensure the 'values' parameter for `BooleanCondition` is always a list or tuple, even if it contains only one element. For example, use `values=[100]` instead of `values=100`.
gspread.exceptions.APIError: {'code': 429, 'message': 'Quota exceeded...', 'status': 'RESOURCE_EXHAUSTED'}
The application has exceeded the Google Sheets API's read or write usage limits (e.g., 60 requests per 60 seconds per user or 300 requests per 60 seconds per project) by making too many individual API calls in a short period.
fix
Consolidate multiple formatting operations into single API calls using batch functions provided by `gspread-formatting`, such as `format_cell_ranges()`, `set_column_widths()`, `set_row_heights()`, and using `ConditionalFormatRule.save()` after modifying rules.
AttributeError: 'Worksheet' object has no attribute 'format_cell_range'
A formatting function from `gspread-formatting` was incorrectly attempted to be called as a method directly on a `gspread.Worksheet` object, instead of being imported and called as a standalone function with the worksheet as an argument.
fix
Import the required formatting functions from `gspread_formatting` (e.g., `from gspread_formatting import format_cell_range`) and then call them, passing your `worksheet` object as an argument, like `format_cell_range(worksheet, 'A1:B1', fmt)`.
Upgrade
Version history
1.2.1latest on PyPI · released Mar 7, 2025
Audit
Dependencies
gspreadrequiredThis library extends gspread functionality; gspread is required for all core operations like authentication and sheet access.
Agent activity
25 hits · last 30 days
node
19
OpenAI (training)
2
Resources
gspread-formatting — pip install gspread-formatting · libregistry