Skip to main content

Read and write XLSB and XLSX files efficiently.

Project description

Python XLSB Reader & Writer

A Python library for reading and writing XLSB and XLSX files efficiently.

Installation

pip install xlspy

Usage

Basic Example

from xlspy import XlsbWriter
import datetime
from decimal import Decimal

data = [
    ["Name", "Age", "City", "info"],
    [-123, 2147483647, 2147483648, 2147483999],
    ["x", "y", "z", datetime.datetime.today()],
    ["Alice", 25, "New York", datetime.date.today()],
    ["Bob", 30, "London", Decimal(3.14)],
    ["Charlie", 35, "Paris", datetime.datetime.now()],
    [True, False, None, datetime.datetime.utcnow()]
]

# Initialize writer with a specific compression level
with XlsbWriter("output.xlsb", compressionLevel=6) as writer:
    # Add a visible sheet
    writer.add_sheet("Visible Sheet")
    writer.write_sheet(data)

    # Add a hidden sheet
    writer.add_sheet("Hidden Sheet", hidden=True)
    writer.write_sheet([["This sheet is hidden."]])

XlsxWriter Example

from xlspy import XlsxWriter
import datetime
from decimal import Decimal

data = [
    ["Name", "Age", "City", "info"],
    [-123, 2147483647, 2147483648, 2147483999],
    ["x", "y", "z", datetime.datetime.today()],
    ["Alice", 25, "New York", datetime.date.today()],
    ["Bob", 30, "London", Decimal(3.14)],
    ["Charlie", 35, "Paris", datetime.datetime.now()],
    [True, False, None, datetime.datetime.utcnow()]
]

# Initialize writer with a specific compression level
with XlsxWriter("output.xlsx", compressionLevel=6) as writer:
    # Add a visible sheet
    writer.add_sheet("Visible Sheet")
    writer.write_sheet(data)

    # Add a hidden sheet
    writer.add_sheet("Hidden Sheet", hidden=True)
    writer.write_sheet([["This sheet is hidden."]])

Reading XLSB and XLSX Files

Reading files is done via the ExcelReader class, which automatically detects the format.

from xlspy import ExcelReader

with ExcelReader("input.xlsx") as reader:  # or .xlsb
    names = reader.get_sheet_names()
    print(f"Sheets: {names}")

    for sheet_name in names:
        rows = reader.read_all(sheet_name)
        for row in rows:
            print(row)

# Generator usage (memory efficient for large files):
with ExcelReader("large_file.xlsb") as reader:
    for row in reader.get_rows("Sheet1"):
        print(row)

Streaming from a Database (Netezza)

This example shows how to stream data directly from a database query into an XLSB file using nzpy-extended. This is highly memory-efficient as it doesn't load the entire dataset into memory.

First, ensure you have nzpy-extended installed:

pip install nzpy-extended

Then, you can use a generator function to feed data to XlsbWriter.

import os
from typing import Generator
from xlspy import XlsbWriter

# --- Configuration ---
NZ_CONFIG = {
    "host": os.environ.get("NZ_DEV_HOST", "your_host"),
    "port": int(os.environ.get("NZ_DEV_PORT", "5480")),
    "database": os.environ.get("NZ_DEV_DB", "your_db"),
    "user": os.environ.get("NZ_DEV_USER", "your_user"),
    "password": os.environ.get("NZ_DEV_PASSWORD", "your_password"),
}
QUERY = "SELECT * FROM YourTable"
OUTPUT_FILENAME = "db_output.xlsb"


def row_generator(cursor) -> Generator[list, None, None]:
    """Yields column headers first, then each data row."""
    headers = [column[0] for column in cursor.description]
    yield headers
    while row := cursor.fetchone():
        yield list(row)


# --- Main Execution ---
try:
    import nzpy_extended.sync as nzpy

    with nzpy.connect(**NZ_CONFIG) as conn:
        cursor = conn.cursor()
        cursor.execute(QUERY)

        with XlsbWriter(OUTPUT_FILENAME) as writer:
            writer.add_sheet("Database Export")
            writer.write_sheet(row_generator(cursor))
            writer.add_sheet("SQL Query", hidden=True)
            writer.write_sheet([["SQL"], [QUERY]])

    print(f"Successfully created '{OUTPUT_FILENAME}'")

except Exception as e:
    print(f"An unexpected error occurred: {e}")

Performance

xlspy is designed for high performance. Since version 0.1.0, the library includes a C extension (_c_core) that accelerates XLSB read and write. The C extension is enabled by default (compiled automatically on install). Set XLSPY_DISABLE_C_EXT=1 to force the pure Python fallback.

All benchmarks: 50000 × 50 dataset (2.5M cells). Tests performed on Windows 11 (Python 3.14, AMD64).

