欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 手游 > python-读写Excel:openpyxl-(3)单元格样式设置

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

2024/10/25 0:30:46 来源:https://blog.csdn.net/JBY2020/article/details/142789041  浏览:    关键词: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, Alignmentsht1.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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com