如何使用openpyxl读取xlsx颜色信息 [英] how to read the xlsx color infomation by using openpyxl

查看:262
本文介绍了如何使用openpyxl读取xlsx颜色信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用openpyxl读取Excel文件.

I am using openpyxl to read excel file.

对于我的应用程序,我需要读取xlsx文件中单元格的背景色 但是我找不到如何加载这些颜色信息.

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.

我尝试使用cell.style.fill.color.index,但是它只返回FFFFFFFF作为背景,这与我读取的文件不正确.

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.

openpyxl是否支持读取颜色格式?

Does openpyxl support reading color format?

推荐答案

UPDATE(2014):

我将更新为2.2版.他们似乎已经解决了我在原始答案中记录的问题(请参见下文).通过Excel手动设置背景颜色后,现在可以成功检索背景颜色.但是,语法略有变化:

UPDATE (2014):

I updated openpyxl 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. However, the syntax has changed slightly:

somecell.fill.start_color.index

原始答复(2012年):

我对此进行了实验,发现如果我通过openpyxl设置背景颜色,如下所示:

ORIGINAL RESPONSE (2012):

I experimented with this and noticed that if I set the background color via openpyxl like this:

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

然后像这样检索值:

_style.fill.start_color.index

然后我得到正确的答复:

then I get the correct response:

'FF008000'

但是,如果我在Excel中设置背景色并保存文件,然后通过openpyxl访问它,那么我会得到与您所做的相同的事情:

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:

'FFFFFFFF'

因此,结果似乎是opepyxl样式仍然不可靠.看到我在问题下方的评论,并带有指向openpyxl作者的评论的链接.

So, the upshot seems to be that opepyxl styles are still unreliable. See my comment, under the question, with a link to the comment from the author of openpyxl.

这篇关于如何使用openpyxl读取xlsx颜色信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