Skip to content

Commit 7178fbb

Browse files
authored
BUG: Ignore chartsheets (#41698)
1 parent 71dd600 commit 7178fbb

File tree

8 files changed

+47
-4
lines changed

8 files changed

+47
-4
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ MultiIndex
173173

174174
I/O
175175
^^^
176+
- Bug in :func:`read_excel` attempting to read chart sheets from .xlsx files (:issue:`41448`)
176177
-
177178
-
178179

pandas/io/excel/_base.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@
8282
or ``StringIO``.
8383
sheet_name : str, int, list, or None, default 0
8484
Strings are used for sheet names. Integers are used in zero-indexed
85-
sheet positions. Lists of strings/integers are used to request
86-
multiple sheets. Specify None to get all sheets.
85+
sheet positions (chart sheets do not count as a sheet position).
86+
Lists of strings/integers are used to request multiple sheets.
87+
Specify None to get all worksheets.
8788
8889
Available cases:
8990
@@ -92,7 +93,7 @@
9293
* ``"Sheet1"``: Load sheet with name "Sheet1"
9394
* ``[0, 1, "Sheet5"]``: Load first, second and sheet named "Sheet5"
9495
as a dict of `DataFrame`
95-
* None: All sheets.
96+
* None: All worksheets.
9697
9798
header : int, list of int, default 0
9899
Row (0-indexed) to use for the column labels of the parsed

pandas/io/excel/_openpyxl.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ def load_workbook(self, filepath_or_buffer: FilePathOrBuffer):
536536

537537
@property
538538
def sheet_names(self) -> list[str]:
539-
return self.book.sheetnames
539+
return [sheet.title for sheet in self.book.worksheets]
540540

541541
def get_sheet_by_name(self, name: str):
542542
self.raise_if_bad_sheet_by_name(name)
42 KB
Binary file not shown.
18 KB
Binary file not shown.
19.6 KB
Binary file not shown.
19.6 KB
Binary file not shown.

pandas/tests/io/excel/test_readers.py

+41
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,34 @@ def test_trailing_blanks(self, read_ext):
12501250
result = pd.read_excel(file_name)
12511251
assert result.shape == (3, 3)
12521252

1253+
def test_ignore_chartsheets_by_str(self, request, read_ext):
1254+
# GH 41448
1255+
if pd.read_excel.keywords["engine"] == "odf":
1256+
pytest.skip("chartsheets do not exist in the ODF format")
1257+
if pd.read_excel.keywords["engine"] == "pyxlsb":
1258+
request.node.add_marker(
1259+
pytest.mark.xfail(
1260+
reason="pyxlsb can't distinguish chartsheets from worksheets"
1261+
)
1262+
)
1263+
with pytest.raises(ValueError, match="Worksheet named 'Chart1' not found"):
1264+
pd.read_excel("chartsheet" + read_ext, sheet_name="Chart1")
1265+
1266+
def test_ignore_chartsheets_by_int(self, request, read_ext):
1267+
# GH 41448
1268+
if pd.read_excel.keywords["engine"] == "odf":
1269+
pytest.skip("chartsheets do not exist in the ODF format")
1270+
if pd.read_excel.keywords["engine"] == "pyxlsb":
1271+
request.node.add_marker(
1272+
pytest.mark.xfail(
1273+
reason="pyxlsb can't distinguish chartsheets from worksheets"
1274+
)
1275+
)
1276+
with pytest.raises(
1277+
ValueError, match="Worksheet index 1 is invalid, 1 worksheets found"
1278+
):
1279+
pd.read_excel("chartsheet" + read_ext, sheet_name=1)
1280+
12531281

12541282
class TestExcelFileRead:
12551283
@pytest.fixture(autouse=True)
@@ -1501,6 +1529,19 @@ def test_engine_invalid_option(self, read_ext):
15011529
with pd.option_context(f"io.excel{read_ext}.reader", "abc"):
15021530
pass
15031531

1532+
def test_ignore_chartsheets(self, request, engine, read_ext):
1533+
# GH 41448
1534+
if engine == "odf":
1535+
pytest.skip("chartsheets do not exist in the ODF format")
1536+
if engine == "pyxlsb":
1537+
request.node.add_marker(
1538+
pytest.mark.xfail(
1539+
reason="pyxlsb can't distinguish chartsheets from worksheets"
1540+
)
1541+
)
1542+
with pd.ExcelFile("chartsheet" + read_ext) as excel:
1543+
assert excel.sheet_names == ["Sheet1"]
1544+
15041545
def test_corrupt_files_closed(self, request, engine, read_ext):
15051546
# GH41778
15061547
errors = (BadZipFile,)

0 commit comments

Comments
 (0)