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.2.0.tar.gz (39.5 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.2.0-cp314-cp314-win_amd64.whl (44.5 kB view details)

Uploaded CPython 3.14Windows x86-64

xlspy-0.2.0-cp314-cp314-musllinux_1_2_x86_64.whl (69.4 kB view details)

Uploaded CPython 3.14musllinux: musl 1.2+ x86-64

xlspy-0.2.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.2.0-cp314-cp314-macosx_11_0_arm64.whl (41.9 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

xlspy-0.2.0-cp313-cp313-win_amd64.whl (44.0 kB view details)

Uploaded CPython 3.13Windows x86-64

xlspy-0.2.0-cp313-cp313-musllinux_1_2_x86_64.whl (69.4 kB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

xlspy-0.2.0-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl (70.2 kB view details)

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

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

Uploaded CPython 3.13macOS 11.0+ ARM64

xlspy-0.2.0-cp312-cp312-win_amd64.whl (44.0 kB view details)

Uploaded CPython 3.12Windows x86-64

xlspy-0.2.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.2.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.2.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.2.0.tar.gz.

File metadata

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

File hashes

Hashes for xlspy-0.2.0.tar.gz
Algorithm Hash digest
SHA256 9261dcb6967821758b039320f3e2292d04db0e8c4366802502e59462a16cc1c5
MD5 38b3f867f0f9f497f441640000879347
BLAKE2b-256 fc09c489680645c5a6e4cb932c1ca15ca0c614dc85377cae9a16e02780a2d929

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: xlspy-0.2.0-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 44.5 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.2.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 77c741bdb11017f94d5d25d899946a54eb48e0494fdb229ac7e8415fedcabced
MD5 a438731231aa5507e19e86987d5974de
BLAKE2b-256 662d57ea74d79421d4b748287019a01197b33bd8c2fa06b3e067b23fbaf9ee69

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for xlspy-0.2.0-cp314-cp314-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 6fa9480ddcad78a9f7f5e3d9123276cc05bbcd59173420ae1ee612358fee94b7
MD5 c458ba8237aed363176bbbe03c6e2194
BLAKE2b-256 a001eb6cf78a34c275eab72337c899fa3417f5901882812e857f587637ae8af0

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.2.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.2.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.2.0-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 8641b189db4d199ce00c4306f0841419de31b4e7ec602b0e3a64d04acc0968b4
MD5 9c8378d47ed9526aeec80680f2a1a719
BLAKE2b-256 9b423fda331fab95e08a13f850fb6b9ac8639036bf7fe3472cf2c0de8b69489b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for xlspy-0.2.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e7d461e665218be18a10383a6de0772daf8555048fcaf7550b4841567b0b7ae2
MD5 09be23b3ff8b2e910db18835411655b3
BLAKE2b-256 bec20836659de3eb8c4c020449744f4f60b9006338490dd52e4f28736d07cdeb

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: xlspy-0.2.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 44.0 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.2.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 5a3756826ce93e8a651a37fdf3c11d84ec6da7ab0388a8aa81a0808209c58c7d
MD5 d6cbd412b902c76da7a749fb90c02770
BLAKE2b-256 780b33493af5f1c289108413c91547cac513e832905cfde75b3662ece4906bc7

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for xlspy-0.2.0-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 35dedfb337505bacfcd016cb32f0050a25b23626519cdd30ef303f4e1c3bb4a2
MD5 0535cc0ed1f71ba18a5436a1a761c04b
BLAKE2b-256 62851a3247bb8ff84c8642c55518bd4bdc53e0b2c9f4ad0bafde592653325730

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.2.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.2.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.2.0-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 fbf9b4f4035a4563fed937a36c8e42a8382488bc808e61eef3d1b37d57257a1f
MD5 25d362ea3855abf0f6cdf2423ba13ba8
BLAKE2b-256 49dc6cd5b1f0bb2c7f58e6977c634f04e91387a8df97493ab3c372ab59dc5b21

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for xlspy-0.2.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3dd1c8c906ebcce26f4f159a0d24f6397efe611bbd152b32a0991889450b3179
MD5 34b8acba1539bd65625adb16c40d5878
BLAKE2b-256 cee2561284c275c2e96799e3f3cddb7899d9f4d3b61f2dd2b8962b6c2fb0e235

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: xlspy-0.2.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 44.0 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.2.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 f919f3e8d24d9f434568af525b5d1000a42ccdf44ba0b9ba23e8cce556cd603a
MD5 17aec779dbcfe8a7aabe3b2d71deb866
BLAKE2b-256 314275bf434d205c728856dd73fad61b59c37ccc37d52405b1f7f1a235b95a08

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for xlspy-0.2.0-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 1cd29c5035f99b620a3d1986ec38a238c564e8aad53ae90bd1a87ad9cd3b3a87
MD5 7f88fdab916afeaec522643df46b12f7
BLAKE2b-256 baa06bc5b89f8da05273ebf04107767f02b0cb1772260e85601dd7f57b160825

See more details on using hashes here.

Provenance

The following attestation bundles were made for xlspy-0.2.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.2.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.2.0-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Algorithm Hash digest
SHA256 adf7573af65fb17fa6442ed1ff0d64d5951b175242577eaa21d142a2376b6e08
MD5 87486593fcc7f9aa21e06cf0923dc5de
BLAKE2b-256 789131c2c42e666b31bc12faa49f1697c83967c5bf5c7155b7edfcac554943b9

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for xlspy-0.2.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 493bbb9bca104d3652bc8c9fd9c6cec08829b1feb90616f44ca9910fe8637636
MD5 b17a6c99e9f82be9af760adc13654aa0
BLAKE2b-256 be11fd82edd4c7f05da65d78bf8625389c4dc2d44ef02d804528690af109afd5

See more details on using hashes here.

Provenance

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