API reference¶
rustpy_xlsxwriter ¶
RustPy-XlsxWriter¶
High-performance Excel file generation powered by Rust. ~9x faster than Python's xlsxwriter.
Quick start::
from rustpy_xlsxwriter import FastExcel
# One-liner
FastExcel("output.xlsx").sheet("Sheet1", records).save()
# Multiple sheets with options
(
FastExcel("report.xlsx", password="secret")
.format(float_format="0.00", index_columns=["Name"], bold_headers=True)
.freeze(row=1, col=1)
.sheet("Users", user_records)
.sheet("Orders", order_records)
.save()
)
# Context manager (auto-saves on exit)
with FastExcel("output.xlsx") as f:
f.sheet("Users", user_records)
f.sheet("Orders", order_records)
# Pandas DataFrame
FastExcel("df.xlsx").sheet("Sheet1", pandas_df).save()
# Polars DataFrame
FastExcel("df.xlsx").sheet("Sheet1", polars_df).save()
# In-memory buffer
import io
buf = io.BytesIO()
FastExcel(buf).sheet("Sheet1", records).save()
# Generator streaming (memory-efficient)
def rows():
for i in range(1_000_000):
yield {"id": i, "value": f"row_{i}"}
FastExcel("big.xlsx").sheet("Data", rows()).save()
You can also use the lower-level functional API directly::
from rustpy_xlsxwriter import write_worksheet, write_worksheets
write_worksheet([{"Name": "Alice"}], "output.xlsx")
Format ¶
A reusable cell format (font, fill, border, alignment, number format).
Setters are chainable — each returns self::
Format().set_bold().set_font_color("#FF0000").set_num_format("0.00%")
Colors accept "#RRGGBB" / "RRGGBB" hex or a color name
(e.g. "red"). Enum-valued setters accept lowercase string names.
FastExcel ¶
Fluent builder for creating Excel files.
Examples::
# Minimal
FastExcel("out.xlsx").sheet("Sheet1", records).save()
# Full options
(
FastExcel("report.xlsx", password="s3cret")
.format(float_format="0.00", index_columns=["ID"])
.freeze(row=1)
.sheet("Users", user_records)
.sheet("Orders", order_records)
.save()
)
__init__ ¶
__init__(
target: Union[str, PathLike, BinaryIO],
*,
output_format: Optional[str] = None,
password: Optional[str] = None,
autofit: bool = True,
sanitize_formulas: bool = False,
bom: bool = False,
columns: Optional[List[str]] = None,
header: bool = True
) -> None
Create a new writer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Union[str, PathLike, BinaryIO]
|
File path ( |
required |
output_format
|
Optional[str]
|
|
None
|
password
|
Optional[str]
|
Optional worksheet-protection password. NOTE: this sets Excel's sheet protection flag only — it does not encrypt the file. The cell data is stored in plaintext and the protection is trivially removed; do not rely on it to keep data confidential. |
None
|
autofit
|
bool
|
Automatically adjust column widths (default |
True
|
sanitize_formulas
|
bool
|
CSV/TSV only. When |
False
|
bom
|
bool
|
CSV/TSV only. Prefix the UTF-8 byte order mark, which is what makes Excel on Windows read the file as UTF-8 rather than the system code page. Off by default so output stays byte-identical. |
False
|
columns
|
Optional[List[str]]
|
CSV/TSV only. Select and order the output columns by name.
An unknown name raises |
None
|
header
|
bool
|
CSV/TSV only. Write the header row (default |
True
|
format ¶
format(
*,
float_format: Optional[str] = None,
datetime_format: Optional[str] = None,
index_columns: Optional[List[str]] = None,
bold_headers: Optional[bool] = None,
na_rep: Optional[str] = None,
inf_value: Optional[str] = None
) -> "FastExcel"
Set number formatting and column styling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
float_format
|
Optional[str]
|
Excel number format for floats (e.g. |
None
|
datetime_format
|
Optional[str]
|
Excel number format for datetimes
(default |
None
|
index_columns
|
Optional[List[str]]
|
Column names to render bold. |
None
|
bold_headers
|
Optional[bool]
|
Whether to render header row in bold. |
None
|
na_rep
|
Optional[str]
|
Text written for |
None
|
inf_value
|
Optional[str]
|
Text written for |
None
|
freeze ¶
freeze(
*,
row: Optional[int] = None,
col: Optional[int] = None,
sheet: Optional[str] = None
) -> "FastExcel"
Configure freeze panes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row
|
Optional[int]
|
Freeze panes above this row number. |
None
|
col
|
Optional[int]
|
Freeze panes to the left of this column number. |
None
|
sheet
|
Optional[str]
|
Apply to a specific sheet only. If |
None
|
sheet ¶
sheet(
name: str,
data: Any,
*,
column_width: Optional[float] = None,
column_widths: Optional[
Union[Dict[str, float], List[float]]
] = None,
column_formats: Optional[
Union[Dict[str, "Format"], List["Format"]]
] = None,
header_format: Optional["Format"] = None,
dedupe_strings: bool = False,
header_row: int = 0,
merge_ranges: Optional[List[Tuple]] = None,
row_heights: Optional[Dict[int, float]] = None,
row_formats: Optional[Dict[int, "Format"]] = None,
banded_rows: Optional[str] = None,
autofilter: bool = False,
url_columns: Optional[
Union[List[str], Dict[str, str]]
] = None,
totals_row: Optional[Dict[str, str]] = None,
totals_label: Optional[str] = None,
totals_format: Optional["Format"] = None,
formula_columns: Optional[Dict[str, str]] = None,
page_setup: Optional[Dict[str, Any]] = None,
conditional_formats: Optional[Dict[str, Any]] = None,
sheet_view: Optional[Dict[str, Any]] = None,
ignore_errors: Optional[
Union[List[str], Dict[str, str]]
] = None,
data_validations: Optional[
Dict[str, Dict[str, Any]]
] = None,
outline: Optional[Dict[str, Any]] = None,
notes: Optional[Dict[str, Any]] = None,
images: Optional[List[Dict[str, Any]]] = None,
sparklines: Optional[Dict[str, Dict[str, Any]]] = None,
charts: Optional[List[Dict[str, Any]]] = None
) -> "FastExcel"
Add a worksheet with data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Sheet name (≤ 31 chars, no |
required |
data
|
Any
|
List of dicts, generator of dicts, or pandas DataFrame. |
required |
column_width
|
Optional[float]
|
Uniform width applied to every column of this sheet. |
None
|
column_widths
|
Optional[Union[Dict[str, float], List[float]]]
|
Per-column width — a dict keyed by header name
( |
None
|
column_formats
|
Optional[Union[Dict[str, 'Format'], List['Format']]]
|
Per-column :class: |
None
|
header_format
|
Optional['Format']
|
:class: |
None
|
dedupe_strings
|
bool
|
Store repeated strings once in the workbook's
shared-string table instead of inline, which can shrink the
|
False
|
header_row
|
int
|
0-based row the header is written on; data follows it. Raise it to leave room for merged banner headers above. |
0
|
merge_ranges
|
Optional[List[Tuple]]
|
Merged cells, as
|
None
|
row_heights
|
Optional[Dict[int, float]]
|
|
None
|
row_formats
|
Optional[Dict[int, 'Format']]
|
|
None
|
banded_rows
|
Optional[str]
|
Background colour ( |
None
|
autofilter
|
bool
|
Add Excel's filter dropdowns over the header row and its
data. The range is computed from the rows actually written, so
it follows |
False
|
url_columns
|
Optional[Union[List[str], Dict[str, str]]]
|
Columns whose text cells become clickable links.
A list names them and each cell shows the URL —
|
None
|
totals_row
|
Optional[Dict[str, str]]
|
Skipped entirely when there are no data rows, since the range
would be empty. NOTE: the formulas carry no computed result, so
readers that use cached values ( |
None
|
totals_label
|
Optional[str]
|
Text for the first column of the totals row, e.g.
|
None
|
totals_format
|
Optional['Format']
|
:class: |
None
|
formula_columns
|
Optional[Dict[str, str]]
|
The formula text is passed through to Excel unchanged, so
anything Excel accepts works: nested calls, |
None
|
page_setup
|
Optional[Dict[str, Any]]
|
Page and print settings as one mapping, because Excel
has about twenty of them and a keyword each would double this
signature. Keys: |
None
|
conditional_formats
|
Optional[Dict[str, Any]]
|
Per-column conditional formatting, as
Rules cover the column's data rows only — never the header — and the range follows the rows actually written, so no manual bounds. An unknown column warns and is skipped; an unknown type or criteria raises. |
None
|
sheet_view
|
Optional[Dict[str, Any]]
|
How the sheet presents on screen, as one mapping:
|
None
|
ignore_errors
|
Optional[Union[List[str], Dict[str, str]]]
|
Suppress Excel's green error triangles on a column.
A list of column names means |
None
|
data_validations
|
Optional[Dict[str, Dict[str, Any]]]
|
Per-column data validation, as
Any rule also accepts |
None
|
outline
|
Optional[Dict[str, Any]]
|
Collapsible row and column groups — the NOTE: a row group takes this sheet out of constant-memory
mode, the same trade-off as |
None
|
notes
|
Optional[Dict[str, Any]]
|
Notes on header cells, as |
None
|
images
|
Optional[List[Dict[str, Any]]]
|
Images anchored to cells, as a list of dicts. Each needs
|
None
|
sparklines
|
Optional[Dict[str, Dict[str, Any]]]
|
A one-cell chart per data row, as
Also takes The target column must already exist: appending one would mean
reaching into the header assembly and column accounting that
|
None
|
charts
|
Optional[List[Dict[str, Any]]]
|
Charts anchored to a cell, as a list of dicts. Each needs a
Types: Also takes |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the sheet name is invalid (validated on save), or a merge range overlaps the header/data rows. |
save ¶
save() -> None
Write all sheets to the target file or buffer.
Writes the format given as output_format, or the one implied by the
target's extension: .csv → CSV, .tsv → TSV, anything else
(including a buffer) → Excel.
Raises:
| Type | Description |
|---|---|
ValueError
|
If no sheets have been added. |
OSError
|
If there are filesystem errors while writing. |
validate_sheet_name ¶
validate_sheet_name(name: str) -> bool
Check whether name is a valid Excel sheet name.
Rules: ≤ 31 characters, no [ ] : * ? / \, not empty.
Examples:
>>> validate_sheet_name("Sheet1")
True
>>> validate_sheet_name("Sheet[1]")
False
write_csv ¶
write_csv(
records: SheetData,
file_name: FileTarget,
delimiter: Optional[str] = None,
sanitize_formulas: bool = False,
bom: bool = False,
columns: Optional[List[str]] = None,
header: bool = True,
na_rep: Optional[str] = None,
inf_value: Optional[str] = None,
) -> None
Write data to a CSV file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
records
|
SheetData
|
Data to write – a list of dicts, a generator of dicts,
a pandas |
required |
file_name
|
FileTarget
|
Destination file path or writable binary buffer. |
required |
delimiter
|
Optional[str]
|
Column delimiter (default |
None
|
sanitize_formulas
|
bool
|
When |
False
|
bom
|
bool
|
Prefix the UTF-8 byte order mark. Excel on Windows reads a BOM-less UTF-8 file as the system code page, which turns non-ASCII text into mojibake; this is the fix. Off by default so output stays byte-identical for pipelines that parse it. |
False
|
columns
|
Optional[List[str]]
|
Select and order the output columns by name. A name that is
not in the data raises |
None
|
header
|
bool
|
Write the header row. Set |
True
|
na_rep
|
Optional[str]
|
Text written for a missing value — |
None
|
inf_value
|
Optional[str]
|
Text written for |
None
|
Examples:
>>> write_csv([{"Name": "Alice", "Age": 30}], "out.csv")
write_worksheet ¶
write_worksheet(
records: SheetData,
file_name: FileTarget,
sheet_name: Optional[str] = None,
password: Optional[str] = None,
freeze_row: Optional[int] = None,
freeze_col: Optional[int] = None,
float_format: Optional[str] = None,
datetime_format: Optional[str] = None,
index_columns: Optional[List[str]] = None,
autofit: bool = True,
bold_headers: bool = False,
column_width: Optional[float] = None,
column_widths: Optional[ColumnWidths] = None,
column_formats: Optional[ColumnFormats] = None,
header_format: Optional[Format] = None,
dedupe_strings: bool = False,
header_row: int = 0,
merge_ranges: Optional[List[MergeRange]] = None,
row_heights: Optional[Dict[int, float]] = None,
row_formats: Optional[Dict[int, Format]] = None,
banded_rows: Optional[str] = None,
autofilter: bool = False,
url_columns: Optional[UrlColumns] = None,
totals_row: Optional[Dict[str, str]] = None,
totals_label: Optional[str] = None,
totals_format: Optional[Format] = None,
formula_columns: Optional[Dict[str, str]] = None,
na_rep: Optional[str] = None,
inf_value: Optional[str] = None,
page_setup: Optional[PageSetup] = None,
conditional_formats: Optional[
ConditionalFormats
] = None,
sheet_view: Optional[SheetView] = None,
ignore_errors: Optional[IgnoreErrors] = None,
data_validations: Optional[DataValidations] = None,
outline: Optional[Outline] = None,
notes: Optional[Notes] = None,
images: Optional[Images] = None,
sparklines: Optional[Sparklines] = None,
charts: Optional[Charts] = None,
) -> None
Write data to a single worksheet in an Excel file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
records
|
SheetData
|
Data to write – a list of dicts, a generator of dicts,
or a pandas |
required |
file_name
|
FileTarget
|
Destination file path ( |
required |
sheet_name
|
Optional[str]
|
Worksheet name (default |
None
|
password
|
Optional[str]
|
Optional password to protect the workbook. |
None
|
freeze_row
|
Optional[int]
|
Freeze panes above this row number. |
None
|
freeze_col
|
Optional[int]
|
Freeze panes to the left of this column number. |
None
|
float_format
|
Optional[str]
|
Excel number format for floats (e.g. |
None
|
index_columns
|
Optional[List[str]]
|
Column names that should be rendered bold. |
None
|
autofit
|
bool
|
Automatically adjust column widths (default |
True
|
column_width
|
Optional[float]
|
Uniform width applied to every column. |
None
|
column_widths
|
Optional[ColumnWidths]
|
Per-column width — a dict keyed by header name or a positional list. |
None
|
dedupe_strings
|
bool
|
Store repeated strings once in the shared-string table instead of inline. Shrinks files with heavily repeated text, at the cost of buffering the sheet in memory (disables constant-memory mode). Off by default. |
False
|
header_row
|
int
|
0-based row the header is written on; data follows it. |
0
|
merge_ranges
|
Optional[List[MergeRange]]
|
|
None
|
row_heights
|
Optional[Dict[int, float]]
|
|
None
|
row_formats
|
Optional[Dict[int, Format]]
|
|
None
|
banded_rows
|
Optional[str]
|
Background colour shaded onto every other data row. |
None
|
autofilter
|
bool
|
Add filter dropdowns over the header row and its data. |
False
|
url_columns
|
Optional[UrlColumns]
|
Columns whose text cells become clickable links. A list
names them and the cell shows the URL; a dict maps each link column
to the column holding its display text
( |
None
|
totals_row
|
Optional[Dict[str, str]]
|
|
None
|
totals_label
|
Optional[str]
|
Text for the first column of the totals row. |
None
|
totals_format
|
Optional[Format]
|
Format applied to the whole totals row. |
None
|
formula_columns
|
Optional[Dict[str, str]]
|
|
None
|
na_rep
|
Optional[str]
|
Text written for a missing value — |
None
|
inf_value
|
Optional[str]
|
Text written for |
None
|
page_setup
|
Optional[PageSetup]
|
Page and print settings, as one mapping — Excel has about
twenty of them and a keyword each would double this signature.
Keys: |
None
|
conditional_formats
|
Optional[ConditionalFormats]
|
Per-column conditional formatting, as
Rules cover the column's data rows only, never the header, and the range follows the rows actually written. An unknown column warns and is skipped; an unknown type or criteria raises. |
None
|
sheet_view
|
Optional[SheetView]
|
How the sheet presents on screen, as one mapping:
|
None
|
ignore_errors
|
Optional[IgnoreErrors]
|
Suppress Excel's green error triangles on a column.
A list of column names means |
None
|
data_validations
|
Optional[DataValidations]
|
Per-column data validation, as
Any rule also takes |
None
|
outline
|
Optional[Outline]
|
Collapsible row and column groups — the +/- brackets in Excel's margin — as one mapping:
NOTE: asking for a row group takes the sheet out of constant-memory
mode, the same trade-off as |
None
|
notes
|
Optional[Notes]
|
Notes on header cells, as |
None
|
images
|
Optional[Images]
|
Images anchored to cells, as a list of dicts. Each needs
|
None
|
sparklines
|
Optional[Sparklines]
|
A one-cell chart per data row, as
Also takes The target column has to exist already: appending one would mean
reaching into the header assembly and column accounting that
|
None
|
charts
|
Optional[Charts]
|
Charts anchored to a cell, as a list of dicts. Each needs a
Types: Also takes |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Invalid sheet name or unsupported data type. |
OSError
|
File system error while writing. |
Examples:
>>> write_worksheet([{"Name": "Alice", "Age": 30}], "out.xlsx")
write_worksheets ¶
write_worksheets(
records_with_sheet_name: List[SheetEntry],
file_name: FileTarget,
password: Optional[str] = None,
freeze_panes: Optional[FreezePanesConfig] = None,
float_format: Optional[str] = None,
datetime_format: Optional[str] = None,
index_columns: Optional[List[str]] = None,
autofit: bool = True,
bold_headers: bool = False,
column_width: Optional[Dict[str, float]] = None,
column_widths: Optional[Dict[str, ColumnWidths]] = None,
column_formats: Optional[
Dict[str, ColumnFormats]
] = None,
header_format: Optional[Dict[str, Format]] = None,
dedupe_strings: Optional[Dict[str, bool]] = None,
header_row: Optional[Dict[str, int]] = None,
merge_ranges: Optional[
Dict[str, List[MergeRange]]
] = None,
row_heights: Optional[
Dict[str, Dict[int, float]]
] = None,
row_formats: Optional[
Dict[str, Dict[int, Format]]
] = None,
banded_rows: Optional[Dict[str, str]] = None,
autofilter: Optional[Dict[str, bool]] = None,
url_columns: Optional[Dict[str, UrlColumns]] = None,
totals_row: Optional[Dict[str, Dict[str, str]]] = None,
totals_label: Optional[Dict[str, str]] = None,
totals_format: Optional[Dict[str, Format]] = None,
formula_columns: Optional[
Dict[str, Dict[str, str]]
] = None,
na_rep: Optional[str] = None,
inf_value: Optional[str] = None,
page_setup: Optional[Dict[str, PageSetup]] = None,
conditional_formats: Optional[
Dict[str, ConditionalFormats]
] = None,
sheet_view: Optional[Dict[str, SheetView]] = None,
ignore_errors: Optional[Dict[str, IgnoreErrors]] = None,
data_validations: Optional[
Dict[str, DataValidations]
] = None,
outline: Optional[Dict[str, Outline]] = None,
notes: Optional[Dict[str, Notes]] = None,
images: Optional[Dict[str, Images]] = None,
sparklines: Optional[Dict[str, Sparklines]] = None,
charts: Optional[Dict[str, Charts]] = None,
) -> None
Write data to multiple worksheets in an Excel file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
records_with_sheet_name
|
List[SheetEntry]
|
A list of |
required |
file_name
|
FileTarget
|
Destination file path or writable binary buffer. |
required |
password
|
Optional[str]
|
Optional password to protect the workbook. |
None
|
freeze_panes
|
Optional[FreezePanesConfig]
|
Per-sheet and/or general freeze-pane config. |
None
|
float_format
|
Optional[str]
|
Excel number format for floats (e.g. |
None
|
index_columns
|
Optional[List[str]]
|
Column names that should be rendered bold. |
None
|
autofit
|
bool
|
Automatically adjust column widths (default |
True
|
column_width
|
Optional[Dict[str, float]]
|
Uniform width per sheet — dict keyed by sheet name ( |
None
|
column_widths
|
Optional[Dict[str, ColumnWidths]]
|
Per-column width per sheet — dict keyed by sheet name mapping to :data: |
None
|
dedupe_strings
|
Optional[Dict[str, bool]]
|
Per-sheet shared-string deduplication — dict keyed by
sheet name ( |
None
|
header_row
|
Optional[Dict[str, int]]
|
Per-sheet header row index — dict keyed by sheet name. |
None
|
merge_ranges
|
Optional[Dict[str, List[MergeRange]]]
|
Per-sheet merged cells — dict keyed by sheet name. |
None
|
row_heights
|
Optional[Dict[str, Dict[int, float]]]
|
Per-sheet row heights — dict keyed by sheet name. |
None
|
row_formats
|
Optional[Dict[str, Dict[int, Format]]]
|
Per-sheet row formats — dict keyed by sheet name. |
None
|
banded_rows
|
Optional[Dict[str, str]]
|
Per-sheet alternating row colour — dict keyed by sheet name. |
None
|
autofilter
|
Optional[Dict[str, bool]]
|
Per-sheet filter dropdowns — dict keyed by sheet name. |
None
|
url_columns
|
Optional[Dict[str, UrlColumns]]
|
Per-sheet link columns — dict keyed by sheet name. |
None
|
totals_row
|
Optional[Dict[str, Dict[str, str]]]
|
Per-sheet totals formulas — dict keyed by sheet name. |
None
|
totals_label
|
Optional[Dict[str, str]]
|
Per-sheet totals label — dict keyed by sheet name. |
None
|
totals_format
|
Optional[Dict[str, Format]]
|
Per-sheet totals row format — dict keyed by sheet name. |
None
|
formula_columns
|
Optional[Dict[str, Dict[str, str]]]
|
Per-sheet computed columns — dict keyed by sheet name. |
None
|
na_rep
|
Optional[str]
|
Text written for a missing value — |
None
|
inf_value
|
Optional[str]
|
Text written for |
None
|
page_setup
|
Optional[Dict[str, PageSetup]]
|
Page and print settings, as one mapping — Excel has about
twenty of them and a keyword each would double this signature.
Keys: |
None
|
conditional_formats
|
Optional[Dict[str, ConditionalFormats]]
|
Per-column conditional formatting, as
Rules cover the column's data rows only, never the header, and the range follows the rows actually written. An unknown column warns and is skipped; an unknown type or criteria raises. |
None
|
sheet_view
|
Optional[Dict[str, SheetView]]
|
How the sheet presents on screen, as one mapping:
|
None
|
ignore_errors
|
Optional[Dict[str, IgnoreErrors]]
|
Suppress Excel's green error triangles on a column.
A list of column names means |
None
|
data_validations
|
Optional[Dict[str, DataValidations]]
|
Per-column data validation, as
Any rule also takes |
None
|
outline
|
Optional[Dict[str, Outline]]
|
Collapsible row and column groups — the +/- brackets in Excel's margin — as one mapping:
NOTE: asking for a row group takes the sheet out of constant-memory
mode, the same trade-off as |
None
|
notes
|
Optional[Dict[str, Notes]]
|
Notes on header cells, as |
None
|
images
|
Optional[Dict[str, Images]]
|
Images anchored to cells, as a list of dicts. Each needs
|
None
|
sparklines
|
Optional[Dict[str, Sparklines]]
|
A one-cell chart per data row, as
Also takes The target column has to exist already: appending one would mean
reaching into the header assembly and column accounting that
|
None
|
charts
|
Optional[Dict[str, Charts]]
|
Charts anchored to a cell, as a list of dicts. Each needs a
Types: Also takes |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Invalid sheet name or unsupported data type. |
OSError
|
File system error while writing. |
Examples:
>>> write_worksheets(
... [("Users", [{"Name": "Alice"}]), ("Items", [{"SKU": "A1"}])],
... "multi.xlsx",
... )