Excel Border Color with Xlwings in Python

huangapple go评论72阅读模式
英文:

Excel Border Color with Xlwings in Python

问题

I've been able to add cell borders and set their weight using xlwings in Python. However, every time I try to change the border color, the code gets stuck:

import xlwings as xw
sheet = xw.sheets[0]
cells = sheet.range((2,2),(2,6))
cells.api.Borders(11).Weight = 3
cells.api.Borders(11).Color = '#ffffff' # This line gets the code stuck

有什么可能导致这个问题的想法吗?

英文:

Using xlwings in Python, I've been able to add cell borders and set their weight. But unfortunately every time I try to change the border color, the code gets stuck

import xlwings as xw
sheet = xw.sheets[0]
cells = sheet.range((2,2),(2,6))
cells.api.Borders(11).Weight = 3
cells.api.Borders(11).Color = '#ffffff' # This line gets the code stuck

Any idea what might be causing it?

答案1

得分: 1

你可以使用:

#https://stackoverflow.com/a/21338319/16120011
def rgbToInt(rgb):
    colorInt = rgb[0] + (rgb[1] * 256) + (rgb[2] * 256 * 256)
    return colorInt

wb = xw.Book()

sheet = wb.sheets[0]

cells = sheet.range((2, 2), (2, 6))
cells.api.Borders(11).Weight = 3
cells.api.Borders(11).Color = rgbToInt((0, 0, 0)) #黑色

输出

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/GTQ2v.png
英文:

You can use :

#https://stackoverflow.com/a/21338319/16120011
def rgbToInt(rgb):
    colorInt = rgb[0] + (rgb[1] * 256) + (rgb[2] * 256 * 256)
    return colorInt

wb = xw.Book()

sheet = wb.sheets[0]

cells = sheet.range((2, 2), (2, 6))
cells.api.Borders(11).Weight = 3
cells.api.Borders(11).Color = rgbToInt((0, 0, 0)) #black color

Output :

Excel Border Color with Xlwings in Python

huangapple
  • 本文由 发表于 2023年6月1日 01:02:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375824.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定