theorchard / openpyxl

Other
54 stars 21 forks source link

AttributeError: 'ReadOnlyWorksheet' object has no attribute 'defined_names' #23

Open Joe-Heffer-Shef opened 1 year ago

Joe-Heffer-Shef commented 1 year ago

I have a problem with the error 'ReadOnlyWorksheet' object has no attribute 'defined_names' when loading a workbook.

Stack trace:

File "/opt/my_project/utils/excel.py", line 55, in load_workbook
return openpyxl.load_workbook(
File "/usr/local/lib/python3.10/site-packages/openpyxl/reader/excel.py", line 346, in load_workbook
reader.read()
File "/usr/local/lib/python3.10/site-packages/openpyxl/reader/excel.py", line 303, in read
self.parser.assign_names()
File "/usr/local/lib/python3.10/site-packages/openpyxl/reader/workbook.py", line 109, in assign_names
sheet.defined_names[name] = defn

I am able to fix this by reverting from version 3.1.1 to 3.1.0 of openpyxl.

However, I'm unable to replicate this problem, so I'm guessing there's some obscure difference in my project's code that isn't captured in the simple example below. The code above is from a Linux container for a project I'm working on, while the snippets below are using Python on Windows 10.

Openpyxl version 3.1.0

> python -m venv openpyxl310
> openpyxl310\Scripts\activate.bat
> pip install openpyxl==3.1.0
> python -c "import openpyxl; wb = openpyxl.load_workbook('Book1.xlsx', read_only=True, data_only=True); print(repr(wb))"
<openpyxl.workbook.workbook.Workbook object at 0x000001EE738762B0>

Openpyxl version 3.1.1

> python -m venv openpyxl311
> openpyxl311\Scripts\activate.bat
> pip install openpyxl==3.1.1
> python -c "import openpyxl; wb = openpyxl.load_workbook('Book1.xlsx', read_only=True, data_only=True); print(repr(wb))"
<openpyxl.workbook.workbook.Workbook object at 0x0000025A95F66250>