Write

Library Format Time Size
xlspy (C_EXT) XLSB 1.02 s 7.20 MB
xlspy (Python) XLSB 2.54 s 7.20 MB
xlspy XLSX 3.66 s 6.32 MB
xlsxwriter XLSX 8.67 s 11.44 MB

Read

Library Format Time Notes
xlspy (C_EXT) XLSB 1.39 s default, compiled C
xlspy XLSX 4.72 s uses expat XML parser (C)
xlspy (Python) XLSB 6.41 s pure Python fallback
openpyxl XLSX 7.85 s read-only mode

Analysis

The 4.6× read speedup comes from two factors:

  • ~60–70% — native C compilation, no interpreter overhead per record
  • ~30–40% — algorithm simplification: flat array indexed by col − first_col instead of Dict[int, Any], no isinstance per cell, no BiffReader.read_worksheet() method call per record

Run the benchmarks yourself with examples/performance_test.py.

Repository

https://github.com/KrzysztofDusko/xlspy/

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

xlspy-0.1.3.tar.gz (37.4 kB view details)

Uploaded Source

Built Distributions

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

xlspy-0.1.3-cp314-cp314-win_amd64.whl (43.2 kB view details)

Uploaded CPython 3.14Windows x86-64

xlspy-0.1.3-cp314-cp314-musllinux_1_2_x86_64.whl (68.2 kB view details)

Uploaded CPython 3.14musllinux: musl 1.2+ x86-64

