import openpyxl
from openpyxl.styles import Font
from openpyxl.styles import colors
wb = openpyxl.Workbook()
ws = wb.active
ws.title = 'Font'
#默认字体 11pt,Calibri
italic24Font = Font(size=24,italic=True) #设置字体对象
ws['B3'].font = italic24Font
ws['B3']='24pt Italic'
ws['A3']='24pt Italic'
boldRedFont = Font(name= 'Time New Roman',bold=True,color=colors.COLOR_INDEX[2])
ws['A1'].font = boldRedFont
ws['A1'] = 'Bold Red Time New Roman'
ws = wb.copy_worksheet(wb['Font']) #表单对象
ws.title = 'Formula' #公式
ws['A1'] = 200
ws['A2'] = 300
ws['A3'] = '=SUM(A1:A2)'
#setting row height and column width dimension 尺寸规模
ws = wb.create_sheet('dimensions')
ws['A1'] = 'Tall row'
ws.row_dimensions[1].height =70
ws['B2'] = 'Wide column'
ws.column_dimensions['B'].width =20
#Merging cells 合并单元格
ws = wb.create_sheet('merged')
ws.merge_cells('A1:D3')
ws['A1'] = 'Tweleve cells merged together'
ws.merge_cells('C5:D5')
ws['C5'] = 'Two merged cells'
#Unmerging cells 取消合并单元格
ws = wb.copy_worksheet(wb['merged'])
ws.title = 'unmerged'
ws.unmerge_cells('A1:D3')
ws.unmerge_cells('C5:D5')
wb.save('ss.xlsx')
openpyxl day2 字体设置
最新推荐文章于 2024-05-06 15:03:20 发布