python 使用 openpyxl 批量调整字体和样式

python 使用 openpyxl 批量调整字体和样式

      • 修改字体样式 Font(name,size,bold,italic,color)
      • 获取表格中格子的字体样式
      • 设置对齐样式 Alignment(horizontal,vertical,text_rotation,wrap_text)
      • 设置边框样式 Side(style,color)
      • 设置填充样式 PatternFill()、GradientFill()
      • 设置行高和列宽
      • 合并单元格与取消合并单元格
        • 合并单元格 merge_cells()
        • 取消合并单元格 unmerge_cells()

修改字体样式 Font(name,size,bold,italic,color)

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")

效果展示
python 使用 openpyxl 批量调整字体和样式_第1张图片

获取表格中格子的字体样式

  • font.name 字体名称
  • font.size 字体大小
  • font.bold 是否加粗 返回的是布尔值
  • font.italic 是否斜体 返回的是布尔值
  • font.color 字体颜色 返回颜色类型和相关参数
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)

Alignment(horizontal=水平对齐模式,vertical=垂直对齐模式,text_rotation=旋转角度,wrap_text=是否自动换行)

  • 水平对齐:
    horizontal代表水平方向,可以左对齐left,还有居中center和右对齐right,分散对齐distributed,跨列居中centerContinuous,两端对齐justify,填充fill,常规general
  • 垂直对齐:
    vertical代表垂直方向,可以居中center,还可以靠上top,靠下bottom,两端对齐justify,分散对齐distributed
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")

python 使用 openpyxl 批量调整字体和样式_第2张图片

设置边框样式 Side(style,color)

 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")

展示效果
python 使用 openpyxl 批量调整字体和样式_第3张图片

设置填充样式 PatternFill()、GradientFill()

语法:

  • PatternFill(fill_type=填充样式,fgColor=填充颜色)
  • GradientFill(stop=(渐变颜色 1,渐变颜色 2……))
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")

展示效果
python 使用 openpyxl 批量调整字体和样式_第4张图片

设置行高和列宽

语法:

.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(待合并的格子编号)
.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")

若合并的多个单元格中均有内容,只保留第一个单元格中的内容

效果如下:
python 使用 openpyxl 批量调整字体和样式_第5张图片

取消合并单元格 unmerge_cells()

语法:

.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")

注意:取消合并单元格,不能恢复合并单元格所丢失的数据

效果如下:
python 使用 openpyxl 批量调整字体和样式_第6张图片

你可能感兴趣的:(#,Python,自动化办公)