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."]])

Cell Formatting (Number, Date, DateTime)

xlspy supports custom cell formatting for numbers, dates, and datetimes in both XLSB and XLSX output. Pass a (value, format_string) tuple to apply a format to a specific cell.

from xlspy import XlsxWriter, F  # or XlsbWriter
import datetime

with XlsxWriter("formatted.xlsx") as writer:
    writer.add_sheet("Formats")
    writer.write_sheet([
        ["Description", "Value"],
        ["Thousands separator", (100000, F.THOUSANDS_SEP)],
        ["Currency PLN",       (100000, F.CURRENCY_PLN)],
        ["Currency EUR",       (100000, F.CURRENCY_EUR)],
        ["Percentage",         (100000, F.PERCENTAGE)],
        ["Scientific",         (100000, F.SCIENTIFIC)],
        ["Two decimals",       (100000, F.TWO_DECIMALS)],
        ["Text",               (100000, F.TEXT)],
        ["Leading zeros",      (100000, F.LEADING_ZEROS)],
        ["Short date",         (datetime.date(2026,6,1), F.DATE_SHORT)],
        ["Long date",          (datetime.date(2026,6,1), F.DATE_LONG)],
        ["ISO date",           (datetime.date(2026,6,1), F.DATE_ISO)],
        ["Month + year",       (datetime.date(2026,6,1), F.DATE_MONTH_YEAR)],
        ["Weekday + date",     (datetime.date(2026,6,1), F.DATE_WEEKDAY)],
        ["Short datetime",     (datetime.datetime(2026,6,1,14,34), F.DATETIME_SHORT)],
        ["Time only",          (datetime.datetime(2026,6,1,14,34), F.TIME_HH_MM)],
        ["12h time",           (datetime.datetime(2026,6,1,14,34), F.TIME_12H)],
        ["ISO datetime",       (datetime.datetime(2026,6,1,14,34), F.DATETIME_ISO)],
    ])

Available Format Constants (xlspy.F)

Number Date DateTime
F.THOUSANDS_SEP#,##0 F.DATE_SHORTdd.mm.yyyy F.DATETIME_SHORTdd.mm.yyyy hh:mm
F.CURRENCY_PLN#,##0.00 "zł" F.DATE_LONGd mmmm yyyy F.DATETIME_LONGd mmmm yyyy hh:mm:ss
F.CURRENCY_EUR#,##0.00 € F.DATE_DAY_MONTH_YEARdd-mm-yyyy F.TIME_HH_MMhh:mm
F.PERCENTAGE0% F.DATE_ISOyyyy-mm-dd F.TIME_HH_MM_SShh:mm:ss
F.SCIENTIFIC0.00E+00 F.DATE_MONTH_YEARmmmm yyyy F.TIME_12Hh:mm AM/PM
F.TWO_DECIMALS#,##0.00 F.DATE_WEEKDAYdddd, d mmmm yyyy F.DATETIME_24Hdd.mm.yyyy hh:mm:ss
F.TEXT@ F.DATE_DAY_MONTHd mmmm F.DATETIME_ISOyyyy-mm-dd"T"hh:mm:ss
F.LEADING_ZEROS000000000 F.DATE_YEAR_ONLYyyyy F.TIME_MShh:mm:ss.000

You can also use custom format strings directly:

writer.write_sheet([
    ["Custom", (1234.56, '#,##0.00 "USD"')],
    ["Date",   (datetime.date(2026,6,1), 'dd.mm.yyyy')],
])

