# 1. First import the classes that can specify the cell font, color, and alignment.
from openpyxl.styles import Font, colors, Alignment
# 2. Configure the font format as: style (Times New Roman), size (11), italic (flase), color (black), bold (flase), you can modify it according to your needs
font_style = Font(name='Times New Roman', size=11, italic=False, color='FF000000', bold=False)
#=====常用字体颜色配置数值=========== # BLACK = 'FF000000' # WHITE = 'FFFFFFFF' # RED = 'FFFF0000' # DARKRED = 'FF800000' # BLUE = 'FF0000FF' # DARKBLUE = 'FF000080' # GREEN = 'FF00FF00' # DARKGREEN = 'FF008000' # YELLOW = 'FFFFFF00' # DARKYELLOW = 'FF808000' #====================================
# Set the font format
sheet1.cell(row=i,column=1).font=font_style
# Set the font alignment. Here are vertical centering and horizontal centering. In addition to center, you can also use parameters such as right and left.
sheet1.cell(row=i,column=1).alignment = Alignment(horizontal='center', vertical='center')
Recommended related tutorials: Python video tutorial
The above is the detailed content of How to change font in python. For more information, please follow other related articles on the PHP Chinese website!