Metadata-Version: 2.3
Name: coasti-xls-io
Version: 0.2.0
Summary: Import Excel files into Postgres, DuckDB and MSSQL with schema validation and before/after preview, and export tables back to Excel.
Keywords: excel,xlsx,import,etl,postgres,duckdb,mssql,sqlalchemy,coasti
Author: Jakob Buergermeister
Author-email: Jakob Buergermeister <jakob.buergermeister@linkfish.eu>
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Database
Classifier: Topic :: Office/Business :: Financial :: Spreadsheet
Requires-Dist: fastexcel>=0.21.0
Requires-Dist: polars>=2.1.0
Requires-Dist: pydantic>=2.14.3
Requires-Dist: python-calamine>=0.9.1
Requires-Dist: sqlalchemy>=2.1.8
Requires-Dist: xlsxwriter>=3.3.1
Requires-Dist: coasti-xls-io[cli,postgres,duckdb,mssql] ; extra == 'all'
Requires-Dist: click>=8.5.1 ; extra == 'cli'
Requires-Dist: eyconf>=0.8.0 ; extra == 'cli'
Requires-Dist: pyyaml>=6.0.3 ; extra == 'cli'
Requires-Dist: rich>=15.0.0 ; extra == 'cli'
Requires-Dist: typer>=0.29.0 ; extra == 'cli'
Requires-Dist: duckdb>=2.0.0 ; extra == 'duckdb'
Requires-Dist: duckdb-engine>=0.17.0 ; extra == 'duckdb'
Requires-Dist: pyarrow>=26.0.1 ; extra == 'duckdb'
Requires-Dist: pyodbc>=5.5.0 ; extra == 'mssql'
Requires-Dist: psycopg[binary]>=3.3.8 ; extra == 'postgres'
Requires-Python: >=3.12
Provides-Extra: all
Provides-Extra: cli
Provides-Extra: duckdb
Provides-Extra: mssql
Provides-Extra: postgres
Description-Content-Type: text/markdown

# coasti-xls-io

Import Excel files into **Postgres, DuckDB and MSSQL**, safely:

- every file is checked against a **table definition** (types, keys, required values, rules),
  declared in Python like the coasti planner's tables; errors point to the Excel row and
  column, and a file with errors is never written
- a **before/after preview** shows what will be inserted, updated (old → new) and deleted
- the import is written **in one transaction**, exactly as previewed, with an **audit log** entry
- German Excel conventions work out of the box (`1.234,56`, `31.12.2025`, `ja/nein`)

Use it as a **Python library** (the coasti planner does), from the **command line**
(`xls-io`) or in the **web UI** (from this repository or the Docker image; not published
on PyPI), and export tables back to Excel.

> Status: v0.1, early development. [PLAN.md](PLAN.md) has the plan, [LOG.md](LOG.md) the
> decisions and progress.

## Quickstart

```bash
pip install "coasti-xls-io[all]"          # or pick extras: cli, postgres, duckdb, mssql
```

**Table definitions.** Tables live in template files: Python files that declare a table and
its import (see [docs/TABLES.md](docs/TABLES.md)):

```python
# orders.py
from coasti_xls_io.core import Column, ColumnDType, ExcelImportTemplate, Table, WriteRule

orders = Table(
    id="orders",
    name="Aufträge",
    columns=[
        Column(id="order_id", name="Auftrag", data_type=ColumnDType.STRING, is_key=True),
        Column(id="amount", name="Betrag", data_type=ColumnDType.FLOAT),
    ],
)
orders_import = ExcelImportTemplate(
    table=orders,
    mode="upsert",
    rules_write_to_db=[
        WriteRule(column="amount", type="decimal(12,2)"),
    ],
)
```

**Command line.** Validate, preview, confirm, write:

```bash
export XLS_IO_CONNECTION_STRING='postgresql://user:password@host/db'
xls-io table init --from-excel sample.xlsx -o orders.py   # draft, then review it
xls-io upload --file orders.xlsx --template orders.py
xls-io download --file orders-export.xlsx --template orders.py
```

**Python.** `to_polars`, `to_sql_alchemy` and `to_xls` take a definition, see
[docs/PYTHON_API.md](docs/PYTHON_API.md).

**Web UI.**

```bash
uv run python -m coasti_xls_io.server setup   # guided: databases, template folder, login
uv run python -m coasti_xls_io.server run     # → http://127.0.0.1:8000
```

**Docker.** See [docs/DOCKER.md](docs/DOCKER.md):

```bash
docker build -t coasti-xls-io .
docker run -it --rm -v ./config:/config coasti-xls-io xls-io-server setup
docker run -d -p 8000:8000 -v ./config:/config -v ./data:/data coasti-xls-io
```

