Python-Excel-字体及对齐方式设置
需要将单元格合并居中
from openpyxl import load_workbook
from openpyxl.styles import Font, colors, Alignment
import os
os.chdir(r'C:\Users\86159\Desktop\file')
excel_address = r"demine.xlsx"
wb = load_workbook(excel_address)
sht = wb.active
sht.merge_cells(start_row=1,end_row=1,start_column=1,end_column=22) #首行
sht["A1"] = "交易日报"
sht['A1'].alignment = Alignment(horizontal='center', vertical='center')
sht.merge_cells(start_row=2,end_row=3,start_column=1,end_column=1) #时间
sht['A2'].alignment = Alignment(horizontal='center', vertical='center')
sht["A2"] = "统计时间"
sht["A1"] = "统计" font_set = Font(name='Arial', size=24, italic=True, color=colors.BLUE, bold=True, underline='doubleAccounting') sht['A1'].font = font_set sht["A5"] = "统计" sht['A5'].alignment = Alignment(horizontal='center', vertical='center') sht["A6"] = "统计" sht['A6'].alignment = Alignment(horizontal='left', vertical='bottom') sht["A7"] = "统计" sht['A7'].alignment = Alignment(horizontal='right', vertical='top') sht["A9"] = "统计" sht['A9'].alignment = Alignment(horizontal='left', vertical='top', wrap_text=True) sht["A10"] = "统计" sht['A10'].alignment = Alignment(horizontal='left', vertical='top', wrap_text=False) wb.save('test2.xlsx')