Python Forum
Best way to set cell background colour in openpyxl - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Best way to set cell background colour in openpyxl (/thread-6041.html)



Best way to set cell background colour in openpyxl - Pedroski55 - Nov-03-2017

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.


RE: Best way to set cell background colour in openpyxl - Larz60+ - Nov-03-2017

from openpyxl.styles import PatternFill
sheet['A1'].fill = PatternFill(bgColor="FFC7CE", fill_type = "solid")
Copied from post https://stackoverflow.com/questions/35918504/adding-a-background-color-to-cell-openpyxl
by https://stackoverflow.com/users/4686330/avik-samaddar


RE: Best way to set cell background colour in openpyxl - Pedroski55 - Nov-04-2017

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.


RE: Best way to set cell background colour in openpyxl - theozh - Feb-20-2018

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...)


RE: Best way to set cell background colour in openpyxl - sumandas89 - Aug-08-2018

(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.