Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to read the xlsx color infomation by using openpyxl

I am using openpyxl to read excel file.

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.

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.

Does openpyxl support reading color format?

like image 867
enzhou.liu Avatar asked Aug 20 '12 19:08

enzhou.liu


1 Answers

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

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

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:

'FFFFFFFF'

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.

like image 62
David Avatar answered Oct 02 '22 16:10

David