Cell colors using OpenPyxl 2.02

I just upgraded from openpyxl 1.6.2 to 2.02 and asked a question about setting cell colors.

The Styles function must handle all necessary formatting and includes the use of the Fill function to set the cell color. This last function has fill_type as one of its arguments. How do you set a solid fill for this? In previous versions, this was done using the following:

mycell.style.fill.fill_type = Fill.FILL_SOLID 

The documentation, which looks like it works, seems to suggest that setting fill_type = Fill.FILL_SOLID will do the trick (scroll down to the notes at the bottom of the page). But I'm trying to get the AttributeError attribute.

 from openpyxl.styles import Fill, Color from openpyxl.styles.colors import RED redfill = Fill(fill_type=Fill.FILL_SOLID,start_color=RED) Traceback (most recent call last): File "<pyshell#20>", line 1, in <module> redfill = Fill(fill_type=Fill.FILL_SOLID,start_color=RED) AttributeError: type object 'Fill' has no attribute 'FILL_SOLID' 

Any thoughts?

+6
source share
2 answers

Constants are now modular rather than class constants.

  from openpyxl.styles import fills, PatternFill fill = PatternFill(patternType=fills.FILL_SOLID) 

Although I think that just using patternType='solid'

+6
source

The style is unchanged after creation, you need to build a new one and assign it to the style property as follows:

 mycell.style = Style(fill=PatternFill(patternType='solid', fgColor=Color('FFFF0000'))) 

this will turn the cell red.

+8
source

Source: https://habr.com/ru/post/969718/


All Articles