Skip to content

ENH: Added autofilter parameter to Excel output for both xlsxwriter and openpyxl engines #42560

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

Closed
wants to merge 4 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 doc/source/user_guide/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3786,6 +3786,7 @@ The look and feel of Excel worksheets created from pandas can be modified using

* ``float_format`` : Format string for floating point numbers (default ``None``).
* ``freeze_panes`` : A tuple of two integers representing the bottommost row and rightmost column to freeze. Each of these parameters is one-based, so (1, 1) will freeze the first row and first column (default ``None``).
* ``autofilter`` : A boolean specifying whether to activate Excel's auto filters on the columns of the Excel table. Auto filters allow users to filter and sort the columns by values.

Using the `Xlsxwriter`_ engine provides many options for controlling the
format of an Excel worksheet created with the ``to_excel`` method. Excellent examples can be found in the
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Other enhancements
- Add support for assigning values to ``by`` argument in :meth:`DataFrame.plot.hist` and :meth:`DataFrame.plot.box` (:issue:`15079`)
- :meth:`Series.sample`, :meth:`DataFrame.sample`, and :meth:`.GroupBy.sample` now accept a ``np.random.Generator`` as input to ``random_state``. A generator will be more performant, especially with ``replace=False`` (:issue:`38100`)
- :meth:`Series.ewm`, :meth:`DataFrame.ewm`, now support a ``method`` argument with a ``'table'`` option that performs the windowing operation over an entire :class:`DataFrame`. See :ref:`Window Overview <window.overview>` for performance and functional benefits (:issue:`42273`)
- ``DataFrame.to_excel()`` has a new ``autofilter`` parameter to turn on Auto Filter when exporting to Excel (:issue:`15307`)
- :meth:`.GroupBy.cummin` and :meth:`.GroupBy.cummax` now support the argument ``skipna`` (:issue:`34047`)
-

