我需要使用xlwings在没有循环的一步中为不同的单元格设置不同的颜色。
我已经尝试过了:
import xlwings as xw
wb = xw.Book(filename)
ws = wb.sheets.active
ws.range("A1:B2").color = [[(255, 0, 0), (0, 255, 0)], [(0, 0, 255), (255, 0, 0)]]
结果:
File "C:/Users/tepli/PycharmProjects/little_progs/excel_game/tst2.py", line 5, in <module>
ws.range("A1:B2").color = [[(255, 0, 0), (0, 255, 0)], [(0, 0, 255), (255, 0, 0)]]
File "C:\Users\tepli\AppData\Local\Programs\Python\Python35\lib\site-packages\xlwings\main.py", line 1456, in color
self.impl.color = color_or_rgb
File "C:\Users\tepli\AppData\Local\Programs\Python\Python35\lib\site-packages\xlwings\_xlwindows.py", line 893, in color
self.xl.Interior.Color = rgb_to_int(color_or_rgb)
File "C:\Users\tepli\AppData\Local\Programs\Python\Python35\lib\site-packages\xlwings\utils.py", line 34, in rgb_to_int
return rgb[0] + (rgb[1] * 256) + (rgb[2] * 256 * 256)
IndexError: list index out of range
我已经尝试过:
r = xw.utils.rgb_to_int((255, 0, 0))
g = xw.utils.rgb_to_int((0, 255, 0))
b = xw.utils.rgb_to_int((0, 0, 255))
ws.range("A1:B2").color = [[r, g], [b, r]]
相同的结果。 有没有办法使用xlwings指定不同的颜色?