Setting a cell's fill RGB color with pywin32 in excel?

让人想犯罪 __ 提交于 2019-11-29 08:21:20
Vinayak Kolagi

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 can use an integer calculated by the formula Red + (Green * 256) + (Blue * 256 * 256)

def rgbToInt(rgb):
    colorInt = rgb[0] + (rgb[1] * 256) + (rgb[2] * 256 * 256)
    return colorInt

ws.Cells(row,col).Font.Color = rgbToInt((255,255,128))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!