Skip to content

Conversation

@antznette1
Copy link

@antznette1 antznette1 commented Oct 12, 2025

Reference Closes #62651.
Usage example:

df.to_excel("out.xlsx", engine="xlsxwriter", engine_kwargs={"autofilter_header": True, "header_bold": True}) 

Notes on behavior, MultiIndex headers considered as first header row for bolding, and not changing defaults.
I can add a docs follow-up and then commit to the same PR to_excel with a short example showing engine_kwargs={"autofilter_header": True, "header_bold": True}

Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm negative on using engine_kwargs here, I added thoughts to the linked issue.

@rhshadrach rhshadrach added Enhancement IO Excel read_excel, to_excel labels Oct 13, 2025
@antznette1
Copy link
Author

@rhshadrach
Thank you, I will go through it and make a commit

@antznette1 antznette1 changed the title Title: ENH: to_excel engine_kwargs for Excel header Autofilter and optional bold (xlsxwriter/openpyxl) ENH: to_excel engine_kwargs for Excel header Autofilter and optional bold (xlsxwriter/openpyxl) Oct 16, 2025
@antznette1 antznette1 requested a review from attack68 as a code owner October 18, 2025 10:53
@antznette1
Copy link
Author

@rhshadrach
Please can you review the updated commit I made to the feature?

@antznette1 antznette1 requested a review from rhshadrach October 19, 2025 17:29
Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me there a lot changing here, far more than just adding the autofilter. Can you comment below on why some of these changes are being made.

Comment on lines +104 to +105
# Map CSS font-weight to xlsxwriter font-weight (bold)
if style_dict.get("font-weight") in ("bold", "bolder", 700, "700") or (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this changing?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is meant to properly handle CSS-like font weights (e.g., "bold", "bolder", 700) in the xlsxwriter engine, similar to how openpyxl handles them. It ensures consistent behavior across different Excel writers.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inf_rep=inf_rep,
)
formatter.write(
formatter.to_excel(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this changing?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was meant to maintain consistency with the ExcelFormatter API. The [write()](cci:1: .../pandas/pandas/io/formats/excel.py:960:4-982:9) method was deprecated in favor of [to_excel()](cci:1: .../pandas/pandas/io/formats/style.py:559:4-619:9), which provides better alignment with pandas' method naming conventions.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +246 to +252
# Process other font properties
for style_key, font_key in key_map.items():
if style_key in style_dict and style_key not in ("b", "bold"):
value = style_dict[style_key]
if font_key == "color" and value is not None:
value = cls._convert_to_color(value)
font_kwargs[font_key] = value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this changing?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to improve the font property handling to be more robust, particularly for color conversion, thereby ensuring that color values are properly converted to openpyxl Color objects when processing font styles.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +232 to +234
# Let xlsxwriter raise its own TypeError to satisfy tests
# expecting that error
self._book = Workbook(self._handles.handle, **self._engine_kwargs) # type: ignore[arg-type]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this changing?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was made to let xlsxwriter raise its own TypeError with a more specific message when there are issues with workbook creation. The comment was updated to be more descriptive of this behavior.

@antznette1 antznette1 requested a review from rhshadrach October 24, 2025 11:33
@rhshadrach
Copy link
Member

Unrelated changes should go in separate PRs - I think I see at least three here:

  • Autofilter
  • Font changes
  • Error message changes

Can you split these out into separate PRs.

@antznette1
Copy link
Author

Unrelated changes should go in separate PRs - I think I see at least three here:

  • Autofilter
  • Font changes
  • Error message changes

Can you split these out into separate PRs.

Okay, I'll get to it now

@antznette1 antznette1 closed this Oct 26, 2025
@antznette1 antznette1 reopened this Oct 26, 2025
@antznette1
Copy link
Author

I will close this PR and create the seperate PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement IO Excel read_excel, to_excel

2 participants