Python OpenPyXL Font Color for Excel Cells

In this Python OpenPyXL tutorial we learn how to set font color for Excel cells using the OpenPyXL package Font class.

How to install the OpenPyXL package

To install the OpenPyXL package using the command below.

pip install openpyxl

How to set font color for the Excel cells

Import the Workbook and Font from the OpenPyXL package.

from openpyxl import Workbook
from openpyxl.styles import Font

Create a new Workbook object.

wb = Workbook()

Get the default active Worksheet object.

ws = wb.active

Assign value for A1, A2, A3, A4 and A5 cells.

ws['A1'] = 'Tori Code'
ws['A2'] = 'Tori Code'
ws['A3'] = 'Tori Code'
ws['A4'] = 'Tori Code'
ws['A5'] = 'Tori Code'

Create Font objects with specified color value and set different font colors for A1 to A5 cells.

font1 = Font(color='00FF0000')
ws['A1'].font = font1

font2 = Font(color='000000FF')
ws['A2'].font = font2

font3 = Font(color='00808080')
ws['A3'].font = font3

font4 = Font(color='003366FF')
ws['A4'].font = font4

font5 = Font(color='00CC99FF')
ws['A5'].font = font5

Then save the Workbook as font_color.xlsx file.

wb.save('font_color.xlsx')

The complete Python program as below.

font_bold.py

from openpyxl import Workbook
from openpyxl.styles import Font

wb = Workbook()
ws = wb.active

ws['A1'] = 'Tori Code'
ws['A2'] = 'Tori Code'
ws['A3'] = 'Tori Code'
ws['A4'] = 'Tori Code'
ws['A5'] = 'Tori Code'

font1 = Font(color='00FF0000')
ws['A1'].font = font1

font2 = Font(color='000000FF')
ws['A2'].font = font2

font3 = Font(color='00808080')
ws['A3'].font = font3

font4 = Font(color='003366FF')
ws['A4'].font = font4

font5 = Font(color='00CC99FF')
ws['A5'].font = font5

wb.save('font_color.xlsx')

Execute the above Python program we will get the Excel file font_color.xlsx as below screenshot.

Python OpenPyXL Font Color for Excel Cells

Happy Coding 😊