'Openpyxl load_workbook only works when I open the file manually and saves it before use

I am trying to read xlsx files using openpyxl:

workbook_skema_72 = openpyxl.load_workbook(skema_72_path)

But I get the following error when I try to load the workbook:

Traceback (most recent call last):
  File "d:\Python\LCR_skema_opdater\202203-test\Skema\Moder\LCR_opdater_skema.py", line 18, in <module>
    workbook_skema_72 = openpyxl.load_workbook(skema_72_path)
  File "C:\Users\e694896\Anaconda3\lib\site-packages\openpyxl\reader\excel.py", line 317, in load_workbook
    reader.read()
  File "C:\Users\e694896\Anaconda3\lib\site-packages\openpyxl\reader\excel.py", line 281, in read
    apply_stylesheet(self.archive, self.wb)
  File "C:\Users\e694896\Anaconda3\lib\site-packages\openpyxl\styles\stylesheet.py", line 198, in apply_stylesheet    
    stylesheet = Stylesheet.from_tree(node)
  File "C:\Users\e694896\Anaconda3\lib\site-packages\openpyxl\styles\stylesheet.py", line 103, in from_tree
    return super(Stylesheet, cls).from_tree(node)
  File "C:\Users\e694896\Anaconda3\lib\site-packages\openpyxl\descriptors\serialisable.py", line 83, in from_tree     
    obj = desc.from_tree(el)
  File "C:\Users\e694896\Anaconda3\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\e694896\Anaconda3\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\e694896\Anaconda3\lib\site-packages\openpyxl\styles\fonts.py", line 109, in from_tree
    return super(Font, cls).from_tree(node)
  File "C:\Users\e694896\Anaconda3\lib\site-packages\openpyxl\descriptors\serialisable.py", line 87, in from_tree     
    obj = desc.expected_type.from_tree(el)
  File "C:\Users\e694896\Anaconda3\lib\site-packages\openpyxl\descriptors\serialisable.py", line 103, in from_tree    
    return cls(**attrib)
  File "C:\Users\e694896\Anaconda3\lib\site-packages\openpyxl\styles\colors.py", line 93, in __init__
    self.rgb = rgb
  File "C:\Users\e694896\Anaconda3\lib\site-packages\openpyxl\styles\colors.py", line 61, in __set__
    raise ValueError("Colors must be aRGB hex values")
ValueError: Colors must be aRGB hex values

But the issue is solved when I open the workbook manually and save it. I have a lot of files so this would be difficult with all the files. Is there some sort of solution or workaround? I hope you can point me in the right direction.



Solution 1:[1]

I finally found a workaround maybe it will help somebody else in the future:

import os
import pyautogui
import time
    filename = ["C_72_00_a.xlsx", "C_73_00_a.xlsx", "C_74_00_a.xlsx", "C_76_00_a.xlsx"]
    path = "D:\\Python\\Intradag_opdater\\04\\2022-04-04\\LCR skema"
    
    def clean2(file_in):
        #solution 2
        open = os.path.join(path, file_in)
        os.startfile(open,'edit')
        #tilføj time sleep start og luk excel
        time.sleep(5)
        pyautogui.hotkey('ctrl', 's')
        pyautogui.hotkey('alt', 'f4')
    
    
    for x in filename:
        clean2(x)
        print(x)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Christoffer