Why does pandas read_excel fail on an openpyxl error saying 'ReadOnlyWorksheet' object has no attribute 'defined_names'?

huangapple go评论69阅读模式
英文:

Why does pandas read_excel fail on an openpyxl error saying 'ReadOnlyWorksheet' object has no attribute 'defined_names'?

问题

这个错误突然出现,之前使用read_excel是正常的。无论我使用哪个版本的Python3都会失败 - 包括10和11。

有人知道怎么修复吗?

  File "/Users/aizenman/My Drive/code/daily_new_clients/code/run_daily_housekeeping.py", line 38, in <module>
    main()
  File "/Users/aizenman/My Drive/code/daily_new_clients/code/run_daily_housekeeping.py", line 25, in main
    sb = diana.superbills.load_superbills_births(args.site, ath)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/aizenman/My Drive/code/daily_new_clients/code/diana/superbills.py", line 148, in load_superbills_births
    sb = pd.read_excel(SUPERBILLS_EXCEL, sheet_name="Births", parse_dates=["DOS", "DOB"])
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/util/_decorators.py", line 211, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/util/_decorators.py", line 331, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/io/excel/_base.py", line 482, in read_excel
    io = ExcelFile(io, storage_options=storage_options, engine=engine)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/io/excel/_base.py", line 1695, in __init__
    self._reader = self._engines[engine](self._io, storage_options=storage_options)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/io/excel/_openpyxl.py", line 557, in __init__
    super().__init__(filepath_or_buffer, storage_options=storage_options)
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/io/excel/_base.py", line 545, in __init__
    self.book = self.load_workbook(self.handles.handle)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/io/excel/_openpyxl.py", line 568, in load_workbook
    return load_workbook(
           ^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/openpyxl/reader/excel.py", line 346, in load_workbook
    reader.read()
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/openpyxl/reader/excel.py", line 303, in read
    self.parser.assign_names()
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/openpyxl/reader/workbook.py", line 109, in assign_names
    sheet.defined_names[name] = defn
    ^^^^^^^^^^^^^^^^^^^
AttributeError: 'ReadOnlyWorksheet' object has no attribute 'defined_names'
英文:

This bug suddenly came up literally today after read_excel previously was working fine. Fails no matter which version of python3 I use - either 10 or 11.

Do folks know the fix?

  File &quot;/Users/aizenman/My Drive/code/daily_new_clients/code/run_daily_housekeeping.py&quot;, line 38, in &lt;module&gt;
    main()
  File &quot;/Users/aizenman/My Drive/code/daily_new_clients/code/run_daily_housekeeping.py&quot;, line 25, in main
    sb = diana.superbills.load_superbills_births(args.site, ath)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;/Users/aizenman/My Drive/code/daily_new_clients/code/diana/superbills.py&quot;, line 148, in load_superbills_births
    sb = pd.read_excel(SUPERBILLS_EXCEL, sheet_name=&quot;Births&quot;, parse_dates=[&quot;DOS&quot;, &quot;DOB&quot;])
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/util/_decorators.py&quot;, line 211, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File &quot;/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/util/_decorators.py&quot;, line 331, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File &quot;/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/io/excel/_base.py&quot;, line 482, in read_excel
    io = ExcelFile(io, storage_options=storage_options, engine=engine)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/io/excel/_base.py&quot;, line 1695, in __init__
    self._reader = self._engines[engine](self._io, storage_options=storage_options)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/io/excel/_openpyxl.py&quot;, line 557, in __init__
    super().__init__(filepath_or_buffer, storage_options=storage_options)
  File &quot;/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/io/excel/_base.py&quot;, line 545, in __init__
    self.book = self.load_workbook(self.handles.handle)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/io/excel/_openpyxl.py&quot;, line 568, in load_workbook
    return load_workbook(
           ^^^^^^^^^^^^^^
  File &quot;/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/openpyxl/reader/excel.py&quot;, line 346, in load_workbook
    reader.read()
  File &quot;/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/openpyxl/reader/excel.py&quot;, line 303, in read
    self.parser.assign_names()
  File &quot;/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/openpyxl/reader/workbook.py&quot;, line 109, in assign_names
    sheet.defined_names[name] = defn
    ^^^^^^^^^^^^^^^^^^^
AttributeError: &#39;ReadOnlyWorksheet&#39; object has no attribute &#39;defined_names&#39;

答案1

得分: 21

我正在使用Azure Databricks,今天遇到了同样的问题。我检查了一下,openpyxl的版本是3.1.1。

在我的笔记本开头,我添加了一行代码来将它固定到之前的版本:

%pip install --force-reinstall -v "openpyxl==3.1.0"

之后,我的任务又开始正常工作了。看起来是最近更新中的某些改变导致了这个问题。

英文:

I am using Azure Databricks and I ran into this same issue today. I checked and openpyxl was on version 3.1.1.

At the start of my notebook I added a line to pin to the previous version:

%pip install --force-reinstall -v &quot;openpyxl==3.1.0&quot;

After that my job was working again. It seems like some change in the most recent update is causing this to break.

答案2

得分: 1

你可以首先尝试卸载 openpyxl:

pip uninstall openpyxl -y

然后使用:

pip install openpyxl==3.1.0 -y

注意:如果在笔记本中使用,请在代码前加上!:

!pip uninstall openpyxl -y
!pip install openpyxl==3.1.0 -y

如果上述代码不起作用,你可以尝试升级 pandas,即:

!pip uninstall pandas -y && !pip install pandas

英文:

You can first try to uninstall the openpyxl
pip uninstall openpyxl -y
and then use
pip install openpyxl==3.1.0 -y

Note: Use ! infront of code if case of using notebooks.
!pip uninstall openpyxl -y
!pip install openpyxl==3.1.0 -y

If the above code does not work. You can try to upgrade the pandas. i.e
!pip uninstall pandas -y && !pip install pandas

答案3

得分: 0

通过安装'xlxswriter',问题得以解决。感谢上述解决方案,但在我的情况下并不起作用。因此,这可能是另一个你需要考虑的问题。

英文:

By installing the 'xlxswriter', the trouble was solved. Thanks to the above solutions, but they do not work in my case. So, this maybe another issuse you may consider.

答案4

得分: 0

目前,我们在python3.8环境(Rocky 8.8操作系统)遇到了问题。我们将openpyxl模块降级至3.0.9版本以解决此问题。

> sudo pip3 install openpyxl==3.0.9

祝使用愉快!

英文:

Currently, we face the issue on python3.8 environment (Rocky 8.8 OS). We downgrade the openpyxl module to 3.0.9 to fix this issue.

> sudo pip3 install openpyxl==3.0.9

Enjoy it!

huangapple
  • 本文由 发表于 2023年2月14日 03:20:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440354.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定