Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(python): Right-align numerical headers in write_excel #17877

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3190,6 +3190,7 @@ def write_excel(
row_totals=row_totals,
sparklines=sparklines,
formulas=formulas,
autofilter=autofilter,
)

# normalise cell refs (eg: "B3" => (2,1)) and establish table start/finish,
Expand Down
17 changes: 13 additions & 4 deletions py-polars/polars/io/spreadsheet/_write_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ def _xl_setup_table_columns(
row_totals: RowTotalsDefinition | None = None,
float_precision: int = 3,
table_style: dict[str, Any] | str | None = None,
autofilter: bool = True, # noqa: FBT001
) -> tuple[list[dict[str, Any]], dict[str | tuple[str, ...], str], DataFrame]:
"""Setup and unify all column-related formatting/defaults."""

Expand Down Expand Up @@ -450,14 +451,25 @@ def _map_str(s: Series) -> Series:
dtype_formats[tp] = fmt

# associate formats/functions with specific columns
header_dict = header_format or {}
add_align = "align" not in header_dict
header_alignment = {"align": "right"}
if autofilter:
header_alignment["indent"] = 2 # type: ignore[assignment]
col_header_format = {}
for col, tp in df.schema.items():
base_type = tp.base_type()
header_fmt = header_format
if base_type in dtype_formats:
fmt = dtype_formats.get(tp, dtype_formats[base_type])
column_formats.setdefault(col, fmt)
if add_align and base_type in [*FLOAT_DTYPES, *INTEGER_DTYPES]:
header_fmt = {**header_dict, **header_alignment}
if col not in column_formats:
column_formats[col] = fmt_default

col_header_format[col] = format_cache.get(header_fmt) if header_fmt else None

# ensure externally supplied formats are made available
for col, fmt in column_formats.items(): # type: ignore[assignment]
if isinstance(fmt, str):
Expand All @@ -473,17 +485,14 @@ def _map_str(s: Series) -> Series:
fmt["valign"] = "vcenter"
column_formats[col] = format_cache.get(fmt)

# optional custom header format
col_header_format = format_cache.get(header_format) if header_format else None

# assemble table columns
table_columns = [
{
k: v
for k, v in {
"header": col,
"format": column_formats[col],
"header_format": col_header_format,
"header_format": col_header_format.get(col),
"total_function": column_total_funcs.get(col),
"formula": (
row_total_funcs.get(col)
Expand Down