官方文档: http://openpyxl.readthedocs.io/en/default/
OpenPyXL库 --单元格样式设置
单元格样式的控制,依赖openpyxl.style包,其中定义有样式需要的对象,引入样式相关:
from openpyxl.styles import PatternFill, Font, Alignment, Border, Side
- Border 边框 Side 边线
- PatternFill 填充
- Font 字体
- Aignment 对齐
以上基本可满足需要
基本用法是,将单元格对象的设置的属性赋为新的与默认不同的相应对象。
导入excel
from openpyxl import load_workbook
from openpyxl.styles import Border,Side
wb = load_workbook("模板.xlsx")#使用openpyxl读取xlsx文件,创建workbook
ws = wb.active
ws
<Worksheet "sheet1">
1、Border 边框 Side 边线
from openpyxl.styles import Border, Side
border_type=Side(border_style=None, color=\'FF000000\')
border = Border(left=border_type,
right=border_type,
top=border_type,
bottom=border_type,
diagonal=border_type,
diagonal_direction=0,
outline=border_type,
vertical=border_type,
horizontal=border_type
)
border_style的样式有:
‘dashDot’,‘dashDotDot’,‘dashed’,‘dotted’,‘double’,
‘hair’,‘medium’,‘mediumDashDot’,‘mediumDashDotDot’,
‘mediumDashed’,‘slantDashDot’,‘thick’,‘thin’
举例,原excel
# 样式1 - 窄边框,黑色
thin = Side(border_style="thin", color="000000")#边框样式,颜色
border = Border(left=thin, right=thin, top=thin, bottom=thin)#边框的位置
ws[\'A3\'].border = border #A3单元格设置边框
for row in ws[\'A5:D6\']:
for cell in row:
cell.border = border#A5:D6区域单元格设置边框
wb.save("test.xlsx")
效果:
# 样式2- 宽边框,蓝色
thin = Side(border_style="thick", color="0000FF")#边框样式,颜色
border = Border(left=thin, right=thin, top=thin, bottom=thin)#边框的位置
ws[\'A3\'].border = border #A3单元格设置边框
for row in ws[\'A5:D6\']:
for cell in row:
cell.border = border#A5:D6区域单元格设置边框
wb.save("test.xlsx")
效果:
2、字体设置
from openpyxl.styles import Font
font = Font(name=\'Calibri\',
size=11,
color=\'FF000000\',
bold=False,
italic=False,
vertAlign=None,
underline=\'none\',
strike=False)
字体名称、字体大小、字体颜色、加粗、斜体、纵向对齐方式(有三种:baseline,superscript, subscript)、下划线、删除线,字体颜色可以用RGB
或 aRGB
,
font = Font(size=14, bold=True, name=\'微软雅黑\', color="FF0000")#字体大小,加粗,字体名称,字体名字
ws[\'A3\']="欢迎关注:永恒君的百宝箱"
ws[\'A3\'].font = font
wb.save("test.xlsx")
3、填充
from openpyxl.styles import PatternFill
# fill_type 的样式为 None 或 solid
fill = PatternFill(fill_type = None,start_color=\'FFFFFF\',end_color=\'000000\')
fill_type
类型有:\'none\'、\'solid\'、\'darkDown\'、\'darkGray\'、\'darkGrid\'、\'darkHorizontal\'、\'darkTrellis\'、\'darkUp\'、\'darkVertical\'、\'gray0625\'、
\'gray125\'、\'lightDown\'、\'lightGray\'、\'lightGrid\'、\'lightHorizontal\'、
\'lightTrellis\'、\'lightUp\'、\'lightVertical\'、\'mediumGray\'
官方文档中写明,fill_type
若没有特别指定类型,则后续的参数都无效
所以上述代码就会出问题,start_color
代表前景色,end_color
是背景色,
之所以设置两个参数是为了方便样式颜色的填充
和渐变色
的显示(个人认为)
如果想要纯色填充
的话可以用\'solid\',然后令前景色为你需要的颜色即可,即:
fill = PatternFill(fill_type = None,start_color=\'FF0000\')
fill = PatternFill(patternType="solid", start_color="33CCFF")#纯色填充
ws[\'A3\']="欢迎关注:永恒君的百宝箱"
ws[\'A3\'].fill = fill
wb.save("test.xlsx")
4、对齐
from openpyxl.styles import Alignment
align = Alignment(horizontal=\'left\',vertical=\'center\',wrap_text=True)
horizontal
代表水平方向,可以左对齐left,还有居中center和右对齐right,分散对齐distributed,跨列居中centerContinuous,两端对齐justify,填充fill,常规general
vertical
代表垂直方向,可以居中center,还可以靠上top,靠下bottom,两端对齐justify,分散对齐distributed
自动换行:wrap_text
,这是个布尔类型的参数,这个参数还可以写作wrapText
align = Alignment(horizontal=\'right\',vertical=\'center\',wrap_text=True)#纯色填充
ws[\'A3\']="永恒君的百宝箱"
ws[\'A3\'].alignment = align
wb.save("test.xlsx")