Home » excel » python – 'Cell' object has no attribute 'styles' when trying to set cell font color

python – 'Cell' object has no attribute 'styles' when trying to set cell font color

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m using openpyxl 2.0.5 and python 3.4 and I’m simply trying to set the font color to a cell to red

from openpyxl import Workbook

from openpyxl.styles import Color, Font, Style, colors

wb = Workbook()

ws = wb.active

cell = 'A1'

ws[cell].styles = Style(font=Font(color=Color(colors.RED))) 

Traceback (most recent call last): File
“C:/Users/b-rosard/PycharmProjects/Test/test.py”, line 12, in
ws[cell].styles = Style(font=Font(color=Color(colors.RED)))

AttributeError: ‘Cell’ object has no attribute ‘styles’

I was following the example here: http://openpyxl.readthedocs.org/en/latest/styles.html and I have no clue why I’m getting that error

How to&Answers:

Simple fix after looking at the attributes of Cell.

ws[cell].style = Style(font=Font(color=Color(colors.RED))) 

Answer:

This code worked for me. I am using python 3.x.

from openpyxl.styles import Color, Font, PatternFill

book = Workbook()
output = book.active

cell = output.cell(row = some value, column = some value)

cell.fill = cell.fill.copy(patternType = 'solid', fgColor = 'FFFFFF00')

‘FFFFFF00’ used for yellow