-  
-   Notifications  You must be signed in to change notification settings 
- Fork 19.2k
ENH: to_excel engine_kwargs for Excel header Autofilter and optional bold (xlsxwriter/openpyxl) #62670
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
Conversation
…sxwriter/openpyxl); tests. Closes pandas-dev#62651
There was a problem hiding this 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 | 
… lines to meet line length requirements- Improve code formatting and readability- Ensure imports are properly sorted
…restore ExcelFormatter.write(); xlsxwriter: raise on append and avoid double-close warning (stacklevel test).
| @rhshadrach | 
There was a problem hiding this 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.
| # Map CSS font-weight to xlsxwriter font-weight (bold) | ||
| if style_dict.get("font-weight") in ("bold", "bolder", 700, "700") or ( | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this changing?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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( | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this changing?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| # 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 | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this changing?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| # 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] | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this changing?
There was a problem hiding this comment.
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.
| Unrelated changes should go in separate PRs - I think I see at least three here: 
 Can you split these out into separate PRs. | 
| 
 Okay, I'll get to it now | 
| I will close this PR and create the seperate PRs. | 
Reference Closes #62651.
Usage example:
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}