Using openpyxl to manipulate Excel files, what is the best way to set the cell background colour?
I got this from stackoverflow:
from openpyxl.styles import Color, Fill
from openpyxl.cell import Cell
_cell.style.fill.fill_type = Fill.FILL_SOLID
_cell.style.fill.start_color.index = Color.DARKRED
This post in from 2011. Is there a better way to do it in python 3.5?
All I want is for every other column to have a yellow background, makes reading the table easier somehow.
Thanks a lot! I see I need to get better at searching the web!
Oh dear!
Using this code:
for sheet in targetFileSheetNames:
targetFileActiveSheet = targetFile.get_sheet_by_name(sheet)
maxRow = targetFileActiveSheet.max_row
maxCol = targetFileActiveSheet.max_column
for colNum in range(2, maxCol + 1, 2):
for rowNum in range(1, maxRow + 1):
targetFileActiveSheet.cell(row=rowNum, column=colNum).fill = PatternFill(bgColor='FFEE08', fill_type = 'solid')
no matter what hex number I use, from FFFFFF to 000000, all I get are black columns.
According to gimp, a nice yellow colour has the number FFEE08.
Can anyone see what is wrong? I imported PatternFill and I get no errors, just black columns after saving.
This does the job:
targetFileActiveSheet.cell(row=rowNum, column=colNum).fill = PatternFill(start_color='FFEE08', end_color='FFEE08', fill_type = 'solid')
No idea why bgColor does not work, but I get what I want with this.
try "
fgColor" instead of "
bgColor"
targetFileActiveSheet.cell(row=rowNum, column=colNum).fill = PatternFill(fgColor='FFEE08', fill_type = 'solid')
seems to work with openpyxl 2.5.0
(don't ask why... just guessed and tried...)
(Feb-20-2018, 08:37 PM)theozh Wrote: [ -> ]try "fgColor" instead of "bgColor"
targetFileActiveSheet.cell(row=rowNum, column=colNum).fill = PatternFill(fgColor='FFEE08', fill_type = 'solid')
seems to work with openpyxl 2.5.0
(don't ask why... just guessed and tried...)
I used this and all above mentioned methods but nothing has been reflected in my excel file. I seen that there are no errors are reflected in my python console.