Excel Border Color with Xlwings in Python

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

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:

  1. import xlwings as xw
  2. sheet = xw.sheets[0]
  3. cells = sheet.range((2,2),(2,6))
  4. cells.api.Borders(11).Weight = 3
  5. 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

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

Any idea what might be causing it?

答案1

得分: 1

你可以使用:

  1. #https://stackoverflow.com/a/21338319/16120011
  2. def rgbToInt(rgb):
  3. colorInt = rgb[0] + (rgb[1] * 256) + (rgb[2] * 256 * 256)
  4. return colorInt
  5. wb = xw.Book()
  6. sheet = wb.sheets[0]
  7. cells = sheet.range((2, 2), (2, 6))
  8. cells.api.Borders(11).Weight = 3
  9. cells.api.Borders(11).Color = rgbToInt((0, 0, 0)) #黑色
  10. 输出
  11. [![enter image description here][1]][1]
  12. [1]: https://i.stack.imgur.com/GTQ2v.png
英文:

You can use :

  1. #https://stackoverflow.com/a/21338319/16120011
  2. def rgbToInt(rgb):
  3. colorInt = rgb[0] + (rgb[1] * 256) + (rgb[2] * 256 * 256)
  4. return colorInt
  5. wb = xw.Book()
  6. sheet = wb.sheets[0]
  7. cells = sheet.range((2, 2), (2, 6))
  8. cells.api.Borders(11).Weight = 3
  9. 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:

确定