自学内容网 自学内容网

python-读写Excel:openpyxl-(3)单元格样式设置

目录

行高列宽

背景色

边框样式

字体样式

对齐样式


行高列宽

sht1.row_dimensions[5].height = 20  # 设置指定行的行高
sht1.column_dimensions['a'].width = 15  # 设置指定列的列宽

背景色

fill_type属性决定了背景填充的类型,可以是none、solid、darkDown、darkUp、darkGrid、darkTrellis、lightHorizontal、lightVertical和lightDown、lightUp、lightGrid和lightTrellis等。

from openpyxl.styles import Border, Side, Font, PatternFill, Alignment

sht1.cell(2, 2).fill = PatternFill(start_color='7CFC00', end_color='7CFC00', fill_type='solid')
sht1.cell(2, 3).fill = PatternFill(start_color='FF6347', end_color='FF6347', fill_type='darkDown')
sht1.cell(2, 4).fill = PatternFill(start_color='CD9B9B', end_color='CD9B9B', fill_type='lightDown')

边框样式

边框类型

left:设置左边框的样式和颜色。
right:设置右边框的样式和颜色。
top:设置顶部边框的样式和颜色。
bottom:设置底部边框的样式和颜色。
diagonal:设置对角线的样式和颜色。
diagonal_direction:设置对角线的方向。
vertical:设置垂直边框的样式和颜色。
horizontal:设置水平边框的样式和颜色。

边框线样式

thin:细线
medium:中等粗细线
thick:粗线
double:双线
hair:细线
dotted:点线
dashDot:点划线
dashDotDot:双点划线
dashed:虚线
dashDotDot:双点划线
mediumDashDot:中等点划线
mediumDashed:中等虚线
mediumDashDotDot:中等双点划线
slantDashDot:斜线点划线

border = Border(left=Side(border_style='thick', color='7CFC00'),
                right=Side(border_style='dotted', color='7CFC00'),
                top=Side(border_style='dashDotDot', color='7CFC00'),
                bottom=Side(border_style='mediumDashDotDot', color='7CFC00'))
sht1['c4'].border = border

字体样式

font = Font(name='Arial', size=12, bold=True, color='FF6347')
sht1['c3'].font = font

对齐样式

alignment = Alignment(horizontal='left', vertical='center')  # horizontal,vertical设置水平,垂直对齐样式
sht1['c3'].alignment = alignment

原文地址:https://blog.csdn.net/JBY2020/article/details/142789041

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!