Skip to content

Commit bb9e2e9

Browse files
committed
ENH: Allow passing read_only, data_only and keep_links arguments to openpyxl using engine_kwargs
Previously it was not possible to override the default values for `openpyxl.reader.excel.load_workbook`'s `read_only`, `data_only` and `keep_links` arguments (see #55027). Now these options can be changed via `engine_kwargs`. Closes #55027
1 parent 0cdb37c commit bb9e2e9

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

doc/source/whatsnew/v2.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Other enhancements
8181
- :func:`tseries.api.guess_datetime_format` is now part of the public API (:issue:`54727`)
8282
- :meth:`ExtensionArray._explode` interface method added to allow extension type implementations of the ``explode`` method (:issue:`54833`)
8383
- :meth:`ExtensionArray.duplicated` added to allow extension type implementations of the ``duplicated`` method (:issue:`55255`)
84+
- Allow passing ``read_only``, ``data_only`` and ``keep_links`` arguments to openpyxl using ``engine_kwargs`` of :func:`read_excel` (:issue:`55027`)
8485
- DataFrame.apply now allows the usage of numba (via ``engine="numba"``) to JIT compile the passed function, allowing for potential speedups (:issue:`54666`)
8586
- Implement masked algorithms for :meth:`Series.value_counts` (:issue:`54984`)
8687

pandas/io/excel/_openpyxl.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -567,11 +567,15 @@ def load_workbook(
567567
) -> Workbook:
568568
from openpyxl import load_workbook
569569

570+
if engine_kwargs is None:
571+
engine_kwargs = {}
572+
573+
engine_kwargs.setdefault("read_only", True)
574+
engine_kwargs.setdefault("data_only", True)
575+
engine_kwargs.setdefault("keep_links", False)
576+
570577
return load_workbook(
571578
filepath_or_buffer,
572-
read_only=True,
573-
data_only=True,
574-
keep_links=False,
575579
**engine_kwargs,
576580
)
577581

pandas/tests/io/excel/test_openpyxl.py

+15
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,21 @@ def test_engine_kwargs_append_data_only(ext, data_only, expected):
130130
DataFrame().to_excel(writer, sheet_name="Sheet2")
131131

132132

133+
@pytest.mark.parametrize("kwarg_name", ["read_only", "data_only"])
134+
@pytest.mark.parametrize("kwarg_value", [True, False])
135+
def test_engine_kwargs_append_reader(datapath, ext, kwarg_name, kwarg_value):
136+
# GH 55027
137+
# test that `read_only` and `data_only` can be passed to
138+
# `openpyxl.reader.excel.load_workbook` via `engine_kwargs`
139+
from pandas.io.excel._openpyxl import OpenpyxlReader
140+
141+
filename = datapath("io", "data", "excel", "test1" + ext)
142+
with contextlib.closing(
143+
OpenpyxlReader(filename, engine_kwargs={kwarg_name: kwarg_value})
144+
) as reader:
145+
assert getattr(reader.book, kwarg_name) == kwarg_value
146+
147+
133148
@pytest.mark.parametrize(
134149
"mode,expected", [("w", ["baz"]), ("a", ["foo", "bar", "baz"])]
135150
)

0 commit comments

Comments
 (0)