Openpyxl load_workbook 仅在我手动打开文件并在使用前保存时有效
Openpyxl load_workbook only works when I open the file manually and saves it before use
我正在尝试使用 openpyxl 读取 xlsx 文件:
workbook_skema_72 = openpyxl.load_workbook(skema_72_path)
但是当我尝试加载工作簿时出现以下错误:
Traceback (most recent call last):
File "d:\Python\LCR_skema_opdater2203-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
但是当我手动打开工作簿并保存时,问题就解决了。
我有很多文件,所以这对所有文件来说都很困难。有某种解决方案或解决方法吗?
我希望你能给我指明正确的方向。
我终于找到了一个解决方法,也许它会在将来帮助其他人:
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)
我正在尝试使用 openpyxl 读取 xlsx 文件:
workbook_skema_72 = openpyxl.load_workbook(skema_72_path)
但是当我尝试加载工作簿时出现以下错误:
Traceback (most recent call last):
File "d:\Python\LCR_skema_opdater2203-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
但是当我手动打开工作簿并保存时,问题就解决了。 我有很多文件,所以这对所有文件来说都很困难。有某种解决方案或解决方法吗? 我希望你能给我指明正确的方向。
我终于找到了一个解决方法,也许它会在将来帮助其他人:
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)