Skip to content

Commit 029b721

Browse files
BUG: fix bug with openpyxl on read_only mode
Co-Authored-By: Dacops <[email protected]>
1 parent 9066a92 commit 029b721

File tree

4 files changed

+12
-24
lines changed

4 files changed

+12
-24
lines changed

pandas/io/excel/_base.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -514,17 +514,14 @@ def read_excel(
514514
if engine_kwargs is None:
515515
engine_kwargs = {}
516516

517-
# set to false so cells have a comment attribute
518-
if notes is not None and engine == "openpyxl":
519-
engine_kwargs = {"read_only": False}
520-
521517
if not isinstance(io, ExcelFile):
522518
should_close = True
523519
io = ExcelFile(
524520
io,
525521
storage_options=storage_options,
526522
engine=engine,
527523
engine_kwargs=engine_kwargs,
524+
notes=notes,
528525
)
529526
elif engine and engine != io.engine:
530527
raise ValueError(
@@ -1574,6 +1571,8 @@ class ExcelFile:
15741571
{storage_options}
15751572
engine_kwargs : dict, optional
15761573
Arbitrary keyword arguments passed to excel engine.
1574+
notes : DataFrame, default None
1575+
DataFrame that holds notes of the Excel file.
15771576
15781577
See Also
15791578
--------
@@ -1609,6 +1608,7 @@ def __init__(
16091608
engine: str | None = None,
16101609
storage_options: StorageOptions | None = None,
16111610
engine_kwargs: dict | None = None,
1611+
notes: DataFrame | None = None,
16121612
) -> None:
16131613
if engine_kwargs is None:
16141614
engine_kwargs = {}
@@ -1655,6 +1655,10 @@ def __init__(
16551655
self.engine = engine
16561656
self.storage_options = storage_options
16571657

1658+
# set so the engine can access the notes from a cell
1659+
if notes is not None and engine == "openpyxl":
1660+
engine_kwargs.update({"read_only": False})
1661+
16581662
self._reader = self._engines[engine](
16591663
self._io,
16601664
storage_options=storage_options,

pandas/io/excel/_openpyxl.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,8 @@ def _write_cells(
542542
if notes is None or notes_col is None:
543543
return
544544

545-
for row_idx, (_, row) in enumerate(notes.iterrows()):
546-
for col_idx, note in enumerate(row):
545+
for row_idx, (_, val) in enumerate(notes.iterrows()):
546+
for col_idx, note in enumerate(val):
547547
xcell = wks.cell(
548548
row=row_idx + 2, # openpyxl starts counting at 1, not 0
549549
column=col_idx + notes_col,

pandas/io/formats/excel.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,8 @@ def get_formatted_cells(self) -> Iterable[ExcelCell]:
884884
yield cell
885885

886886
def get_notes(self) -> Iterable[str]:
887-
yield from self.notes.iterrows()
887+
if self.notes:
888+
yield from self.notes.iterrows()
888889

889890
@doc(storage_options=_shared_docs["storage_options"])
890891
def write(

pandas/io/formats/style.py

-17
Original file line numberDiff line numberDiff line change
@@ -82,25 +82,8 @@
8282

8383
from pandas import ExcelWriter
8484

85-
try:
86-
import matplotlib as mpl
87-
import matplotlib.pyplot as plt
88-
89-
has_mpl = True
90-
except ImportError:
91-
has_mpl = False
92-
9385
import textwrap
9486

95-
96-
@contextmanager
97-
def _mpl(func: Callable) -> Generator[tuple[Any, Any], None, None]:
98-
if has_mpl:
99-
yield plt, mpl
100-
else:
101-
raise ImportError(f"{func.__name__} requires matplotlib.")
102-
103-
10487
####
10588
# Shared Doc Strings
10689

0 commit comments

Comments
 (0)