Skip to content

Commit

Permalink
fix(python): Ensure explicit values given to column_widths override…
Browse files Browse the repository at this point in the history
… autofit in `write_excel` (#20893)
  • Loading branch information
arnabanimesh authored Jan 24, 2025
1 parent c465164 commit 884feeb
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3542,6 +3542,18 @@ def write_excel(
else:
hidden = set(_expand_selectors(df, hidden_columns))

# Autofit section needs to be present above column_widths section
# to ensure that parameters provided in the column_widths section
# are not overwritten by autofit
#
# table/rows all written; apply (optional) autofit
if autofit and not is_empty:
xlv = xlsxwriter.__version__
if parse_version(xlv) < (3, 0, 8):
msg = f"`autofit=True` requires xlsxwriter 3.0.8 or higher, found {xlv}"
raise ModuleUpgradeRequiredError(msg)
ws.autofit()

if isinstance(column_widths, int):
column_widths = dict.fromkeys(df.columns, column_widths)
else:
Expand Down Expand Up @@ -3588,14 +3600,6 @@ def write_excel(
for idx, height in _unpack_multi_column_dict(row_heights).items(): # type: ignore[assignment]
ws.set_row_pixels(idx, height)

# table/rows all written; apply (optional) autofit
if autofit and not is_empty:
xlv = xlsxwriter.__version__
if parse_version(xlv) < (3, 0, 8):
msg = f"`autofit=True` requires xlsxwriter 3.0.8 or higher, found {xlv}"
raise ModuleUpgradeRequiredError(msg)
ws.autofit()

if freeze_panes:
if isinstance(freeze_panes, str):
ws.freeze_panes(freeze_panes)
Expand Down

0 comments on commit 884feeb

Please sign in to comment.