Copying from a range of cells with openpyxl, Error: Colors must be aRGB hex values

Copying from a range of cells with openpyxl, Error: Colors must be aRGB hex values

我正在使用以下代码将带有 openpyxl 的一系列单元格从一个工作簿复制到另一个工作簿:

import openpyxl
import os

#Current path
path = os.path.dirname(os.path.abspath(__file__))

#Beregningsmodul navn
Beregningsmodul_moder_navn = "Beregning COREP LCR - MODER - 202202.xlsx"

#workbook_beregn path
beregn_path = path + "\" + Beregningsmodul_moder_navn
workbook_beregn = openpyxl.load_workbook(beregn_path)

#Skema 72 navn
skema_72_navn ="C_72_00_a.xlsx"
#skema path
skema_72_path = path + "\" + skema_72_navn
workbook_skema_72 = openpyxl.load_workbook(skema_72_path)

#Kopier til
wb_72C = workbook_beregn["72C"]['E8':'G54']

#kopier fra
C_72_00_a = workbook_skema_72["C_72_00_a"]['D9':'F55']

#Pair the rows
for row1,row2 in zip(C_72_00_a, workbook_beregn):
    #within the row pair, pair the cells
    for cell1, cell2 in zip(row1,row2):
        #assign the value of cell 1 to the destination cell 2 for each row
        cell2.value = cell1.value
#save document
workbook_beregn.save('destination.xlsx')

但是 运行 时出现以下错误:

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
PS C:\Users\e694896> 

我做错了什么?工作簿中有一些颜色格式,但我不太关心颜色数字是重要的部分。

希望你能给我指明正确的方向。

#更新: 当我打开并保存文件时,问题已解决,有什么方法可以避免手动执行此操作?

我找到了解决方法。如果我打开文件并保存,问题就解决了。

我发现的最佳方法是使用以下代码修复文件:

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"]#list of filenames
    path = "D:\Python\Intradag_opdater\04\2022-04-04\LCR skema"
    
    def clean2(file_in):
        open = os.path.join(path, file_in)
        os.startfile(open,'edit')#open file
        time.sleep(5)#timer to wait for excel to open
        pyautogui.hotkey('ctrl', 's')#Save file
        pyautogui.hotkey('alt', 'f4')#close excel
    
    
    for x in filename:#loop through all files
        clean2(x)
        print(x)

它有效,但我对我找到的解决方案并不感到自豪。