Unable to open file with load_workbook with error IndexError: list index out of range
Reproducing the Problem
Trying to upload file with the following code but get an error:
from openpyxl import load_workbook
wb = load_workbook(f"{'Data.xlsx'}", data_only=True, keep_vba=True)
This file loads correctly in Excel, but not with openpyxl.
Stack Exception:
Traceback (most recent call last):
File "c:\main.py", line 26, in <module>
wb = load_workbook(f"{'Data.xlsx'}", data_only=True, keep_vba=True)
File "C:\Python39\lib\site-packages\openpyxl\reader\excel.py", line 317, in load_workbook
reader.read()
File "C:\Python39\lib\site-packages\openpyxl\reader\excel.py", line 281, in read
apply_stylesheet(self.archive, self.wb)
File "C:\Python39\lib\site-packages\openpyxl\styles\stylesheet.py", line 196, in apply_stylesheet
stylesheet = Stylesheet.from_tree(node)
File "C:Python39\lib\site-packages\openpyxl\styles\stylesheet.py", line 102, in from_tree
return super(Stylesheet, cls).from_tree(node)
File "C:\Python39\lib\site-packages\openpyxl\descriptors\serialisable.py", line 103, in from_tree
return cls(**attrib)
File "C:\Python39\lib\site-packages\openpyxl\styles\stylesheet.py", line 94, in __init__
self.named_styles = self._merge_named_styles()
File "C:\Python39\lib\site-packages\openpyxl\styles\stylesheet.py", line 112, in _merge_named_styles
self._expand_named_style(style)
File "C:\Python39\lib\site-packages\openpyxl\styles\stylesheet.py", line 121, in _expand_named_style
xf = self.cellStyleXfs[named_style.xfId]
File "C:\Python39\lib\site-packages\openpyxl\styles\cell_style.py", line 185, in __getitem__
return self.xf[idx]
IndexError: list index out of range
Proposed Fix:
In Lib\site-packages\openpyxl\styles\stylesheet.py:_expand_named_style set a try except:
try: xf = self.cellStyleXfs[named_style.xfId]
except: return # WORKAROUND for faulty Excel sheets