Change font color of excel cells using Openpyxl in Python

Here, we will learn how to change the font color of excel cells using Openpyxl in Python.

To do this, we first need to import Font and Workbook from openpyxl as follows:

import openpyxl
from openpyxl.styles import Font
from openpyxl import Workbook

Then, we need to create a workbook and select a spreadsheet to work on as directed:

wb=Workbook()
w_sheet=wb.active

Then, we enter the data in the spreadsheet:

w_sheet['B1']='learning'
w_sheet['C2']='OPENPYXL'
w_sheet['D3']='using python'

Now, assign the names to the cells you want to change the style of:

a1=w_sheet['C2']
a2=w_sheet['D3']

Changing the font color: using text color specified in RGB

a1.font=Font(color='003366FF')
a2.font=Font(color='00FF0000')

Then, save the spreadsheet:

wb.save('ctpl.xlsx')

If you open this .xlsx file, you will obtain the following output:

Change font color of excel cells using Openpyxl in Python

Leave a Reply

Your email address will not be published. Required fields are marked *