How To Set Cell Styles Using OpenPyX – Font

Method

Create a `Font` object to define the font’s name, size, boldness, italics, etc. The main parameters and their descriptions are as follows:

  • nameFont name.
  • sizeFont size.
  • colorFont color.
  • boldWhether the font is bold.
  • italicWhether the font is italic.
  • underlineUnderline setting, can be “none”, “single”, or “double”.
  • strikeWhether the font has a strike-through.
  • strikethroughAlternate for strike-through.
  • vertalignSuperscript, subscript setting, can be “superscript”, “subscript”, or “baseline”.

Sample Code

#Cell Style: Setting Fonts

#Import load_workbook function
from openpyxl import Workbook

#Create a new workbook
wb=Workbook()
#Get the active worksheet
ws=wb.active

#Import the Font class
from openpyxl.styles import Font

#Create a font
font=Font(name='Arial', size=12, bold=True, italic=True, \
          underline='single', strike=False, color='FF0000')

#Set the font of a cell
ws.cell(row=3, column=3).font=font
ws['C3']='Test123'

wb.save('test.xlsx')
wb.close()
Set Cell Styles Using OpenPyX - Font

Leave a Reply

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