Set background and separate foreground color for cell in openpyxl
<p>I am using <strong>openpyxl 2.3.5</strong>.</p>

<p>I am writing an Excel sheet from Python and need to set a cell's background color to black and its foreground color (font color?) to white.</p>

<p>I know how to set the background color to white (leaving the foreground/font color):</p>

<pre><code>from openpyxl import Workbook
from openpyxl.styles import PatternFill

wb = Workbook()
ws = wb.create_sheet(title='test')

ws['A1'] = 'hello world'

#define the color:
whiteFill = PatternFill(fgColor='FFFFFF', bgColor='FFFFFF', fill_type='solid')
ws['A1'].fill = whiteFill
</code></pre>

<p>This method "PatternFill" is probably for setting gradients in general. I tried changing fgColor and bgColor to white and black, respectively, but no luck.</p>

<p>I have been looking around for help, but most answers are for earlier versions of openpyxl and apparently a lot has changed in the api.</p>

<p>While styles have changed a lot since version 1.x, fills haven't changed that much.</p>

<p>In this case you need to set the foreground to black (<code>000000</code>) and the background doesn't matter because the "pattern" is solid.</p>

python openpyxl