SoFunction
Updated on 2025-03-02

Python openpyxl read cell font color process analysis

question

I'm trying to print some_cell. and get various results.

For some people I get what I want (like "FF000000"), but for others it gives me Value must be type 'basetring'. I'm assuming the latter is because I don't actually define the font color for these cells.

I'm using openpyxl 2.2.2

Solution

I think this is a bug in openpyxl, I think you shouldhereReport.

Debug the following code (of coursetrepan3k):

from openpyxl import Workbook
wb = Workbook()
ws = 
c = ws['A4'] # cell gets created here
print(ws['A4'].)

I see:

Color(rgb=Value must be type 'str', indexed=Value must be type 'int', auto=Value must be type 'bool', theme=1, tint=0.0, type='theme')

This comes from _repr_() of class Typed() in the fileopenpyxl/descriptors/. This message is given when the value is not initialized. Please note that "indexed" and "auto" are not set either.

But these should be set when executing access code.

Note: nuances in the message: 'str' instead of 'basestring' may be attributed to me using Python 3 or unlikely to use openpyxl 2.2.3

If I should add some other extra code in my example, then at least/en/latest/This should be shown.

See alsoOpenpyxl cell style is not reported correctlyOne of the developers seems to say the same thing in so many words.

edit:

Some other things may be worth paying attention to. First, you can set a value and then read it, for example you can do this:

 = "FF000000"

Second, if you test in a boolean, it will look like the value that has been set. That's

if : print("yes")

Will print "Yes".

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.