Python 设置字体样式

时间:2021-06-12 23:37:53
# 1、先导入分别可指定单元格字体相关,颜色,和对齐方式的类
from openpyxl.styles import Font, colors, Alignment

# 2、配置字体格式为:样式(Times New Roman)、尺寸(11)、斜体(flase)、颜色(黑色)、粗体(flase),这里可以根据自己需求修改
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'
#====================================

# 设置字体格式
sheet1.cell(row=i,column=1).font=font_style
# 设置字体对齐方式,这里为垂直居中跟水平居中,除了center,还可以用right、left等参数
sheet1.cell(row=i,column=1).alignment = Alignment(horizontal='center', vertical='center')