pola-rs/polars · error · ValueError

invalid table style key

Error message

invalid table style key: {key!r}

What it means

When table_style is given as a dict to write_excel, only a fixed set of keys (e.g. 'style', banded rows/columns, first/last_column) are valid. This ValueError fires for any unknown key, catching typos in the styling options dict.

Solutions

  1. Check valid keys: style, banded_rows, banded_columns, first_column, last_column, row_totals, column_totals etc., and correct the key spelling.
  2. Pass just a style name string if you only need a table style: table_style='TableStyleMedium9'.
  3. Inspect the write_excel signature/docstring for the current accepted table_style keys in your polars version.

Example fix

// before
df.write_excel('out.xlsx', table_style={'stlye': 'TableStyleMedium9'})
// after
df.write_excel('out.xlsx', table_style={'style': 'TableStyleMedium9'})
Defensive patterns

Strategy: validation

Validate before calling

VALID = {'style','banded_rows','banded_columns','first_column','last_column','row_totals','column_totals'}
bad = set(table_style) - VALID
assert not bad, f'unknown table_style keys: {bad}'

Prevention

When it happens

Trigger: write_excel(table_style={'stlye': 'TableStyleMedium9'}) or any misspelled/unsupported key such as 'banded_col' instead of 'banded_columns'.

Common situations: Typos in style dict keys, copying options from a different library (xlsxwriter/openpyxl) that uses different key names, or version changes to valid keys.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/152d7c3b670a8458. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/io/spreadsheet/_write_utils.py:556

    return table_columns, column_formats, df  # type: ignore[return-value]


def _xl_setup_table_options(
    table_style: dict[str, Any] | str | None,
) -> tuple[dict[str, Any] | str | None, dict[str, Any]]:
    """Setup table options, distinguishing style name from other formatting."""
    if isinstance(table_style, dict):
        valid_options = (
            "style",
            "banded_columns",
            "banded_rows",
            "first_column",
            "last_column",
        )
        for key in table_style:
            if key not in valid_options:
                msg = f"invalid table style key: {key!r}"
                raise ValueError(msg)

        table_options = table_style.copy()
        table_style = table_options.pop("style", None)
    else:
        table_options = {}

    return table_style, table_options


@overload
def _xl_worksheet_in_workbook(
    wb: Workbook, ws: Worksheet, *, return_worksheet: Literal[False] = ...
) -> bool: ...
@overload
def _xl_worksheet_in_workbook(
    wb: Workbook, ws: Worksheet, *, return_worksheet: Literal[True]
) -> Worksheet: ...

View on GitHub (pinned to fe841f959e)