使用openpyxl中的styles修改一个单元格的背景颜色,第一次运行程序可以修改背景颜色成功,但是再次运行这个python程序的时候,总是提示load_workbook失败,提示信息如下:
Traceback (most recent call last):
File "e:\pythoncode\switch.py", line 218, in <module>
wbbb = load_workbook(file_path)
File "d:\Python27\lib\site-packages\openpyxl-2.5.0a2-py2.7.egg\openpyxl\reader\excel.py", line 222, in load_workbook
apply_stylesheet(archive, wb) # bind styles to workbook
File "d:\Python27\lib\site-packages\openpyxl-2.5.0a2-py2.7.egg\openpyxl\styles\stylesheet.py", line 182, in apply_stylesheet
stylesheet = Stylesheet.from_tree(node)
File "d:\Python27\lib\site-packages\openpyxl-2.5.0a2-py2.7.egg\openpyxl\styles\stylesheet.py", line 99, in from_tree
return super(Stylesheet, cls).from_tree(node)
File "d:\Python27\lib\site-packages\openpyxl-2.5.0a2-py2.7.egg\openpyxl\descriptors\serialisable.py", line 79, in from_tree
obj = desc.from_tree(el)
File "d:\Python27\lib\site-packages\openpyxl-2.5.0a2-py2.7.egg\openpyxl\descriptors\sequence.py", line 86, in from_tree
return [self.expected_type.from_tree(el) for el in node]
File "d:\Python27\lib\site-packages\openpyxl-2.5.0a2-py2.7.egg\openpyxl\styles\fills.py", line 65, in from_tree
return super(Fill, cls).from_tree(child)
File "d:\Python27\lib\site-packages\openpyxl-2.5.0a2-py2.7.egg\openpyxl\descriptors\serialisable.py", line 99, in from_tree
return cls(**attrib)
TypeError: object() takes no parameters
看样子应该是load_workbook的时候加载表格的样式失败了,真心不知道咋解决这个问题。请各位大侠指教!
if __name__ == '__main__': wb = load_workbook(file_path) wsKaoqin = wb.get_sheet_by_name(KQetName) wsKaoqin.cell(row = 3, column = 1).fill = styles.fills.GradientFill(stop=['FFCC00', 'FFCC00']) wb .save(file_path)