首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在excel中用pywin32设置单元格的填充RGB颜色?

在excel中用pywin32设置单元格的填充RGB颜色?
EN

Stack Overflow用户
提问于 2012-07-12 10:20:38
回答 2查看 9.5K关注 0票数 4

我正在避免使用任何其他的模块集。

我要做的是使用pywin32库在Excel中设置单元格的颜色。到目前为止,我已经找到了获取单元格颜色的方法:

print xl.ActiveSheet.Cells(row, column).interior.color

您可以通过以类似的方式分配它来简单地设置它:

xl.ActiveSheet.Cells(row, column).interior.color = 0 #black

我的问题是如何在中设置单元格的颜色?

我需要一个叫做ColorTranslator to OLE的东西,但是我不知道如何访问system.drawing,因为它似乎是一个.NET的东西。http://msdn.microsoft.com/en-us/library/system.drawing.colortranslator.toole.aspx

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-12 12:47:47

interior.color需要BGR的十六进制值。如果你想在下面的RGB格式中指定,可以使用代码。

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))
票数 13
EN

Stack Overflow用户

发布于 2014-01-25 01:04:59

Excel可以使用由公式红色+(绿色* 256) +(蓝色* 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))
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11444207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档