Skip to content

Commit 288f9df

Browse files
authored
BUG: Correct ExcelWriter (#472)
closes #462
1 parent 7545d16 commit 288f9df

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

pandas-stubs/io/excel/_base.pyi

+3-3
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class ExcelWriter:
104104
def __init__(
105105
self,
106106
path: FilePath | WriteExcelBuffer | ExcelWriter,
107-
engine: Literal["auto", "openpyxl", "pyxlsb", "odf"] | None = ...,
107+
engine: Literal["auto", "openpyxl", "odf", "xlsxwriter"] | None = ...,
108108
date_format: str | None = ...,
109109
datetime_format: str | None = ...,
110110
mode: Literal["w", "a"] = ...,
@@ -115,11 +115,11 @@ class ExcelWriter:
115115
@property
116116
def supported_extensions(self) -> tuple[str, ...]: ...
117117
@property
118-
def engine(self) -> Literal["openpyxl", "pyxlsb", "odf"]: ...
118+
def engine(self) -> Literal["openpyxl", "odf", "xlsxwriter"]: ...
119119
@property
120120
def sheets(self) -> dict[str, Any]: ...
121121
@property
122-
def book(self) -> Workbook | OpenDocument | pyxlsb.workbook.Workbook: ...
122+
def book(self) -> Workbook | OpenDocument: ...
123123
@property
124124
def date_format(self) -> str: ...
125125
@property

pyproject.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ tables = { version = ">=3.7.0", python = "<3.11" }
5252
lxml = { version = ">=4.7.1,<4.9.0", python = "<3.11" }
5353
pyreadstat = ">=1.2.0"
5454
xlrd = ">=2.0.1"
55-
pyxlsb = ">=1.0.9"
55+
xlsxwriter = ">=3.0.3"
56+
pyxlsb = ">=1.0.10"
5657
odfpy = ">=1.4.1"
5758
xarray = ">=22.6.0"
5859
tabulate = ">=0.8.10"

tests/test_io.py

+37-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
Dict,
1212
Generator,
1313
List,
14+
Literal,
1415
Union,
1516
)
1617

@@ -694,12 +695,46 @@ def test_excel_writer():
694695
check(assert_type(ef.close(), None), type(None))
695696

696697

698+
def test_excel_writer_engine():
699+
with ensure_clean(".xlsx") as path:
700+
with pd.ExcelWriter(path, engine="auto") as ew:
701+
check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter)
702+
DF.to_excel(ew, sheet_name="A")
703+
704+
with ensure_clean(".xlsx") as path:
705+
with pd.ExcelWriter(path, engine="openpyxl") as ew:
706+
check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter)
707+
DF.to_excel(ew, sheet_name="A")
708+
check(
709+
assert_type(ew.engine, Literal["openpyxl", "odf", "xlsxwriter"]),
710+
str,
711+
)
712+
713+
with ensure_clean(".ods") as path:
714+
with pd.ExcelWriter(path, engine="odf") as ew:
715+
check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter)
716+
DF.to_excel(ew, sheet_name="A")
717+
check(
718+
assert_type(ew.engine, Literal["openpyxl", "odf", "xlsxwriter"]),
719+
str,
720+
)
721+
722+
with ensure_clean(".xlsx") as path:
723+
with pd.ExcelWriter(path, engine="xlsxwriter") as ew:
724+
check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter)
725+
DF.to_excel(ew, sheet_name="A")
726+
check(
727+
assert_type(ew.engine, Literal["openpyxl", "odf", "xlsxwriter"]),
728+
str,
729+
)
730+
731+
697732
def test_excel_writer_append_mode():
698733
with ensure_clean(".xlsx") as path:
699734
with pd.ExcelWriter(path, mode="w") as ew:
700735
DF.to_excel(ew, sheet_name="A")
701-
with pd.ExcelWriter(path, mode="a") as ew:
702-
pass
736+
with pd.ExcelWriter(path, mode="a", engine="openpyxl") as ew:
737+
DF.to_excel(ew, sheet_name="B")
703738

704739

705740
def test_to_string():

0 commit comments

Comments
 (0)