diff --git a/pandas-stubs/io/excel/_base.pyi b/pandas-stubs/io/excel/_base.pyi index e0347bc65..328010b08 100644 --- a/pandas-stubs/io/excel/_base.pyi +++ b/pandas-stubs/io/excel/_base.pyi @@ -104,7 +104,7 @@ class ExcelWriter: def __init__( self, path: FilePath | WriteExcelBuffer | ExcelWriter, - engine: Literal["auto", "openpyxl", "pyxlsb", "odf"] | None = ..., + engine: Literal["auto", "openpyxl", "odf", "xlsxwriter"] | None = ..., date_format: str | None = ..., datetime_format: str | None = ..., mode: Literal["w", "a"] = ..., @@ -115,11 +115,11 @@ class ExcelWriter: @property def supported_extensions(self) -> tuple[str, ...]: ... @property - def engine(self) -> Literal["openpyxl", "pyxlsb", "odf"]: ... + def engine(self) -> Literal["openpyxl", "odf", "xlsxwriter"]: ... @property def sheets(self) -> dict[str, Any]: ... @property - def book(self) -> Workbook | OpenDocument | pyxlsb.workbook.Workbook: ... + def book(self) -> Workbook | OpenDocument: ... @property def date_format(self) -> str: ... @property diff --git a/pyproject.toml b/pyproject.toml index 3b93b4b7f..bf871a55f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -52,7 +52,8 @@ tables = { version = ">=3.7.0", python = "<3.11" } lxml = { version = ">=4.7.1,<4.9.0", python = "<3.11" } pyreadstat = ">=1.2.0" xlrd = ">=2.0.1" -pyxlsb = ">=1.0.9" +xlsxwriter = ">=3.0.3" +pyxlsb = ">=1.0.10" odfpy = ">=1.4.1" xarray = ">=22.6.0" tabulate = ">=0.8.10" diff --git a/tests/test_io.py b/tests/test_io.py index 38f0bcffe..56300c83b 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -11,6 +11,7 @@ Dict, Generator, List, + Literal, Union, ) @@ -694,12 +695,46 @@ def test_excel_writer(): check(assert_type(ef.close(), None), type(None)) +def test_excel_writer_engine(): + with ensure_clean(".xlsx") as path: + with pd.ExcelWriter(path, engine="auto") as ew: + check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter) + DF.to_excel(ew, sheet_name="A") + + with ensure_clean(".xlsx") as path: + with pd.ExcelWriter(path, engine="openpyxl") as ew: + check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter) + DF.to_excel(ew, sheet_name="A") + check( + assert_type(ew.engine, Literal["openpyxl", "odf", "xlsxwriter"]), + str, + ) + + with ensure_clean(".ods") as path: + with pd.ExcelWriter(path, engine="odf") as ew: + check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter) + DF.to_excel(ew, sheet_name="A") + check( + assert_type(ew.engine, Literal["openpyxl", "odf", "xlsxwriter"]), + str, + ) + + with ensure_clean(".xlsx") as path: + with pd.ExcelWriter(path, engine="xlsxwriter") as ew: + check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter) + DF.to_excel(ew, sheet_name="A") + check( + assert_type(ew.engine, Literal["openpyxl", "odf", "xlsxwriter"]), + str, + ) + + def test_excel_writer_append_mode(): with ensure_clean(".xlsx") as path: with pd.ExcelWriter(path, mode="w") as ew: DF.to_excel(ew, sheet_name="A") - with pd.ExcelWriter(path, mode="a") as ew: - pass + with pd.ExcelWriter(path, mode="a", engine="openpyxl") as ew: + DF.to_excel(ew, sheet_name="B") def test_to_string():