使用Python设置excel单元格的字体(font值)
一、前言
通过使用Python的openpyxl库,来操作excel单元格,设置单元格的字体,也就是font值。
把学习的过程分享给大家。大佬勿喷!
二、程序展示
1、新建excel
import openpyxl
from openpyxl.styles import Font
wb = openpyxl.Workbook()
sheet = wb.active
2、设置单元格字体
font1 = Font(name="Times New Roman", bold=True)
sheet['F4'].font=font1
sheet['F4'] = "Times New Roman, bold = True"
在excel的F4单元格设置字体格式为“Times New Roman”,字体加粗。
font2 = Font(size=30, italic=True, shadow= True,underline='single')
sheet['G6'].font=font2
sheet['G6'] = "size=30, italic=True, shadow=True"
wb.save('F:\python_study\表格\设置font.xlsx')
在excel的G6单元格,设置字的大小为30,字体倾斜,带阴影,带下划线。
3、结果展示
点开Font函数,可以看到通过设置的格式很多,可以逐个去试验下,看下效果。注意有的参数值是bool型,有的值是字符串型。