Skip to main content

SQLAlchemy dialect for Excel files — use Excel as a database

Project description

sqlalchemy-excel sqlalchemy-excel

CI codecov PyPI Python 3.10+ License: MIT

SQLAlchemy dialect for Excel files — use Excel as a database.

from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import DeclarativeBase, Session, Mapped, mapped_column

engine = create_engine("excel:///data.xlsx")

class Base(DeclarativeBase):
    pass

class User(Base):
    __tablename__ = "Sheet1"
    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str] = mapped_column()

Base.metadata.create_all(engine)

with Session(engine) as session:
    session.add(User(id=1, name="Alice"))
    session.commit()

with Session(engine) as session:
    users = session.query(User).all()

Installation

pip install sqlalchemy-excel

excel-dbapi is automatically installed as a dependency.

URL Format

# Relative path
engine = create_engine("excel:///data.xlsx")

# Absolute path (note four slashes)
engine = create_engine("excel:////home/user/data.xlsx")

# With engine options
engine = create_engine("excel:///data.xlsx", connect_args={"engine": "openpyxl"})

Remote Excel via Microsoft Graph API

Access Excel files on OneDrive/SharePoint directly:

pip install sqlalchemy-excel[graph]
from sqlalchemy import create_engine
from azure.identity import DefaultAzureCredential

engine = create_engine(
    "excel+graph:///drive_id/item_id",
    connect_args={"credential": DefaultAzureCredential()},
)

with engine.connect() as conn:
    result = conn.execute(text("SELECT * FROM Sheet1"))
    for row in result:
        print(row)

URL format: excel+graph:///drive_id/item_id where drive_id and item_id are Microsoft Graph resource identifiers. Query parameters: ?readonly=false to enable write operations.

Features

  • Full SQLAlchemy 2.0 dialect
  • PEP 249 DB-API 2.0 compliant driver (excel-dbapi)
  • SELECT with WHERE, ORDER BY, LIMIT
  • INSERT, UPDATE, DELETE
  • CREATE TABLE / DROP TABLE with metadata tracking
  • IN, BETWEEN, LIKE operators in WHERE clauses
  • ORM support with DeclarativeBase
  • Schema inspection (get_table_names, get_columns, has_table)
  • Type mapping: String, Integer, Float, Boolean, Date, DateTime

Type Mapping

SQLAlchemy Type Excel Storage Notes
String, Text, VARCHAR, CHAR TEXT All string types map to TEXT
Integer, SmallInteger, BigInteger INTEGER All integer types map to INTEGER
Float, Numeric, Decimal FLOAT All numeric types map to FLOAT
Boolean BOOLEAN
Date DATE
DateTime, TIMESTAMP DATETIME
Time TEXT Stored as text
Uuid TEXT Stored as text

BLOB, BINARY, JSON, and ARRAY types are not supported and will raise CompileError.

ORM Examples

Define a Model

from sqlalchemy import create_engine
from sqlalchemy.orm import DeclarativeBase, Session, Mapped, mapped_column

engine = create_engine("excel:///data.xlsx")

class Base(DeclarativeBase):
    pass

class User(Base):
    __tablename__ = "users"
    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str] = mapped_column()
    age: Mapped[int] = mapped_column()

Base.metadata.create_all(engine)

Insert

with Session(engine) as session:
    session.add(User(id=1, name="Alice", age=30))
    session.add(User(id=2, name="Bob", age=25))
    session.commit()

Query with Filters

from sqlalchemy import select

with Session(engine) as session:
    # Basic query
    users = session.query(User).all()

    # WHERE clause
    user = session.query(User).filter(User.name == "Alice").first()

    # IN operator
    stmt = select(User).where(User.name.in_(["Alice", "Bob"]))
    users = session.scalars(stmt).all()

    # BETWEEN operator
    stmt = select(User).where(User.age.between(25, 35))
    users = session.scalars(stmt).all()

    # LIKE operator
    stmt = select(User).where(User.name.like("A%"))
    users = session.scalars(stmt).all()

    # ORDER BY + LIMIT
    stmt = select(User).order_by(User.age.desc()).limit(5)
    users = session.scalars(stmt).all()

Update and Delete

with Session(engine) as session:
    user = session.query(User).filter(User.id == 1).first()
    if user:
        user.name = "Ann"
        session.commit()

with Session(engine) as session:
    user = session.query(User).filter(User.id == 2).first()
    if user:
        session.delete(user)
        session.commit()

Core Usage

from sqlalchemy import create_engine, text

engine = create_engine("excel:///data.xlsx")

with engine.connect() as conn:
    result = conn.execute(text("SELECT * FROM Sheet1"))
    for row in result:
        print(row)

Schema Inspection

from sqlalchemy import create_engine, inspect

engine = create_engine("excel:///data.xlsx")
inspector = inspect(engine)

# List all sheets (tables)
print(inspector.get_table_names())

# Get column info
print(inspector.get_columns("Sheet1"))

# Check if a sheet exists
print(inspector.has_table("Sheet1"))

Limitations

  • No JOIN, GROUP BY, HAVING, DISTINCT, OFFSET
  • No subqueries, CTEs, or aggregate functions
  • No ALTER TABLE, foreign keys, or indexes
  • Single-table operations only
  • No concurrent writes — use a single-writer model
  • Session.rollback() is a no-op — Excel files do not support transactional rollback

Related Projects

  • excel-dbapi — The underlying PEP 249 DB-API 2.0 driver for Excel files.

License

MIT

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

sqlalchemy_excel-0.3.1.tar.gz (30.6 kB view details)

Uploaded Source

Built Distribution

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

sqlalchemy_excel-0.3.1-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_excel-0.3.1.tar.gz.

File metadata

  • Download URL: sqlalchemy_excel-0.3.1.tar.gz
  • Upload date:
  • Size: 30.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sqlalchemy_excel-0.3.1.tar.gz
Algorithm Hash digest
SHA256 a9421a5633c3cfa55fd232dee1d254c3d07b6f402dcb2c33004b7b49016a067b
MD5 54eb990e9859c87043cd5b4715cb7bf2
BLAKE2b-256 4e56ad399335962b3c86bdf795afe59a054da0dd68e1b573c975c6fc4fc34075

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlalchemy_excel-0.3.1.tar.gz:

Publisher: publish-pypi.yml on yeongseon/sqlalchemy-excel

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sqlalchemy_excel-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_excel-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2f32696aafff361e595bc3af435cd5005670e5213276a26055ad16ad56bb6c79
MD5 e7036793ea196a4bd6ee2466abefffe6
BLAKE2b-256 c9298c0e6f6b34f704df4bce2108deaf6dd017639f6212886c088d72a7509e28

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlalchemy_excel-0.3.1-py3-none-any.whl:

Publisher: publish-pypi.yml on yeongseon/sqlalchemy-excel

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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