4 Examples to Make Excel Text Bold, Italic by openpyxl

In this tutorial, we will show you examples of making text bold in Excel cells, rows and columns by using openpyxl library.

How to make text bold, italic, strikethrough, and underlines using openpyxl

In this tutorial, we will show you examples of making text bold in Excel cells, rows, and columns by using openpyxl library.

For our examples, we will use the following sample sheet:

Excel-bold-italic-sample

Steps to style text bold, italic, and strikethrough sheet cells

Step 1

Load the required libraries:

from openpyxl import load_workbook

from openpyxl.styles import Font

Step 2

Load workbook & specify active sheet:

wb_style = load_workbook('test_Excel.xlsx')

sheet = wb_style.active

Step 3

Apply Style:

cell_bold = sheet["C5"]

cell_bold.font = Font(bold=True)

Step 4

Save the Workbook:

wb_style.save("test_Excel.xlsx")

Complete code and result sheet

Now let us combine all the above code in addition to making text italic and strikethrough and see the result sheet. Our target is to make text bold for C5 cell, italic for C6, and strikethrough for C7 cell:

Python Program to make text bold, italic, strikethrough

# Bold, Italic, strikethrough example
from openpyxl import load_workbook
from openpyxl.styles import Font

#Loading the Workbook
wb_style = load_workbook('test_Excel.xlsx')

#Accessing Product Informaiton Sheet
sheet = wb_style.active

#Specify cell and apply style
cell_bold = sheet["C5"]
cell_bold.font = Font(bold=True)

cell_italic = sheet["C6"]
cell_italic.font = Font(italic=True)

cell_strike = sheet["C7"]
cell_strike.font = Font(strikethrough=True)

wb_style.save("test_Excel.xlsx")

Result:

Excel-Style-Bold

Making whole row bold, italic, strikethrough, and underline example

In this example, we make 4th row bold, 5th row italic, 6th row strikethrough, and 7th row underline by using openpyxl library. Have a look at code and compare the sheet before and after code execution:

The code:

# Bold, Italic, strikethrough example
from openpyxl import load_workbook
from openpyxl.styles import Font

#Loading the Workbook
wb_style = load_workbook('test_Excel.xlsx')

#Accessing Product Informaiton Sheet
sheet = wb_style.active

#Logic for making row text bold
for rows in sheet.iter_rows(min_row=4, max_row=4, min_col=None):

   for cell in rows:

     cell.font = Font(bold=True)

#Logic for making row text italic
for rows in sheet.iter_rows(min_row=5, max_row=5, min_col=None):
   for cell in rows:
     cell.font = Font(italic=True)

#Logic for making row text strikethrough
for rows in sheet.iter_rows(min_row=6, max_row=6, min_col=None):
   for cell in rows:
     cell.font = Font(strikethrough=True)

#Logic for making row text underline
for rows in sheet.iter_rows(min_row=7, max_row=7, min_col=None):

   for cell in rows:

     cell.font = Font(underline="double")

wb_style.save("test_Excel.xlsx")

Result:

Excel-italic-strike-un

You may notice the highlighted rows number 4,5,6, and 7.

Applying all styles at one line example

In the example below, we applied bold, italic, underline and strikethrough in one line of code to four rows:

The code:

# Bold, Italic, strikethrough, underline example
from openpyxl import load_workbook
from openpyxl.styles import Font

#Loading the Workbook
wb_style = load_workbook('test_Excel.xlsx')


#Accessing Product Informaiton Sheet
sheet = wb_style.active

#Logic for making row text bold, italic, strikethrough and underlined = doubleAccounting
for rows in sheet.iter_rows(min_row=4, max_row=7, min_col=None):

   for cell in rows:

     cell.font = Font(bold=True, italic=True, strikethrough=True, underline="doubleAccounting")


wb_style.save("test_Excel.xlsx")

Result:

Excel-bold-italic-rows

Applying the above style to the column example

Similarly, you may apply the bold, italic etc. styles to columns by using iter_cols function. In the example below, we changed the font of columns number 2 and 3 and applied:

  • Bold
  • Italic
  • Underlined
  • strikethrough

The code:

# Bold, Italic, strikethrough, underline example
from openpyxl import load_workbook
from openpyxl.styles import Font

#Loading the Workbook
wb_style = load_workbook('test_Excel.xlsx')

#Accessing Product Informaiton Sheet
sheet = wb_style.active

#Logic for making Columns italic, bold etc.
for rows in sheet.iter_cols(min_col=2, max_col=3, min_row=2, max_row=None):

   for cell in rows:

     cell.font = Font(bold=True, italic=True, strikethrough=True, underline="single")

wb_style.save("test_Excel.xlsx")

Result:

Excel-bold-italic-colu