Skip to content
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

BUG: excelwriter engine_kwargs fix #42214

Closed
wants to merge 5 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
10 changes: 10 additions & 0 deletions pandas/io/excel/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,16 @@ class ExcelWriter(metaclass=abc.ABCMeta):
>>> with ExcelWriter("path_to_file.xlsx", mode="a", engine="openpyxl") as writer:
... df.to_excel(writer, sheet_name="Sheet3")

You can specify arguments to the underlying engine. For example to not
calculate the result of a formula:

>>> df = pd.DataFrame(["=1+1"])
... with ExcelWriter(
... "path_to_file.xlsx",
... engine_kwargs={"strings_to_formulas":False}
... ) as writer:
Comment on lines +762 to +765
Copy link
Member

Choose a reason for hiding this comment

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

I think this will only be valid with xlsxwriter, is that right? Can you specify the engine here to make it clear that's what is being used.

... df.to_excel(writer)

You can store Excel file in RAM:

>>> import io
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/excel/_xlsxwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def __init__(
engine_kwargs=engine_kwargs,
)

self.book = Workbook(self.handles.handle, **engine_kwargs)
self.book = Workbook(self.handles.handle, engine_kwargs)
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a test for this.


def save(self):
"""
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/io/excel/test_xlsxwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def test_write_append_mode_raises(ext):
ExcelWriter(f, engine="xlsxwriter", mode="a")



Copy link
Member

Choose a reason for hiding this comment

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

Remove this blank line, it will not pass code checks (two blank lines between functions)

@pytest.mark.parametrize("nan_inf_to_errors", [True, False])
def test_kwargs(ext, nan_inf_to_errors):
# GH 42286
Expand Down