Skip to main content

Lightweight helpers for Google Drive and Google Sheets with unified credential resolution.

Project description

toolsgdrivegsheet

Lightweight helpers for Google Drive and Google Sheets with unified credential resolution.

Credentials are resolved once and shared across both APIs — no duplicate file reads or token refreshes.

Install

pip install toolsgdrivegsheet

Quick start

from toolsgdrivegsheet import get_services

drive, sheets = get_services(path_keyfile="~/key.json")

Or individually:

from toolsgdrivegsheet import gdrive_get_service, gsheet_get_service

drive  = gdrive_get_service()
sheets = gsheet_get_service()

Authentication

All service constructors (gdrive_get_service, gsheet_get_service, get_services) accept the same credential options. Resolution order:

  1. credentials — pre-resolved credentials object (e.g. from toolsbq via bq_client._credentials)
  2. keyfile_json — SA key as dict
  3. path_keyfile — path to SA JSON file (supports ~ and $HOME expansion)
  4. GOOGLE_APPLICATION_CREDENTIALS env var
  5. Local RAM-ADC fast path (macOS ramdisk / Linux /dev/shm)
  6. ADC fallback (Cloud Run metadata, gcloud auth application-default login, etc.)
from toolsgdrivegsheet import gdrive_get_service

# ADC (default)
drive = gdrive_get_service()

# Service account file
drive = gdrive_get_service(path_keyfile="~/.config/gcloud/sa-keys/key.json")

# Service account info dict (e.g. from CI secret)
drive = gdrive_get_service(keyfile_json={"type": "service_account", "...": "..."})

# Reuse credentials from toolsbq
from toolsbq import bq_get_client
bq = bq_get_client(path_keyfile="~/key.json")
drive = gdrive_get_service(credentials=bq._credentials)

Note: The service account must have access to the target Drive folders and spreadsheets. Share them with the service account email.

Credential reuse

get_credentials() is exposed for when you need the raw credentials object:

from toolsgdrivegsheet import get_credentials, gdrive_get_service, gsheet_get_service

creds = get_credentials(path_keyfile="~/key.json")
drive  = gdrive_get_service(credentials=creds)    # no file re-read
sheets = gsheet_get_service(credentials=creds)     # no file re-read

Or use the convenience function:

from toolsgdrivegsheet import get_services

drive, sheets = get_services(path_keyfile="~/key.json")

Drive API

from toolsgdrivegsheet import gdrive_get_service, list_files, download_file_to_disk, download_file_to_csv

drive = gdrive_get_service(path_keyfile="~/key.json")

List files

# All files (up to 200)
files = list_files(drive)

# CSV files in a specific folder, modified after a date
files = list_files(
    drive,
    q_filter="mimeType='text/csv' and trashed=False and '<FOLDER_ID>' in parents and modifiedTime > '2024-01-01'",
    limit_pull=50,
)

for f in files:
    print(f["name"], f["modifiedTime"], f["id"])

Download files

# To disk
download_file_to_disk(drive, file_id="abc123", filename_path="/tmp/report.csv")

# To CSV DictReader (in memory)
reader = download_file_to_csv(drive, file_id="abc123")
if reader:
    for row in reader:
        print(row)

Drive API reference

Function Description
gdrive_get_service(...) Build Drive v3 service object
list_files(service, q_filter, fields, limit_pull, sort_order) List files with optional filtering/sorting
download_file(service, file_id) Download file → BytesIO
download_file_to_disk(service, file_id, filename_path) Download file → save to disk
download_file_to_csv(service, file_id, decoding, delimiter) Download CSV → DictReader

Sheets API

from toolsgdrivegsheet import gsheet_get_service, list_sheet_names, read_sheet, read_sheet_as_dicts

sheets = gsheet_get_service(path_keyfile="~/key.json")
spreadsheet_id = "your_spreadsheet_id"

List sheet names

names = list_sheet_names(sheets, spreadsheet_id)
print(names)  # ["Sheet1", "Sheet2", "Config"]

Read a sheet

# Raw rows (list of lists)
rows = read_sheet(sheets, spreadsheet_id, sheet_range="Sheet1!A1:Z100")

# As list of dicts (first row = headers)
data = read_sheet_as_dicts(sheets, spreadsheet_id, sheet_range="Sheet1")
for row in data:
    print(row)

Read all sheets

from toolsgdrivegsheet import read_all_sheets

all_data = read_all_sheets(sheets, spreadsheet_id)
for sheet_name, rows in all_data.items():
    print(f"{sheet_name}: {len(rows)} rows")

Sheets: read-only vs. read-write

# Read-only (default)
sheets = gsheet_get_service(readonly=True)

# Read-write (for updating cells)
sheets = gsheet_get_service(readonly=False)

Sheets API reference

Function Description
gsheet_get_service(readonly=True, ...) Build Sheets v4 service object
list_sheet_names(service, spreadsheet_id) Get all tab names
read_sheet(service, spreadsheet_id, sheet_range) Read range → list of lists
read_sheet_as_dicts(service, spreadsheet_id, sheet_range) Read range → list of dicts (header row = keys)
read_all_sheets(service, spreadsheet_id, sheet_range_data) Read all tabs → dict of sheet name → rows

Enable APIs

Make sure these APIs are enabled in your GCP project:

Security notes

  • Avoid committing service account keyfiles to git.
  • Prefer keyfile_json sourced from a secure secret store (CI secrets, vault, etc.).

Development

python -m pip install --upgrade build twine
python -m build
twine check dist/*
twine upload dist/*

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

toolsgdrivegsheet-0.1.1.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

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

toolsgdrivegsheet-0.1.1-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file toolsgdrivegsheet-0.1.1.tar.gz.

File metadata

  • Download URL: toolsgdrivegsheet-0.1.1.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for toolsgdrivegsheet-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4f2548197921bff354cc9a0ff385749ea82746fcbaeaa2651846bb681781a753
MD5 7693091efed1387ba64bc7837ca9efbc
BLAKE2b-256 3c03bfd7e3d064c5ccb9c94de44a89be42e7ce38c43859dbc68c67d0d14fe0b2

See more details on using hashes here.

File details

Details for the file toolsgdrivegsheet-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for toolsgdrivegsheet-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 798a7300def4472c6cebf5bae371f30a9de686c8cbebaee746cb327e6156d9c3
MD5 ebfaeb5faa5cb293bbe59dbc6cf1caf9
BLAKE2b-256 6fe54a488c1f95324c4c901c78eb20ed9dc8acc011667ec3859a0e90b80ff084

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