## Documentation

| Guide | Contents |
|---|---|
| [examples/finance](examples/finance/README.md) | **Start here:** demo of a monthly finance import, from installation to web UI |
| [docs/CLI.md](docs/CLI.md) | All CLI commands and options, with working examples |
| [docs/TABLES.md](docs/TABLES.md) | Table definitions: template files, `Table`, `ExcelImportTemplate`, rules per import step, types, German defaults, error codes |
| [docs/SERVER.md](docs/SERVER.md) | Server setup, users, configuration, HTTP API, audit log, logging |
| [docs/DOCKER.md](docs/DOCKER.md) | Running the image, folders, secrets |
| [docs/FRONTEND.md](docs/FRONTEND.md) | Web UI development and embedding the React components |
| [CHANGELOG.md](CHANGELOG.md) | Changes per release, with the migration guide from 0.1.x to 0.2.0 |
| [docs/RELEASE.md](docs/RELEASE.md) | PyPI releases (0.1.0, 0.1.1, 0.2.0) and how to publish; scope, requirements, open decisions |
| [docs/CORE_ARCHITECTURE.md](docs/CORE_ARCHITECTURE.md) | Import template classes: public data models vs. the private `Template`, with diagrams |
| [docs/PYTHON_API.md](docs/PYTHON_API.md) | Library use (coasti planner): `to_polars`, `to_sql_alchemy`, `to_xls` with planner tables |

Connection strings: `postgresql://…`, `mssql://…` (ODBC Driver 18 by default; install it on the
machine) and `duckdb:///path/file.duckdb`. CLI exit codes: 0 ok, 1 validation errors,
2 usage/config error, 3 database error, 4 aborted.

## Development

Requirements: [uv](https://docs.astral.sh/uv/), Docker, Node 24 with pnpm, and for MSSQL the
[ODBC Driver 18 for SQL Server](https://learn.microsoft.com/sql/connect/odbc/download-odbc-driver-for-sql-server).
Optional: LibreOffice, to generate the legacy `.xls` fixtures.

```bash
uv sync                                       # .venv with all extras + dev tools
uv run pre-commit install                     # git hooks: ruff, mypy, import-linter, frontend checks
docker compose -f docker/compose.yml up -d    # Postgres :55432, MSSQL :51433
uv run pytest                                 # unit + integration tests
cd frontend && pnpm install && pnpm test      # web UI (see docs/FRONTEND.md)
uv run python scripts/check_package.py        # release build: wheel + sdist in dist/, checked in clean installs
```

### Databases for tests

For each database, tests use the first one available:

1. An explicit URL: `XLS_IO_TEST_PG_URL`, `XLS_IO_TEST_MSSQL_URL`
2. The docker compose services (ports and passwords: [docker/.env.example](docker/.env.example))
3. A throwaway testcontainers container (needs Docker)
4. Otherwise the tests are skipped

DuckDB always uses a temporary file. Run a subset with markers, e.g. `uv run pytest -m duckdb`
or `uv run pytest -m "not slow"`.

### Excel test fixtures

[tests/fixtures/make_fixtures.py](tests/fixtures/make_fixtures.py) generates messy Excel files
(leading zeros, Excel serial dates, German numbers and dates, header offsets, formulas, …)
and their expected results. The matching table definitions are template files, one per case
([tests/fixtures/excel/tables/](tests/fixtures/excel/tables/)):

```bash
uv run python tests/fixtures/make_fixtures.py         # .xlsx + expected CSV
uv run python tests/fixtures/make_fixtures.py --xls   # also .xls (needs LibreOffice)
```

### Checks

```bash
uv run ruff check . && uv run ruff format --check .
uv run mypy
uv run lint-imports        # core must not import cli/server/typer/fastapi
uv run pre-commit run --all-files
```

CI and publishing run on Forgejo (`.forgejo/`, set up like the coasti planner's):
`ci.yml` checks every pull request, `publish.yml` uploads to PyPI when the version in
`pyproject.toml` changes on `main` (a manual run can upload to TestPyPI), and `audit.yml`
checks the dependencies daily. Publishing is blocked by the `Private :: Do Not Upload`
classifier and needs the `PYPI_TOKEN` secret; see [docs/RELEASE.md](docs/RELEASE.md).

### Layout

```text
src/coasti_xls_io/
  core/       template, reader, validate, diff, writer, db, config, audit, pipeline (no CLI/web deps)
  cli/        Typer app `xls-io`
  server/     FastAPI app (+ built web UI in static/); not in the published package
frontend/     pnpm workspace: client, components, apps/web
tests/        unit/ integration/ fixtures/
docker/       compose.yml for dev databases
Dockerfile    server image
```
