Python OpenPyXL Font Bold for Excel Cells
In this Python OpenPyXL tutorial we learn how to set the font bold for Excel cells using the OpenPyXL package.
How to install the OpenPyXL package
To install the OpenPyXL package using the command below.
pip install openpyxl
How to set font bold 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 bold is True.
font = Font(bold=True)
Assign the font to A1 cell.
ws['A1'].font = font
Then save the Workbook as font_bold.xlsx file.
The complete Python program as below.
from openpyxl import Workbook from openpyxl.styles import Font wb = Workbook() ws = wb.active ws['A1'] = 'Tori Code' ws['A2'] = 'Tori Code' font = Font(bold=True) ws['A1'].font = font wb.save('font_bold.xlsx')
Execute the above Python program we will get the Excel file font_bold.xlsx as below screenshot.
Happy Coding 😊