xlspy-0.1.3-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (68.9 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

xlspy-0.1.3-cp314-cp314-macosx_11_0_arm64.whl (40.6 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

xlspy-0.1.3-cp313-cp313-win_amd64.whl (42.7 kB view details)

Uploaded CPython 3.13Windows x86-64

xlspy-0.1.3-cp313-cp313-musllinux_1_2_x86_64.whl (68.2 kB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

xlspy-0.1.3-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (69.0 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

xlspy-0.1.3-cp313-cp313-macosx_11_0_arm64.whl (40.6 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

xlspy-0.1.3-cp312-cp312-win_amd64.whl (42.7 kB view details)

Uploaded CPython 3.12Windows x86-64

xlspy-0.1.3-cp312-cp312-musllinux_1_2_x86_64.whl (68.2 kB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

xlspy-0.1.3-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (69.0 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64manylinux: glibc 2.5+ x86-64

xlspy-0.1.3-cp312-cp312-macosx_11_0_arm64.whl (40.6 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

File details

Details for the file xlspy-0.1.3.tar.gz.

File metadata

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

File hashes

Hashes for xlspy-0.1.3.tar.gz
Algorithm Hash digest
SHA256 5172206aa9665d5c312731ec51ec9de4f309a485bc3f1ef23bbb2a6e2b6303e3
MD5 e469aa873b98e3a45e64222a9975538f
BLAKE2b-256 ff3fa9bdef08fd8f4a515ebd58aa4da85f3204d15c3b600080625a7ec4887196

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.1.3.tar.gz:

Publisher: publish.yml on KrzysztofDusko/xlspy

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

File details

Details for the file xlspy-0.1.3-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: xlspy-0.1.3-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 43.2 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for xlspy-0.1.3-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 4adf4186ec9548b0bb47d488491defc2b9efa01577f3b936bcf0deb765a58991
MD5 00831ec1c3d851046be4d68134ba3af9
BLAKE2b-256 1df0a84fbc795da11105bbbc5041cbb2ebae2691f2e6bad0dfd86333f3587995

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.1.3-cp314-cp314-win_amd64.whl:

Publisher: publish.yml on KrzysztofDusko/xlspy

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

File details

Details for the file xlspy-0.1.3-cp314-cp314-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for xlspy-0.1.3-cp314-cp314-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 c12e0b0b24442a00727caecf9e2eb607b2b1d5dfd5dbb96b5f8857b519a21659
MD5 b2d6c4abdc0580b8aa4e7600aa3cbbf7
BLAKE2b-256 08631f13447f5498f8606c9635aec9cf4cc0577f8d33d31947c1e0015896f15f

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.1.3-cp314-cp314-musllinux_1_2_x86_64.whl:

Publisher: publish.yml on KrzysztofDusko/xlspy

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

File details

Details for the file xlspy-0.1.3-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for xlspy-0.1.3-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 ba08b2a4503533196295fb07cb4a1eaa40e868a597452e39aa3c6200d5a9230f
MD5 fa34fd8e7ff9bd8cecedd20537277625
BLAKE2b-256 934ece2c67741de554e825a717b250fb856bdddcc0e4972d6f72bc3a0478deab

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.1.3-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl:

Publisher: publish.yml on KrzysztofDusko/xlspy

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

File details

Details for the file xlspy-0.1.3-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for xlspy-0.1.3-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 087ba68b53ffd8e0aac7ccfebae525c61bfa6126f6d568ed9cecf08b834f4f5b
MD5 a3519b43c27f3e18d92b72b310a37b7e
BLAKE2b-256 87682bc1a03f09ce3990660037a99fb9629ffe66e2c0e12ca07c52541b8a7309

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.1.3-cp314-cp314-macosx_11_0_arm64.whl:

Publisher: publish.yml on KrzysztofDusko/xlspy

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

File details

Details for the file xlspy-0.1.3-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: xlspy-0.1.3-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 42.7 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for xlspy-0.1.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 ce150fc04c384611e181b50579d1250d83988524e3915a9482c2e3faee91b289
MD5 848d0409a012c34a0b66fa1a74eed19e
BLAKE2b-256 a8d45b1aa47a72b6edbdeac453666c0528bd14b17325dbeaa74f1f698c9cb721

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.1.3-cp313-cp313-win_amd64.whl:

Publisher: publish.yml on KrzysztofDusko/xlspy

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

File details

Details for the file xlspy-0.1.3-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for xlspy-0.1.3-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 2671f3f09c44fea2190d92c17d15925ea8b40d7058d549f86b923d4729bc03b2
MD5 b8d5c9ba04bb05c2026f677238ba09c7
BLAKE2b-256 338ac439dc2de0a86a5c3690ca2b4abd0eb34491c7897d8267fa953ecf4a2d07

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.1.3-cp313-cp313-musllinux_1_2_x86_64.whl:

Publisher: publish.yml on KrzysztofDusko/xlspy

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

File details

Details for the file xlspy-0.1.3-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for xlspy-0.1.3-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 41daecf79ee6450892f23d450dbea5acb3637444d33f93b78a4c997eaceb9756
MD5 28175980ee760abccf1f5ecfd114845e
BLAKE2b-256 6218d52f3a05acd6f2ae25f526a0d760de220f8084f259b3361790b3f4467836

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.1.3-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl:

Publisher: publish.yml on KrzysztofDusko/xlspy

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

File details

Details for the file xlspy-0.1.3-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for xlspy-0.1.3-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 eeb1867be3a66cec8d72097f8fd20924ae6b0612ba988d911f133d2c5cbb0fee
MD5 8f8dd3e08b1a7dc84ebe94af338d2850
BLAKE2b-256 901ec9a17a83650059df9ad2a84a35ac96e09da49945c3d6f1cead2751718603

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.1.3-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: publish.yml on KrzysztofDusko/xlspy

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

File details

Details for the file xlspy-0.1.3-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: xlspy-0.1.3-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 42.7 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for xlspy-0.1.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 a1671b3fdcabaed86e8ae39732d155327d9529828a692abe1467bd4eecf9da0d
MD5 70b2be02446575fce7c603c7baae10d9
BLAKE2b-256 353cceccfcc62985089cbbba6646d914a629ad6c75753433cc59397b670812b9

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.1.3-cp312-cp312-win_amd64.whl:

Publisher: publish.yml on KrzysztofDusko/xlspy

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

File details

Details for the file xlspy-0.1.3-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for xlspy-0.1.3-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 9ab4986bd1ac2d2cadf6aec5745deda03fe5f78c2e9656818bf22d778a2ccba9
MD5 fd854e6dcae89d7c341c4bc43d5e151b
BLAKE2b-256 52b8bc85abdb1da7946b409a8a4e4553d2ecb63da35735aface023eb36eb09ac

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.1.3-cp312-cp312-musllinux_1_2_x86_64.whl:

Publisher: publish.yml on KrzysztofDusko/xlspy

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

File details

Details for the file xlspy-0.1.3-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for xlspy-0.1.3-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 cc5c06b8fe3ebba5ba0c2af1f33f9f9160b5e1b8a002938b1ca20484c88f8d9f
MD5 f12fa0453e92ed4e9dadfe35ec00ee55
BLAKE2b-256 cf77f458eafd317bb04c6c58293e9f9396cae87dc4b265a294212ddac4130824

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.1.3-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl:

Publisher: publish.yml on KrzysztofDusko/xlspy

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

File details

Details for the file xlspy-0.1.3-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for xlspy-0.1.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a6d7444ffc55ed175a2049f03845b1423e399311dc87f6332aa1cce7da131e0f
MD5 e3021aeb651442c11f72133e8245b50b
BLAKE2b-256 a115c65967eb86fc9959b291ffe6ba933349e28f6c06c1e2e40a88a17690808e

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.1.3-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: publish.yml on KrzysztofDusko/xlspy

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