Python OpenPyXL Font Name for Excel Cells

In this Python OpenPyXL tutorial we learn how to set the font name 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 name 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 and A2 cells.

ws['A1'] = 'Tori Code'
ws['A2'] = 'Tori Code'

Create a new Font object with specified font name.

font = Font(name='Courier New')

Assign the font to A1 cell.

ws['A1'].font = font

Then save the Workbook as font_name.xlsx file.

wb.save('font_name.xlsx')

The complete Python program as below.

font_name.py

from openpyxl import Workbook
from openpyxl.styles import Font

wb = Workbook()
ws = wb.active

ws['A1'] = 'Tori Code'
ws['A2'] = 'Tori Code'

font = Font(name='Courier New')
ws['A1'].font = font

wb.save('font_name.xlsx')

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

Python OpenPyXL Font Name for Excel Cells

Happy Coding 😊