Skip to content

Pass through Engine kwargs in ExcelWriter #43445

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

Merged
merged 17 commits into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 11 additions & 1 deletion pandas/io/excel/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,8 @@ class ExcelWriter(metaclass=abc.ABCMeta):

.. versionadded:: 1.3.0
engine_kwargs : dict, optional
Keyword arguments to be passed into the engine.
Keyword arguments to be passed into the engine. Only works for
xlsxwriter, openpyxl and xlwt. Does not work in append mode.

.. versionadded:: 1.3.0
**kwargs : dict, optional
Expand Down Expand Up @@ -771,6 +772,15 @@ class ExcelWriter(metaclass=abc.ABCMeta):
... with zf.open("filename.xlsx", "w") as buffer:
... with pd.ExcelWriter(buffer) as writer:
... df.to_excel(writer)

You can specify additional arguments to the underlying engine:

>>> with ExcelWriter(
... "path_to_file.xlsx",
... engine="xlsxwriter",
... engine_kwargs={"options":{"nan_inf_to_errors":True}}
... ) as writer:
... df.to_excel(writer)
"""

# Defining an ExcelWriter implementation (see abstract methods for more...)
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/excel/_openpyxl.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def __init__(

else:
# Create workbook object with default optimized_write=True.
self.book = Workbook()
self.book = Workbook(**engine_kwargs)

if self.book.worksheets:
self.book.remove(self.book.worksheets[0])
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/excel/_xlwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __init__(

if encoding is None:
encoding = "ascii"
self.book = xlwt.Workbook(encoding=encoding)
self.book = xlwt.Workbook(encoding=encoding, **engine_kwargs)
self.fm_datetime = xlwt.easyxf(num_format_str=self.datetime_format)
self.fm_date = xlwt.easyxf(num_format_str=self.date_format)

Expand Down
7 changes: 4 additions & 3 deletions pandas/tests/io/excel/test_openpyxl.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,15 @@ def test_kwargs(ext, write_only):
DataFrame().to_excel(writer)


@pytest.mark.parametrize("write_only", [True, False])
def test_engine_kwargs(ext, write_only):
@pytest.mark.parametrize("iso_dates", [True, False])
def test_engine_kwargs(ext, iso_dates):
# GH 42286
# openpyxl doesn't utilize kwargs, only test that supplying a engine_kwarg works
engine_kwargs = {"write_only": write_only}
engine_kwargs = {"iso_dates": iso_dates}
with tm.ensure_clean(ext) as f:
with ExcelWriter(f, engine="openpyxl", engine_kwargs=engine_kwargs) as writer:
# ExcelWriter won't allow us to close without writing something
assert writer.book.iso_dates == iso_dates
DataFrame().to_excel(writer)


Expand Down
11 changes: 6 additions & 5 deletions pandas/tests/io/excel/test_xlwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,13 @@ def test_kwargs(ext, write_only):
DataFrame().to_excel(writer)


@pytest.mark.parametrize("write_only", [True, False])
def test_engine_kwargs(ext, write_only):
@pytest.mark.parametrize("style_compression", [0, 2])
def test_engine_kwargs(ext, style_compression):
# GH 42286
# xlwt doesn't utilize kwargs, only test that supplying a engine_kwarg works
engine_kwargs = {"write_only": write_only}
#
engine_kwargs = {"style_compression": style_compression}
with tm.ensure_clean(ext) as f:
with ExcelWriter(f, engine="openpyxl", engine_kwargs=engine_kwargs) as writer:
with ExcelWriter(f, engine="xlwt", engine_kwargs=engine_kwargs) as writer:
# xlwt won't allow us to close without writing something
assert writer.book._Workbook__styles.style_compression == style_compression
DataFrame().to_excel(writer)