from openpyxl.styles import Font, Alignment, PatternFill
# 设置字体样式
font = Font(name='Calibri', size=12, bold=True, italic=False, underline='single')
ws['A1'].font = font
# 设置单元格对齐方式
alignment = Alignment(horizontal='center', vertical='center')
ws['A1'].alignment = alignment
# 设置单元格填充颜色
fill = PatternFill(patternType='solid', fgColor='FFC000')
ws['A1'].fill = fill
# 合并单元格
ws.merge_cells('A1:B2')
# 取消合并单元格
ws.unmerge_cells('A1:B2')
from openpyxl.chart import BarChart, Reference, Series
# 生成柱状图
chart = BarChart()
data = Reference(ws, min_row=1, max_row=5, min_col=2, max_col=3)
categories = Reference(ws, min_row=1, max_row=5, min_col=1)
chart.add_data(data, titles_from_data=True)
chart.set_categories(categories)
ws.add_chart(chart, 'E1')
# 设置单元格公式
ws['C1'] = '=SUM(A1:B1)'
# 计算公式
wb.calculation = 'automatic'
# 设置工作表保护密码
ws.protection.set_password('password')
# 启用工作表保护
ws.protection.enable()