Expand Down
5 changes: 5 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2148,6 +2148,7 @@ def to_excel(
inf_rep="inf",
verbose=True,
freeze_panes=None,
autofilter=False,
storage_options: StorageOptions = None,
) -> None:
"""
Expand Down Expand Up @@ -2213,6 +2214,9 @@ def to_excel(
freeze_panes : tuple of int (length 2), optional
Specifies the one-based bottommost row and rightmost column that
is to be frozen.
autofilter : bool, default False
Specifies whether filters should be added to the columns in the
spreadsheet.
{storage_options}

.. versionadded:: 1.2.0
Expand Down Expand Up @@ -2289,6 +2293,7 @@ def to_excel(
startrow=startrow,
startcol=startcol,
freeze_panes=freeze_panes,
autofilter=autofilter,
engine=engine,
storage_options=storage_options,
)
Expand Down
8 changes: 7 additions & 1 deletion pandas/io/excel/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,13 @@ def engine(self):

@abc.abstractmethod
def write_cells(
self, cells, sheet_name=None, startrow=0, startcol=0, freeze_panes=None
self,
cells,
sheet_name=None,
startrow=0,
startcol=0,
freeze_panes=None,
autofilter=None,
):
"""
Write given formatted cells into Excel an excel sheet
Expand Down
1 change: 1 addition & 0 deletions pandas/io/excel/_odswriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def write_cells(
startrow: int = 0,
startcol: int = 0,
freeze_panes: tuple[int, int] | None = None,
autofilter: bool = False,
) -> None:
"""
Write the frame cells using odf
Expand Down
21 changes: 20 additions & 1 deletion pandas/io/excel/_openpyxl.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,13 @@ def _convert_to_protection(cls, protection_dict):
return Protection(**protection_dict)

def write_cells(
self, cells, sheet_name=None, startrow=0, startcol=0, freeze_panes=None
self,
cells,
sheet_name=None,
startrow=0,
startcol=0,
freeze_panes=None,
autofilter=False,
):
# Write the frame cells using openpyxl.
sheet_name = self._get_sheet_name(sheet_name)
Expand Down Expand Up @@ -454,6 +460,9 @@ def write_cells(
row=freeze_panes[0] + 1, column=freeze_panes[1] + 1
)

max_row = 0
max_col = 0

for cell in cells:
xcell = wks.cell(
row=startrow + cell.row + 1, column=startcol + cell.col + 1
Expand Down Expand Up @@ -501,6 +510,16 @@ def write_cells(
for k, v in style_kwargs.items():
setattr(xcell, k, v)

if startrow + cell.row > max_row:
max_row = startrow + cell.row
if startcol + cell.col > max_col:
max_col = startcol + cell.col

if autofilter:
from openpyxl.utils import get_column_letter

wks.auto_filter.ref = "A1:" + get_column_letter(max_col + 1) + str(max_row)


class OpenpyxlReader(BaseExcelReader):
def __init__(
Expand Down
19 changes: 18 additions & 1 deletion pandas/io/excel/_xlsxwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,13 @@ def save(self):
return self.book.close()

def write_cells(
self, cells, sheet_name=None, startrow=0, startcol=0, freeze_panes=None
self,
cells,
sheet_name=None,
startrow=0,
startcol=0,
freeze_panes=None,
autofilter=False,
):
# Write the frame cells using xlsxwriter.
sheet_name = self._get_sheet_name(sheet_name)
Expand All @@ -224,6 +230,9 @@ def write_cells(
if validate_freeze_panes(freeze_panes):
wks.freeze_panes(*(freeze_panes))

max_row = 0
max_col = 0

for cell in cells:
val, fmt = self._value_with_fmt(cell.val)

Expand All @@ -248,3 +257,11 @@ def write_cells(
)
else:
wks.write(startrow + cell.row, startcol + cell.col, val, style)

if startrow + cell.row > max_row:
max_row = startrow + cell.row
if startcol + cell.col > max_col:
max_col = startcol + cell.col

if autofilter:
wks.autofilter(0, 0, max_row, max_col)
8 changes: 7 additions & 1 deletion pandas/io/excel/_xlwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ def save(self):
self.book.save(self.handles.handle)

def write_cells(
self, cells, sheet_name=None, startrow=0, startcol=0, freeze_panes=None
self,
cells,
sheet_name=None,
startrow=0,
startcol=0,
freeze_panes=None,
autofilter=False,
):

sheet_name = self._get_sheet_name(sheet_name)
Expand Down
5 changes: 5 additions & 0 deletions pandas/io/formats/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,7 @@ def write(
startrow=0,
startcol=0,
freeze_panes=None,
autofilter=False,
engine=None,
storage_options: StorageOptions = None,
):
Expand All @@ -801,6 +802,9 @@ def write(
freeze_panes : tuple of integer (length 2), default None
Specifies the one-based bottommost row and rightmost column that
is to be frozen
autofilter : bool, default False
Specifies whether filters should be added to the columns in the
spreadsheet.
engine : string, default None
write engine to use if writer is a path - you can also set this
via the options ``io.excel.xlsx.writer``, ``io.excel.xls.writer``,
Expand Down Expand Up @@ -843,6 +847,7 @@ def write(
startrow=startrow,
startcol=startcol,
freeze_panes=freeze_panes,
autofilter=autofilter,
)
finally:
# make sure to close opened file handles
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/io/excel/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,13 @@ def test_freeze_panes(self, path):
result = pd.read_excel(path, index_col=0)
tm.assert_frame_equal(result, expected)

def test_autofilter(self, path):
expected = DataFrame([[1, 2], [3, 4]], columns=["col1", "col2"])
expected.to_excel(path, "Sheet1", autofilter=True)

result = pd.read_excel(path, index_col=0)
tm.assert_frame_equal(result, expected)

def test_path_path_lib(self, engine, ext):
df = tm.makeDataFrame()
writer = partial(df.to_excel, engine=engine)
Expand Down