简体   繁体   English

如何使用openpyxl读取xlsx颜色信息

[英]how to read the xlsx color infomation by using openpyxl

I am using openpyxl to read excel file. 我正在使用openpyxl来读取excel文件。

For my application, i need to read the background color of the cell in the xlsx file But i cant find how to load those color info. 对于我的应用程序,我需要读取xlsx文件中单元格的背景颜色但我无法找到如何加载这些颜色信息。

I tried to use cell.style.fill.color.index, but it only return FFFFFFFF as background which is not correct to the file i read. 我尝试使用cell.style.fill.color.index,但它只返回FFFFFFFF作为背景,这对我读取的文件不正确。

Does openpyxl support reading color format? openpyxl支持读取颜色格式吗?

UPDATE (2014): 更新(2014年):

I updated to v. 2.2. 我将更新为 They seem to have resolved the problem that I documented in my original answer (see below). 他们似乎已经解决了我在原始答案中记录的问题(见下文)。 I am now able to successfully retrieve the background color after I've set it manually through Excel. 我现在能够通过Excel手动设置后成功检索背景颜色。 However, the syntax has changed slightly: 但是,语法略有改变:

somecell.fill.start_color.index

ORIGINAL RESPONSE (2012): 原始回应(2012年):

I experimented with this and noticed that if I set the background color via openpyxl like this: 我试验过这个并注意到如果我通过openpyxl设置背景颜色,如下所示:

_cell.style.fill.fill_type = Fill.FILL_SOLID
_cell.style.fill.start_color.index = Color.DARKGREEN

then retrieve the value like this: 然后像这样检索值:

_style.fill.start_color.index

then I get the correct response: 然后我得到正确的答复:

'FF008000'

However, if I set the background color in Excel and save the file, then access it via openpyxl, then I get the same thing you did: 但是,如果我在Excel中设置背景颜色并保存文件,然后通过openpyxl访问它,那么我得到你做的同样的事情:

'FFFFFFFF'

So, the upshot seems to be that opepyxl styles are still unreliable. 因此,结果似乎是opepyxl样式仍然不可靠。 See my comment, under the question, with a link to the comment from the author of openpyxl. 在问题的帮助下查看我的评论,其中包含openpyxl作者评论的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM