from openpyxl.styles import Font
from openpyxl import load_workbook
'''
Font(name=字体名称,size=字体大小,bold=是否加粗,italic=是否斜体,color=字体颜色)
'''
workbook = load_workbook(filename="../excelDemo/testCase.xlsx")
sheet = workbook.active
cell = sheet["A1"]
font = Font(name="微软雅黑",size=20,bold=True,italic=True,color="FF0000")
# 这个 color 是 RGB 的 16 进制表示
cell.font = font
workbook.save(filename="../excelDemo/testCase.xlsx")
from openpyxl.styles import Font
from openpyxl import load_workbook
workbook = load_workbook(filename="../excelDemo/testCase.xlsx")
sheet = workbook.active
cell = sheet["A1"]
font = cell.font
print(font.name, font.size, font.bold, font.italic, font.color)
'''
输出结果:
微软雅黑 20.0 True True
Parameters:
rgb='00FF0000', indexed=None, auto=None, theme=None, tint=0.0, type='rgb'
'''
Alignment(horizontal=水平对齐模式,vertical=垂直对齐模式,text_rotation=旋转角度,wrap_text=是否自动换行)
workbook = load_workbook(filename="../excelDemo/testCase.xlsx")
sheet = workbook.active
cell = sheet["A3"]
alignment = Alignment(horizontal="center",vertical="center",text_rotation=45,wrap_text=True)
cell.alignment = alignment
workbook.save(filename="../excelDemo/testCase.xlsx")
Side(style=边线样式,color=边线颜色)
* style 参数的种类: 'double, 'mediumDashDotDot', 'slantDashDot',
'dashDotDot','dotted','hair', 'mediumDashed, 'dashed', 'dashDot', 'thin',
'mediumDashDot','medium', 'thick'
Border(left=左边线样式,right=右边线样式,top=上边线样式,bottom=下边线样式)
from openpyxl.styles import Side,Border
from openpyxl import load_workbook
workbook = load_workbook(filename="../excelDemo/testCase.xlsx")
sheet = workbook.active
cell = sheet["D6"]
side1 = Side(style="thin",color="FF0000")
side2 = Side(style="thick",color="FFFF0000")
border = Border(left=side1,right=side1,top=side2,bottom=side2)
cell.border = border
workbook.save(filename="../excelDemo/testCase.xlsx")
语法:
from openpyxl.styles import PatternFill,GradientFill
from openpyxl import load_workbook
workbook = load_workbook(filename="../excelDemo/testCase.xlsx")
sheet = workbook.active
cell_b9 = sheet["B9"]
pattern_fill = PatternFill(fill_type="solid",fgColor="99ccff")
cell_b9.fill = pattern_fill
cell_b10 = sheet["B10"]
gradient_fill = GradientFill(stop=("FFFFFF","99ccff","000000"))
cell_b10.fill = gradient_fill
workbook.save(filename="../excelDemo/testCase.xlsx")
语法:
.row_dimensions[行编号].height = 行高
.column_dimensions[列编号].width = 列宽
from openpyxl import load_workbook
workbook = load_workbook(filename="../excelDemo/testCase.xlsx")
sheet = workbook.active
# 设置第 1 行的高度
sheet.row_dimensions[1].height = 50
# 设置 B 列的宽度
sheet.column_dimensions["B"].width = 20
workbook.save(filename="../excelDemo/testCase.xlsx")
也可以统一整张表的行高和列宽
from openpyxl import load_workbook
workbook = load_workbook(filename="../excelDemo/testCase.xlsx")
sheet = workbook.active
# 将整个表的行高设置为 50,列宽设置为 30;
sheet.row_dimensions.height = 50
sheet.column_dimensions.width = 30
workbook.save(filename="../excelDemo/testCase.xlsx")
语法:
.merge_cells(待合并的格子编号)
.merge_cells(start_row=起始行号,start_column=起始列号,end_row=结束行号,end_column=结束列号)
from openpyxl import load_workbook
workbook = load_workbook(filename="./testCase.xlsx")
sheet = workbook.active
sheet.merge_cells("C1:D2")
sheet.merge_cells(start_row=7,start_column=1,end_row=8,end_column=3)
workbook.save(filename="./testCase.xlsx")
若合并的多个单元格中均有内容,只保留第一个单元格中的内容
语法:
.unmerge_cells(待合并的格子编号)
.unmerge_cells(start_row=起始行号,start_column=起始列号,end_row=结束行号,end_column=结束列号)
from openpyxl import load_workbook
workbook = load_workbook(filename="./testCase.xlsx")
sheet = workbook.active
sheet.merge_cells("C1:D2")
sheet.merge_cells(start_row=7,start_column=1,end_row=8,end_column=3)
workbook.save(filename="./testCase.xlsx")
注意:取消合并单元格,不能恢复合并单元格所丢失的数据