Skip to main content

A unified database interface for Google Sheets, Excel, and CSV.

Project description

py-sheet-db: Unified Tabular Database

A "plug and play" Python library that treats Google Sheets, Excel files, and CSV directories as relational databases. It features unified caching, schema validation, and multi-format support.

Features

  • Multi-Format Support: One API for Google Sheets, .xlsx, and .csv.
  • Fast CRUD: Uses a local SQLite cache for near-instant operations.
  • No Data Loss: Changes are tracked locally and synchronized via flush().
  • Relational Views: Perform Joins and Filters across multiple sheets or formats.
  • Schema Validation: Enforce data types and required fields across any storage format.

Installation

pip install gspread google-auth pandas openpyxl

Quick Start

PySheetDB can automatically detect the driver or you can set it explicitly using the driver_type parameter.

from py_sheet_db import PySheetDB

# 1. Automatic Detection
db = PySheetDB("data.xlsx")

# 2. Explicit Type Selection ('excel', 'csv', or 'gsheets')
db = PySheetDB("my_data_folder", driver_type='csv')

# 3. Google Sheets (requires credentials.json)
db = PySheetDB("credentials.json", driver_type='gsheets', spreadsheet_id='YOUR_ID')
# Or use the helper:
db = PySheetDB.connect_gsheets("credentials.json")

Schema Validation

Enforce data types and required fields. py-sheet-db will attempt to auto-cast types (e.g., string to int) when possible.

from py_sheet_db import Schema

user_schema = Schema({
    'id': int,
    'name': str,
    'email': str,
    'balance': float
})

users = db.table('Users', schema=user_schema)
users.insert({'id': 1, 'name': 'Alice', 'email': 'alice@example.com', 'balance': 50.5})

Relational Views (Joins & Filters)

You can create virtual views that merge data from multiple tables using a SQL-like interface powered by Pandas.

# 1. Define your tables
orders = db.table('Orders')
products = db.table('Products')

# 2. Create a View
# Join Orders and Products on 'product_id'
order_details = db.view('OrderDetails', [orders, products]) \
    .join(orders, products, on='product_id', how='left') \
    .filter(lambda df: df[df['status'] == 'completed'])

# 3. Get results as a list of dicts
results = order_details.get_data()

CRUD Operations

# Access a Table
users = db.table('Users')

# Insert
users.insert({'id': 1, 'name': 'Alice'})

# Find (Returns list of dicts)
admin = users.find(role='admin')

# Find One
bob = users.find_one(id=102)

# Flush to source (GSheets/Excel/CSV)
db.flush()

Google Sheets Setup

To use Google Sheets, you need a Service Account JSON key file.

1. Get Google Credentials

  1. Go to the Google Cloud Console.
  2. Create a new project.
  3. Go to APIs & Services > Library and enable:
    • Google Sheets API
    • Google Drive API
  4. Go to APIs & Services > Credentials.
  5. Click Create Credentials > Service Account.
  6. Once created, go to the Keys tab and click Add Key > Create New Key (JSON).
  7. Download the file and rename it to credentials.json in your project folder.

2. Share the Spreadsheet

  • Once you have your service account email (e.g., account-name@project-id.iam.gserviceaccount.com), you must Share your Google Spreadsheet with this email address with "Editor" permissions.
  • Alternatively, if you use db.create_database("Name"), the library creates a new spreadsheet owned by the service account. You may still need to share it with your personal email to see it in your browser.

How it works

  1. Initialization: Data is synced from the source into a hidden local SQLite DB (.db_cache/).
  2. Operations: All CRUD reflects immediately in the fast SQLite cache.
  3. Synchronization: db.flush() (or automatic exit hook) pushes "dirty" rows back to the primary storage.

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

py_sheet_db-0.1.0.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

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

py_sheet_db-0.1.0-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file py_sheet_db-0.1.0.tar.gz.

File metadata

  • Download URL: py_sheet_db-0.1.0.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.6

File hashes

Hashes for py_sheet_db-0.1.0.tar.gz
Algorithm Hash digest
SHA256 61f45a3e051dffe604d4ee5b2919b176047a58de5afa1b5bc904e5ded735fd78
MD5 0cedbe3f4abb738b723dd59919c5d0ef
BLAKE2b-256 a2dba0ccc810a0ae62e7f8f74d34f41c0f05c7f1b1303ad316414a1a51f3565c

See more details on using hashes here.

File details

Details for the file py_sheet_db-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: py_sheet_db-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.6

File hashes

Hashes for py_sheet_db-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 54c589b599b9077ee9e894463d953c55711f5acd4a56358acdf8b940ba27c4ba
MD5 bc963f79feb60f1b8ce4becc3f0cbb22
BLAKE2b-256 b6b2b3ddd90b484484dacd20eb51a9e5d79d6da138c1ef3e2c94ce80eacd68a8

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