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

前端 未结 2 1747
猫巷女王i
猫巷女王i 2020-12-18 10:32

I\'m avoiding using any other set of modules.

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 cel

相关标签:
2条回答
  • 2020-12-18 10:51

    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))
    
    0 讨论(0)
  • 2020-12-18 10:59

    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))
    
    0 讨论(0)
提交回复
热议问题