Search code examples
pythonexcelpandasopenxmlxlm

Can't read XLSM file with pandas because of negative relativeIndents in styles.xml


When reading an XLSM file with pandas I'm getting the following error:

  File "C:\Users\user\company\rollout\\Skripte\\\Puma.py", line 42, in run
    report_filter = pd.read_excel(bodopath + "Master ROO.xlsm", sheet_name='PUMA Auswertung', usecols="A:D")

  File "C:\Users\user\AppData\Local\WPy64-3940\python-3.9.4.amd64\lib\site-packages\pandas\util\_decorators.py", line 299, in wrapper
    return func(*args, **kwargs)

  File "C:\Users\user\AppData\Local\WPy64-3940\python-3.9.4.amd64\lib\site-packages\pandas\io\excel\_base.py", line 336, in read_excel
    io = ExcelFile(io, storage_options=storage_options, engine=engine)

  File "C:\Users\user\AppData\Local\WPy64-3940\python-3.9.4.amd64\lib\site-packages\pandas\io\excel\_base.py", line 1131, in __init__
    self._reader = self._engines[engine](self._io, storage_options=storage_options)

  File "C:\Users\user\AppData\Local\WPy64-3940\python-3.9.4.amd64\lib\site-packages\pandas\io\excel\_openpyxl.py", line 475, in __init__
    super().__init__(filepath_or_buffer, storage_options=storage_options)

  File "C:\Users\user\AppData\Local\WPy64-3940\python-3.9.4.amd64\lib\site-packages\pandas\io\excel\_base.py", line 391, in __init__
    self.book = self.load_workbook(self.handles.handle)

  File "C:\Users\user\AppData\Local\WPy64-3940\python-3.9.4.amd64\lib\site-packages\pandas\io\excel\_openpyxl.py", line 486, in load_workbook
    return load_workbook(

  File "C:\Users\user\AppData\Local\WPy64-3940\python-3.9.4.amd64\lib\site-packages\openpyxl\reader\excel.py", line 317, in load_workbook
    reader.read()

  File "C:\Users\user\AppData\Local\WPy64-3940\python-3.9.4.amd64\lib\site-packages\openpyxl\reader\excel.py", line 281, in read
    apply_stylesheet(self.archive, self.wb)

  File "C:\Users\user\AppData\Local\WPy64-3940\python-3.9.4.amd64\lib\site-packages\openpyxl\styles\stylesheet.py", line 198, in apply_stylesheet
    stylesheet = Stylesheet.from_tree(node)

  File "C:\Users\user\AppData\Local\WPy64-3940\python-3.9.4.amd64\lib\site-packages\openpyxl\styles\stylesheet.py", line 103, in from_tree
    return super(Stylesheet, cls).from_tree(node)

  File "C:\Users\user\AppData\Local\WPy64-3940\python-3.9.4.amd64\lib\site-packages\openpyxl\descriptors\serialisable.py", line 83, in from_tree
    obj = desc.from_tree(el)

  File "C:\Users\user\AppData\Local\WPy64-3940\python-3.9.4.amd64\lib\site-packages\openpyxl\descriptors\sequence.py", line 85, in from_tree
    return [self.expected_type.from_tree(el) for el in node]

  File "C:\Users\user\AppData\Local\WPy64-3940\python-3.9.4.amd64\lib\site-packages\openpyxl\descriptors\sequence.py", line 85, in <listcomp>
    return [self.expected_type.from_tree(el) for el in node]

  File "C:\Users\user\AppData\Local\WPy64-3940\python-3.9.4.amd64\lib\site-packages\openpyxl\descriptors\serialisable.py", line 87, in from_tree
    obj = desc.expected_type.from_tree(el)

  File "C:\Users\user\AppData\Local\WPy64-3940\python-3.9.4.amd64\lib\site-packages\openpyxl\descriptors\serialisable.py", line 103, in from_tree
    return cls(**attrib)

  File "C:\Users\user\AppData\Local\WPy64-3940\python-3.9.4.amd64\lib\site-packages\openpyxl\styles\alignment.py", line 52, in __init__
    self.relativeIndent = relativeIndent

  File "C:\Users\user\AppData\Local\WPy64-3940\python-3.9.4.amd64\lib\site-packages\openpyxl\descriptors\base.py", line 107, in __set__
    raise ValueError('Min value is {0}'.format(self.min))

ValueError: Min value is 0

After googling a bit the problem seems to be a negative indent in the styles.xlm:

    <dxf>
        <alignment relativeIndent="-1"/>
    </dxf>

I would like to know what causes this or if I can fix this before reading the excel_file. Like, how would a -1 relativeIndent look in the Workmap?

Also, yes I can fix this and reupload the styles.xlm but somehow those negative indents come again, sometimes even more than one.

Alright I found the solution. For anyone who has the same problem: Upgrade openpyxl!


Solution

  • Alright I found the solution. For anyone who has the same problem: Upgrade openpyxl!