Tan*_*ash 3 python excel pywin32
我正在避免使用任何其他模块.
我想要做的是使用pywin32 libary在Excel中设置单元格的颜色.到目前为止,我已经找到了如何让细胞变色:
print xl.ActiveSheet.Cells(row, column).interior.color
Run Code Online (Sandbox Code Playgroud)
你可以通过以类似的方式分配它来设置它:
xl.ActiveSheet.Cells(row, column).interior.color = 0 #black
Run Code Online (Sandbox Code Playgroud)
我的问题是如何设置RGB的单元格颜色?
我需要一个叫做ColorTranslator到OLE的东西,但我不知道如何访问,system.drawing因为它似乎是一.NET件事.http://msdn.microsoft.com/en-us/library/system.drawing.colortranslator.toole.aspx
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))
Run Code Online (Sandbox Code Playgroud)
小智 7
Excel可以使用由公式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))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6880 次 |
| 最近记录: |