I am currently using openpyxl v2.2.2 for Python 2.7 and i wanted to set colors to cells. I have used the following imports
JavaScript
x
6
1
import openpyxl,
2
from openpyxl import Workbook
3
from openpyxl.styles import Color, PatternFill, Font, Border
4
from openpyxl.styles import colors
5
from openpyxl.cell import Cell
6
and the following is the code I tried using:
JavaScript
1
9
1
wb = openpyxl.Workbook()
2
ws = wb.active
3
4
redFill = PatternFill(start_color='FFFF0000',
5
end_color='FFFF0000',
6
fill_type='solid')
7
8
ws['A1'].style = redFill
9
but I get the following error:
JavaScript
1
4
1
Traceback (most recent call last)
2
self.font = value.font.copy()
3
AttributeError: 'PatternFill' object has no attribute 'font'
4
Any idea on how to set cell A1 (or any other cells) with colors using openpyxl?
Advertisement
Answer
I believe the issue is that you’re trying to assign a fill object to a style.
ws['A1'].fill = redFill
should work fine.