在excel中使用pywin32设置单元格的填充RGB颜色? [英] Setting a cell's fill RGB color with pywin32 in excel?
问题描述
我避免使用其他任何模块.
I'm avoiding using any other set of modules.
我想做的是使用pywin32库在Excel中设置单元格的颜色.到目前为止,我已经找到了如何获得单元格颜色的方法:
What I am trying to do is set a cell's color in Excel, using the pywin32 libary. So far I've found how to get the cells color:
print xl.ActiveSheet.Cells(row, column).interior.color
,您可以简单地通过以类似的方式分配它来设置它:
and you can set it simply by assigning it in a similar fashion:
xl.ActiveSheet.Cells(row, column).interior.color = 0 #black
我的问题是如何在RGB中设置单元格的颜色?
我需要一个名为OLE的ColorTranslator,但是我不知道如何访问system.drawing
,因为它似乎是.NET
. http://msdn.microsoft.com/en-us /library/system.drawing.colortranslator.toole.aspx
I need something called a ColorTranslator to OLE , but I don't know how to access system.drawing
since it seems like it's a .NET
thing. http://msdn.microsoft.com/en-us/library/system.drawing.colortranslator.toole.aspx
推荐答案
interior.color期望BGR中为十六进制值. 如果要以RGB形式指定,则可以使用以下代码.
interior.color expects a hex value in BGR. If you want to specify in RGB form below code can be used.
def rgb_to_hex(rgb):
'''
ws.Cells(1, i).Interior.color uses bgr in hex
'''
bgr = (rgb[2], rgb[1], rgb[0])
strValue = '%02x%02x%02x' % bgr
# print(strValue)
iValue = int(strValue, 16)
return iValue
ws.Cells(1, 1).Interior.color = rgb_to_hex((218, 36, 238))
这篇关于在excel中使用pywin32设置单元格的填充RGB颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!