使用openpyxl将Excel单元格背景主题的颜色获取为十六进制 [英] Getting Excel cell background themed color as hex with openpyxl
问题描述
我正在使用openpyxl python库读取* xlsx文件,在其他数据中,我想获取有关单元格背景色的信息.
I'm reading *xlsx files with openpyxl python library, and within other data I want to get information on cells background color.
我通常使用的这段代码,而我的工作表仅包含标准颜色,然后它以字符串格式返回了诸如"FFFFC000"之类的东西,供我进一步使用.
This code I was using normally, while my worksheets contained only standard colors, then it returned me something like "FFFFC000" in a string format that I can use further.
color = cell.fill.start_color.index
遇到包含主题颜色单元格的文档后,对于这些单元格,它返回的只是整数,例如'1'或'9',因为我可以猜测它等于菜单中主题色的列:
After I've met documents containing cells of themed colors it returns me just integers like '1' or '9' for such cells, as I can guess it equals to column of theme color from the menu:
有没有办法获取此类主题颜色的十六进制或rgb信息?
Is there any way to get hex or rgb info on such themed colors?
推荐答案
After hours of googling finally found the solution - proposed idea to add converting between theme + tint to RGB in openpyxl. Importing their set of color converting functions, I can reduce my solution to the next steps:
wb = load_workbook(filename, data_only=True)
theme = cell.fill.start_color.theme
tint = cell.fill.start_color.tint
color = theme_and_tint_to_rgb(wb, theme, tint)
这篇关于使用openpyxl将Excel单元格背景主题的颜色获取为十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!