gspread - Google Spreadsheets Python API
raw JSON → 6.2.1 verified Tue May 12 auth: no python install: verified quickstart: verified
gspread is a Python API for Google Sheets, providing a simple interface to interact with spreadsheets. It supports Google Sheets API v4 and offers features like opening spreadsheets by title, key or URL, reading, writing, and formatting cell ranges, sharing, access control, and batching updates. It is currently at version 6.2.1 and is actively maintained with frequent minor and patch releases.
pip install gspread Common errors
error google.auth.exceptions.RefreshError: ('invalid_grant: Token has been expired or revoked.', {'error': 'invalid_grant', 'error_description': 'Token has been expired or revoked.'}) ↓
cause This error typically means your OAuth 2.0 client credentials (specifically the refresh token) have expired, been revoked, or are otherwise invalid, often due to an outdated `authorized_user.json` file or the user revoking access from their Google account.
fix
Delete the
authorized_user.json file (on Windows: %APPDATA%\gspread\, on other systems: ~/.config/gspread/credentials.json) and re-run your code to initiate a new authentication flow. error gspread.exceptions.APIError: {'code': 403, 'message': 'The caller does not have permission', 'status': 'PERMISSION_DENIED'} ↓
cause This error occurs when the service account or authenticated user lacks the necessary permissions (e.g., editor access) to interact with the target Google Sheet or Drive API.
fix
Share the Google Sheet directly with the service account's email address (found in your service account JSON key file) and grant it editor permissions, or ensure the authenticated user has sufficient access to the sheet.
error gspread.exceptions.SpreadsheetNotFound: {'code': 404, 'message': 'Requested entity was not found.'} ↓
cause The specified Google Sheet could not be found, often due to an incorrect spreadsheet title, key, or URL, or because the service account/user does not have access to the spreadsheet.
fix
Double-check the spreadsheet title, key, or URL for typos, and ensure the Google Sheet is shared with the service account's email address.
error AttributeError: 'Worksheet' object has no attribute 'update' ↓
cause This error usually indicates that the `gspread` library version is outdated, and the `update()` method (or similar methods like `insert_rows()`) that you are trying to use was not available in that older version.
fix
Upgrade
gspread to the latest version using pip install --upgrade gspread to access the newer API methods. Warnings
breaking Python 3.7 End-of-Life: gspread v6+ officially drops support for Python 3.7. Your project must use Python 3.8 or newer. ↓
fix Upgrade your Python environment to 3.8 or a later version.
breaking Worksheet.update() arguments swapped and value format changed. The `values` and `range_name` arguments are swapped. Additionally, `values` must now be a 2D array (list of lists), not a simple list. ↓
fix Either swap the arguments (`worksheet.update(values, range_name)`) or, preferably, use named arguments (`worksheet.update(values=..., range_name=...)`) for clarity and forward compatibility. Ensure `values` is always a list of lists.
breaking Worksheet.get_records() method removed. This method is no longer available in v6. ↓
fix Use `worksheet.get_all_records()` to retrieve all records or fetch specific ranges with `worksheet.get()` and process them using `gspread.utils.to_records(header, cells)` if partial records are needed.
breaking Color representation for formatting changed from dictionary to hexadecimal strings. ↓
fix Convert color dictionaries to hexadecimal strings (e.g., `{'red': 1, 'green': 0.5, 'blue': 1}` to `"#FF7FFF"`). A utility function `gspread.utils.convert_colors_to_hex_value()` is available for compatibility.
gotcha SpreadsheetNotFound error (or similar access issues) when using a Service Account. ↓
fix You *must* share the target Google Sheet with the `client_email` address found in your service account's JSON key file. Treat it like sharing with any other Google user.
gotcha Google Sheets API rate limits can lead to `gspread.exceptions.APIError: 429 RESOURCE_EXHAUSTED`. ↓
fix The Sheets API has limits (e.g., 100 requests/100 seconds per project, 60 requests/60 seconds per user). Minimize API calls by using batch operations (`update()`, `batch_update()`, `get_all_values()`, `batch_get()`) instead of individual cell/row operations in loops. Consider using `gspread.http_client.BackOffHTTPClient` for automatic retry with exponential back-off.
gotcha OAuth Client ID authentication sometimes results in `google.auth.exceptions.RefreshError: invalid_grant: Token has been expired or revoked.` ↓
fix This usually means the `authorized_user.json` credentials have expired. Delete the `authorized_user.json` file (located in `~/.config/gspread/` on Linux/macOS or `%APPDATA%\gspread\` on Windows) and re-run your code to initiate a new authentication flow.
gotcha Service account key file not found. This error indicates that the gspread client could not locate the specified service account JSON key file, preventing authentication. ↓
fix Ensure that the path to your service account JSON key file (e.g., `./path/to/your/service_account.json`) is correct and that the file exists at that location. Double-check file permissions if the path is correct but the file is inaccessible by the running process. The path is typically provided during client authorization (e.g., `gspread.service_account(filename='path/to/key.json')`).
gotcha Service account key file not found at the specified path during initialization. ↓
fix Verify that the JSON key file for your service account exists at the path you are providing (e.g., to `gspread.service_account(filename='path/to/key.json')`) or that the `GSPREAD_SERVICE_ACCOUNT_PATH` environment variable points to a valid file. Double-check the filename and directory path for typos and correct permissions.
Install compatibility verified last tested: 2026-05-12
python os / libc status wheel install import disk
3.10 alpine (musl) wheel - 1.26s 44.7M
3.10 alpine (musl) - - 1.21s 43.6M
3.10 slim (glibc) wheel 3.8s 0.92s 45M
3.10 slim (glibc) - - 0.92s 44M
3.11 alpine (musl) wheel - 1.54s 48.1M
3.11 alpine (musl) - - 1.72s 47.0M
3.11 slim (glibc) wheel 3.6s 1.40s 49M
3.11 slim (glibc) - - 1.31s 47M
3.12 alpine (musl) wheel - 1.57s 39.7M
3.12 alpine (musl) - - 1.71s 38.5M
3.12 slim (glibc) wheel 3.2s 1.66s 40M
3.12 slim (glibc) - - 1.63s 39M
3.13 alpine (musl) wheel - 1.63s 39.4M
3.13 alpine (musl) - - 1.64s 38.2M
3.13 slim (glibc) wheel 3.3s 1.63s 40M
3.13 slim (glibc) - - 1.67s 39M
3.9 alpine (musl) wheel - 1.18s 44.7M
3.9 alpine (musl) - - 1.12s 43.7M
3.9 slim (glibc) wheel 4.5s 1.10s 45M
3.9 slim (glibc) - - 0.98s 44M
Imports
- gspread
import gspread - gspread.service_account wrong
gc = gspread.authorize(credentials)correctimport gspread gc = gspread.service_account() - gspread.oauth
import gspread gc = gspread.oauth() - gspread.api_key
import gspread gc = gspread.api_key('YOUR_API_KEY') - gspread.exceptions.APIError
from gspread.exceptions import APIError
Quickstart verified last tested: 2026-04-24
import gspread
import os
# Ensure your service account key file path is set as an environment variable
# or replace with the actual path.
# For example: export GSPREAD_SERVICE_ACCOUNT_KEYFILE="./path/to/your/service_account.json"
SERVICE_ACCOUNT_KEYFILE = os.environ.get(
'GSPREAD_SERVICE_ACCOUNT_KEYFILE',
'./path/to/your/service_account.json' # Placeholder, replace or use env var
)
try:
# Authenticate using a service account
# Make sure to share your Google Sheet with the service account email address.
gc = gspread.service_account(filename=SERVICE_ACCOUNT_KEYFILE)
# Open a spreadsheet by its title
spreadsheet_title = "My Test Spreadsheet"
sh = gc.open(spreadsheet_title)
# Select the first worksheet
wks = sh.sheet1
print(f"Successfully opened spreadsheet: {sh.title}")
print(f"First worksheet title: {wks.title}")
# Read a single cell value
cell_a1 = wks.acell('A1').value
print(f"Value in A1: {cell_a1}")
# Update a single cell
wks.update_acell('B1', 'Hello gspread!')
print("Updated cell B1.")
# Update a range of cells (using v6 syntax with 2D array and named args)
data_to_write = [['Name', 'Age'], ['Alice', 30], ['Bob', 24]]
wks.update(values=data_to_write, range_name='A3')
print("Updated range A3:B5.")
# Get all values from the worksheet as a list of lists
all_values = wks.get_all_values()
print("\nAll values in the worksheet:")
for row in all_values:
print(row)
except FileNotFoundError:
print(f"Error: Service account key file not found at {SERVICE_ACCOUNT_KEYFILE}. ")
print("Please ensure the file exists and the path is correct.")
except gspread.exceptions.SpreadsheetNotFound:
print(f"Error: Spreadsheet '{spreadsheet_title}' not found or not shared with the service account.")
print("Ensure the spreadsheet name is correct and shared with the client_email from your service account JSON.")
except gspread.exceptions.APIError as e:
print(f"Google Sheets API Error: {e}")
print("This might be a rate limit issue or incorrect API permissions.")
except Exception as e:
print(f"An unexpected error occurred: {e}")