diff --git a/py-polars/polars/dataframe/frame.py b/py-polars/polars/dataframe/frame.py index 6c26ed7feb2c..cf23828c420b 100644 --- a/py-polars/polars/dataframe/frame.py +++ b/py-polars/polars/dataframe/frame.py @@ -3186,6 +3186,7 @@ def write_excel( dtype_formats=dtype_formats, header_format=header_format, float_precision=float_precision, + table_style=table_style, row_totals=row_totals, sparklines=sparklines, formulas=formulas, diff --git a/py-polars/polars/io/spreadsheet/_write_utils.py b/py-polars/polars/io/spreadsheet/_write_utils.py index ab97658aa206..6509eaa54390 100644 --- a/py-polars/polars/io/spreadsheet/_write_utils.py +++ b/py-polars/polars/io/spreadsheet/_write_utils.py @@ -48,8 +48,6 @@ def _cluster(iterable: Iterable[Any], n: int = 2) -> Iterable[Any]: Date: "yyyy-mm-dd;@", Time: "hh:mm:ss;@", } -for tp in INTEGER_DTYPES: - _XL_DEFAULT_DTYPE_FORMATS_[tp] = _XL_DEFAULT_INTEGER_FORMAT_ class _XLFormatCache: @@ -331,6 +329,7 @@ def _xl_setup_table_columns( formulas: dict[str, str | dict[str, str]] | None = None, row_totals: RowTotalsDefinition | None = None, float_precision: int = 3, + table_style: dict[str, Any] | str | None = None, ) -> tuple[list[dict[str, Any]], dict[str | tuple[str, ...], str], DataFrame]: """Setup and unify all column-related formatting/defaults.""" @@ -428,13 +427,19 @@ def _map_str(s: Series) -> Series: # seed format cache with default fallback format fmt_default = format_cache.get({"valign": "vcenter"}) - # default float format + # default float format; account for dark styles + if table_style is None or "table style dark" not in str(table_style).lower(): + int_base_fmt = _XL_DEFAULT_INTEGER_FORMAT_ + flt_base_fmt = _XL_DEFAULT_FLOAT_FORMAT_ + else: + int_base_fmt = _XL_DEFAULT_INTEGER_FORMAT_.split(";", 1)[0] + flt_base_fmt = _XL_DEFAULT_FLOAT_FORMAT_.split(";", 1)[0] + + for tp in INTEGER_DTYPES: + _XL_DEFAULT_DTYPE_FORMATS_[tp] = int_base_fmt + zeros = "0" * float_precision - fmt_float = ( - _XL_DEFAULT_INTEGER_FORMAT_ - if not zeros - else _XL_DEFAULT_FLOAT_FORMAT_.replace(".000", f".{zeros}") - ) + fmt_float = int_base_fmt if not zeros else flt_base_fmt.replace(".000", f".{zeros}") # assign default dtype formats for tp, fmt in _XL_DEFAULT_DTYPE_FORMATS_.items(): diff --git a/py-polars/tests/unit/io/test_spreadsheet.py b/py-polars/tests/unit/io/test_spreadsheet.py index 744c029ae059..d1c3aae42186 100644 --- a/py-polars/tests/unit/io/test_spreadsheet.py +++ b/py-polars/tests/unit/io/test_spreadsheet.py @@ -545,7 +545,7 @@ def test_read_excel_all_sheets_with_sheet_name(path_xlsx: Path, engine: str) -> # basic formatting { "autofit": True, - "table_style": "Table Style Light 16", + "table_style": "Table Style Dark 2", "column_totals": True, "float_precision": 0, },