Skip to main content

A Pydantic integration for Google Sheets

Project description

pydantic-gsheets

A Python library for sending and receiving data from Google Sheets using Pydantic models.

Features

  • Type-safe data mapping — Convert Google Sheets rows into strongly typed Pydantic models.
  • Validation — Ensure incoming data meets schema requirements before processing.
  • Serialization — Write validated models back to Sheets easily.
  • Batch operations — Read and write large ranges in a single request.
  • Built on official Google Sheets API — Reliable and well-maintained foundation.

Usage

Example: Inventory sheet with typed rows and optional Drive image download

This example shows how to:

  • authorize with User OAuth
  • map a sheet to a typed Pydantic row model
  • stream rows and (optionally) pre-download Drive images
  • update a row and append a new one
from typing import Annotated, Optional
from pydantic_gsheets import (
    GoogleWorkSheet, SheetRow,
    GSIndex, GSRequired, GSParse, 
    GSFormat, DriveFile, GSDrive, 
    get_drive_service, AuthConfig, 
    AuthMethod, get_sheets_service
)

# --- Auth (User OAuth). Make sure your OAuth client and consent screen are set up.
sheets = get_sheets_service(AuthConfig(
    method=AuthMethod.USER_OAUTH,
    client_secrets_file="client_secret.json",
    token_cache_file=".tokens/sheets_token.json",
))
# For Drive-backed columns (optional), you also need a Drive client (same auth).
drive = get_drive_service(AuthConfig(
    method=AuthMethod.USER_OAUTH,
    client_secrets_file="client_secret.json",
    token_cache_file=".tokens/sheets_token.json",
))

# --- Small parsers for typed columns
def parse_bool(v):
    s = str(v).strip().lower()
    if s in ("true", "1", "yes", "y"): return True
    if s in ("false", "0", "no", "n"): return False
    raise ValueError(f"Not a bool: {v}")

def parse_float(v):
    return float(str(v).replace(",", "."))

# --- Define your sheet row model (adjust GSIndex to match your columns)
class InventoryRow(SheetRow):
    sku:      Annotated[str,   GSIndex(0), GSRequired()]
    name:     Annotated[str,   GSIndex(1), GSRequired()]
    price:    Annotated[float, GSIndex(2), GSParse(parse_float), GSFormat("NUMBER", "0.00")]
    in_stock: Annotated[bool,  GSIndex(3), GSParse(parse_bool)]
    photo:    Annotated[
        Optional[DriveFile],
        GSIndex(4),
        # If the cell contains a Drive URL (or an =IMAGE("...drive...")), predownload it:
        GSDrive(
            predownload=True,
            dest_dir="downloads/photos",
            filename_template="{row}_{field}_{id}.{ext}",  # e.g., 12_photo_1AbCdEf.png
            export_mime=None,     # set e.g. "image/png" for Google Drawings export
            overwrite=False,
        )
    ]

# --- Open a worksheet bound to your model
sheet = GoogleWorkSheet(
    model=InventoryRow,               # the row type for this sheet
    service=sheets,                   # Sheets API client
    spreadsheet_id="<YOUR-SPREADSHEET-ID>",
    sheet_name="Inventory",
    start_row=2,                      # data starts at row 2 (headers on row 1)
    has_headers=True,
    drive_service=drive,              # optional; enables GSDrive(predownload=...)
)

# --- Read and iterate typed rows
for row in sheet.read_rows():
    print(row.sku, row.name, row.price, row.in_stock,
          getattr(row.photo, "local_path", None))  # path if predownloaded

# --- Update an existing row and save it back
first = next(sheet.read_rows())
first.in_stock = False
first.save()  # writes the row back to the same line

# --- Append a brand-new row
new_item = InventoryRow(
    sku="SKU-12345",
    name="Widget Mini",
    price=19.99,
    in_stock=True,
    photo=None,  # or a Drive URL in the cell — it will be parsed on next read
)
sheet.append_row(new_item)  # binds new_item to its newly created row number

# --- (Optional) apply number/date formats defined via GSFormat annotations
sheet.apply_formats_for_model(InventoryRow)

Notes

  • Column indices: GSIndex(0) is the first logical column of your data region (i.e., relative to start_column). Adjust to match your sheet.
  • Scopes: if you use DriveFile (with GSDrive), your OAuth scopes must include Drive read access (e.g., drive.readonly) in addition to Sheets.
  • Predownload: GSDrive(predownload=True) will download files at read time if drive_service is provided. If you omit drive_service, the field still parses as a DriveFile, but no auto-download happens.
  • Readonly cells: fields marked with GSReadonly() will never be overwritten by save()/append_row().
  • Formatting: GSFormat sets Google Sheets column number formats through apply_formats_for_model(...) – run it once after creating/binding a sheet.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_gsheets-0.0.4.tar.gz (25.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_gsheets-0.0.4-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_gsheets-0.0.4.tar.gz.

File metadata

  • Download URL: pydantic_gsheets-0.0.4.tar.gz
  • Upload date:
  • Size: 25.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pydantic_gsheets-0.0.4.tar.gz
Algorithm Hash digest
SHA256 954cdceb9568273778f83ececea8c192670fbf4a765a20509792d19ce823c256
MD5 c95421f41e7374f552e656ab95b861bc
BLAKE2b-256 175d82b9b39de6c1343c63650902d4d3205ec23f3f0159dfd0fa3c958c79e396

See more details on using hashes here.

File details

Details for the file pydantic_gsheets-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_gsheets-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2d7dcdd4b8978861341925dd738e8ac3f5a57150320837ca5c67d29aa970e9ef
MD5 285b001e7f76dbb03945ba1418885635
BLAKE2b-256 bf1306f9505e14bdb71fcb6417f751845621aabbe46662dd630014469b3bc39e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page