openpyxl 单元格样式处理

1-openpyxl基础用法

2-openpyxl单元格样式处理

3-openpyxl日期格式处理

4-openpyxl单元格合并

5-openpyxl批量操作单元格

6-openpyxl数字格式处理

openpyxl 单元格样式处理

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

dest_filename = 'data.xlsx'
# 加载工作簿
wb = load_workbook(filename=dest_filename)
# 选择分页
sheet1 = wb['first sheet']
# 字体设置
# color: 字体颜色,size: 字体大写, name: 字体, bold: 加粗, underline: 下划线类型, strike: 删除线
sheet1['B6'].font = Font(color='FF00FF', size=12, name='隶书', bold=True, underline='double', strike=True)
sheet1['B6'].value = '样式处理'
# 填充色设置
# fgColor: 单元格填充色,tint: 颜色深浅 0-1, fill_type: 填充方式
sheet1['A1'].fill = PatternFill(fgColor='00ff00', fill_type='solid')
sheet1['A2'].fill = PatternFill(fgColor=Color(rgb='00ff00', tint=0.3), fill_type='darkTrellis')
sheet1['A3'].fill = PatternFill(fgColor=Color(rgb='00ff00', tint=0.7), fill_type='lightHorizontal')
sheet1['A4'].fill = PatternFill(fgColor=Color(rgb='00ff00', tint=1), fill_type='solid')
# 单元格边框
sheet1['C2'].border = Border(bottom=Side(style='thin', color='00ffff'),
                             right=Side(style='double', color='0000ff'),
                             left=Side(style='hair', color='0000ff'),
                             top=Side(style='dashDotDot', color='0000ff'))
sheet1['C2'].value = '边框'
# 单元格内容对齐
sheet1['D4'].alignment = Alignment(horizontal='center', vertical='center')
sheet1['D4'].value = '对齐'

wb.save(filename=dest_filename)

样式处理结果

猜你喜欢

转载自blog.csdn.net/BDawn/article/details/111403647