-
-
Notifications
You must be signed in to change notification settings - Fork 19.3k
ENH: adding autofilter when writing to excel (pandas-dev#61194) #62994
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
base: main
Are you sure you want to change the base?
Changes from all commits
90f75a5
6bc6952
d1b05e8
1365c21
f69c62f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -532,6 +532,8 @@ class ExcelFormatter: | |
| Defaults to ``CSSToExcelConverter()``. | ||
| It should have signature css_declarations string -> excel style. | ||
| This is only called for body cells. | ||
| autofilter : bool, default False | ||
| If True, add automatic filters to all columns | ||
| """ | ||
|
|
||
| max_rows = 2**20 | ||
|
|
@@ -549,6 +551,7 @@ def __init__( | |
| merge_cells: ExcelWriterMergeCells = False, | ||
| inf_rep: str = "inf", | ||
| style_converter: Callable | None = None, | ||
| autofilter: bool = False, | ||
| ) -> None: | ||
| self.rowcounter = 0 | ||
| self.na_rep = na_rep | ||
|
|
@@ -584,6 +587,7 @@ def __init__( | |
| raise ValueError(f"Unexpected value for {merge_cells=}.") | ||
| self.merge_cells = merge_cells | ||
| self.inf_rep = inf_rep | ||
| self.autofilter = autofilter | ||
|
|
||
| def _format_value(self, val): | ||
| if is_scalar(val) and missing.isna(val): | ||
|
|
@@ -873,6 +877,34 @@ def get_formatted_cells(self) -> Iterable[ExcelCell]: | |
| cell.val = self._format_value(cell.val) | ||
| yield cell | ||
|
|
||
| def _num2excel(self, index: int) -> str: | ||
| """ | ||
| Convert 0-based column index to Excel column name. | ||
| Parameters | ||
| ---------- | ||
| index : int | ||
| The numeric column index to convert to a Excel column name. | ||
| Returns | ||
| ------- | ||
| column_name : str | ||
| The column name corresponding to the index. | ||
| Raises | ||
| ------ | ||
| ValueError | ||
| Index is negative | ||
| """ | ||
| if index < 0: | ||
| raise ValueError(f"Index cannot be negative: {index}") | ||
| column_name = "" | ||
| # while loop in case column name needs to be longer than 1 character | ||
| while index > 0 or not column_name: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you raise when index < 0? Also, what's the point of this being a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ignore my So makes sense, but still in need of a cleanup here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @WillAyd Thanks for the feedback - input validation added. |
||
| index, remainder = divmod(index, 26) | ||
| column_name = chr(65 + remainder) + column_name | ||
| return column_name | ||
|
|
||
| @doc(storage_options=_shared_docs["storage_options"]) | ||
| def write( | ||
| self, | ||
|
|
@@ -916,6 +948,13 @@ def write( | |
| f"Max sheet size is: {self.max_rows}, {self.max_cols}" | ||
| ) | ||
|
|
||
| if self.autofilter: | ||
| start = f"{self._num2excel(startcol)}{startrow + 1}" | ||
| end = f"{self._num2excel(startcol + num_cols)}{startrow + num_rows + 1}" | ||
| autofilter_range = f"{start}:{end}" | ||
| else: | ||
| autofilter_range = None | ||
|
|
||
| if engine_kwargs is None: | ||
| engine_kwargs = {} | ||
|
|
||
|
|
@@ -938,6 +977,7 @@ def write( | |
| startrow=startrow, | ||
| startcol=startcol, | ||
| freeze_panes=freeze_panes, | ||
| autofilter_range=autofilter_range, | ||
| ) | ||
| finally: | ||
| # make sure to close opened file handles | ||
|
|
||
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 we should be raising an error here if a user specifies
autofilter=Trueand it is not supported with an engine rather than silently ignore the user's specification.