The formatting works transparently on both XlsxWriter and XlsbWriter.

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.25 MB
xlspy (Python) XLSB 2.54 s 7.25 MB
xlspy XLSX 5.35 s 6.34 MB
xlsxwriter XLSX 9.80 s 11.57 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.3.0.tar.gz (39.9 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.3.0-cp314-cp314-win_amd64.whl (44.1 kB view details)

Uploaded CPython 3.14Windows x86-64

xlspy-0.3.0-cp314-cp314-musllinux_1_2_x86_64.whl (69.5 kB view details)

Uploaded CPython 3.14musllinux: musl 1.2+ x86-64

xlspy-0.3.0-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (70.2 kB view details)

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

xlspy-0.3.0-cp314-cp314-macosx_11_0_arm64.whl (41.9 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

xlspy-0.3.0-cp313-cp313-win_amd64.whl (43.5 kB view details)

Uploaded CPython 3.13Windows x86-64

xlspy-0.3.0-cp313-cp313-musllinux_1_2_x86_64.whl (69.5 kB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

xlspy-0.3.0-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (70.3 kB view details)

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

xlspy-0.3.0-cp313-cp313-macosx_11_0_arm64.whl (41.9 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

xlspy-0.3.0-cp312-cp312-win_amd64.whl (43.5 kB view details)

Uploaded CPython 3.12Windows x86-64

xlspy-0.3.0-cp312-cp312-musllinux_1_2_x86_64.whl (69.4 kB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

xlspy-0.3.0-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (70.2 kB view details)

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

xlspy-0.3.0-cp312-cp312-macosx_11_0_arm64.whl (41.9 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

File details

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

File metadata

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

File hashes

Hashes for xlspy-0.3.0.tar.gz
Algorithm Hash digest
SHA256 693f55a798ba989a2906bf0f25eaa3533d0a0752bad6db64bd2c17784d749875
MD5 56929d9c20c9e7666cd7a806fb22d313
BLAKE2b-256 a346e53bf108654cf4b7a5b0c927e864acc8eef02147e75efa8818a0b2139f5f

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.3.0.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.3.0-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: xlspy-0.3.0-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 44.1 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.3.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 b390656fc674b6da0d527a82d24356e187db40ea4b7eb5a449a4739d9f1bc642
MD5 7f7ca839c22a4e074422e20fcf219879
BLAKE2b-256 c47c8c01ef967347fa89fe0f173ba6724477834e26468b62cadbdda9ad576c7b

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.3.0-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.3.0-cp314-cp314-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for xlspy-0.3.0-cp314-cp314-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 ee82c6ed0d587d5ec690381773fcb198fb176fc00ba5600308029263c3cd5074
MD5 8746cf82a14c9e5e325d0606a58b6d27
BLAKE2b-256 4a00c325e6dd78eea6f9d9f7307fd1528aab1fc28a0176d499a9cf68071176e8

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.3.0-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.3.0-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for xlspy-0.3.0-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 5356a15a2cc00e27fa13b7067a32d25c20a04f0ab6d333a28298fe199b630d30
MD5 95df8d0cc63f7c185a38b4c746dea618
BLAKE2b-256 1c44a86d1078af41e67dd85f6b52c649ffff0ebb358a4be4f2f4ef7383b3ff05

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.3.0-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.3.0-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for xlspy-0.3.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e1a2dfe32aefe7c0af3d17a2e04223e5d45438a84652a2bb33813270b6d86280
MD5 b0a28d62fe62cb4e79ab45583a84d957
BLAKE2b-256 83807a4fbc2409f3559b5131de8a194e07577ff28bf6eee6a58a69f3836ad818

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.3.0-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.3.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: xlspy-0.3.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 43.5 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.3.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 1d9a257da79b7efd2454de55a5ad1cd852fd7b184f021d723ace6d2746e55b7f
MD5 ec1b3da1abe7205e0e4eea022b614bcc
BLAKE2b-256 afc3e4eeccbe00f59326d640765ed9ea9f9fe86e81791e6aba6cd13d16b1af18

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.3.0-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.3.0-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for xlspy-0.3.0-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 f1ed6c7bf72d07750023b651330eec01742a4b511c1aded02f4ac284403a2886
MD5 9b8c7d6413188e42d73d4b4474f9419b
BLAKE2b-256 d7cd76710b41559e89f372fdbb59c158c7a3df9c74053e7316e623c3a08ffbc1

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.3.0-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.3.0-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for xlspy-0.3.0-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 633d1a784ac300d10a3b40f51732a91bf103c11ba2ce4b9a77094a66b7b66e6a
MD5 fb3283a11a48b8fa675a59232296def5
BLAKE2b-256 811f8251d59cfc64222ff3e6d691d97766ed96487fe860a9cf0c401de1560d86

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.3.0-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.3.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for xlspy-0.3.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2281a4d56b32ec6fc8e0dee198bc31e449c479587a4b30feccbea46be1810ebe
MD5 c3d05a2cd159b599c9aec21c6cd7b6b7
BLAKE2b-256 2417b4b3a58e77a819f964446e9e2005c581a85d93cf2490878a155d9fb71f9a

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.3.0-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.3.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: xlspy-0.3.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 43.5 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.3.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 ca6c86abfca9576337076ea03e1d0dc517459c981d3d24c75d14beb990271ac0
MD5 8e85f9e3dc70a345c3a331bf5692fc14
BLAKE2b-256 61403014e44c2ed6626397be7c680df66e2064e7962c0960412a3cc645928f12

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.3.0-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.3.0-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for xlspy-0.3.0-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 a927d9e2d2beff579edb93c17cf6bea0212f1932f2258880ee17b357ddd87249
MD5 7661130d8d2de9a938b6343186007072
BLAKE2b-256 8397139fc258d5c2adfea8d7b45938f08e8451f99fefbb07e7d0191423a9fe6f

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.3.0-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.3.0-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.

File metadata

File hashes

Hashes for xlspy-0.3.0-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 3f0408527087d908fcead849cd542b73329aeed9d5017af8d8dc007020727e46
MD5 1fcbe0ed179bec6e8517df9afd6b9c08
BLAKE2b-256 06fa754e9b0fcb43f7cd000ddaf25b4490c710d31f5437d9d9db2d3017ab534c

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.3.0-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.3.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for xlspy-0.3.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c762b5bc24298b79b7ecfe5e41835ee4f52ff950c9eb418c3ca0989b4cfff49a
MD5 319b983a695746da3d3dd375dbbf172c
BLAKE2b-256 7410781c9998ad43670710ee9064007dcf4bd26b4f92689128afe07964539959

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.3.0-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