代码如下:
# encoding=gbk
import xlsxwriter
# 自定义单元格格式
def set_merge_style(workbook, tag):
number_format, font_color, align = '', '', 'center'
if tag == 'head1':
bold, font_size, border = True, 14, 1
fg_color = '#4F81BD'
font_color = 'white'
else:
bold, font_size, border = True, 12, 1
fg_color = '#FFFFFF'
font_color = 'red'
number_format = '0.00%'
cell_format = workbook.add_format({
'bold': bold,
'font_size': font_size,
'border': border,
'align': align,
'valign': 'vcenter', # 垂直居中
'fg_color': fg_color, # 颜色填充
'num_format': number_format,
'font_color': font_color,
'font_name': '宋体',
})
return cell_format
workbook = xlsxwriter.Workbook('demo.xlsx')
worksheet = workbook.add_worksheet('表格1')
# 合并A列-N列,第一个参数是位置,第二个参数是内容,第三个参数是自定义格式
worksheet.merge_range('A1:N2', 'xx模型xx清单',set_merge_style(workbook, tag='head1'))
worksheet.merge_range('A3:N3', '次级标题1',set_merge_style(workbook, tag='head2'))
#设置行高,set_tow(行号,对应行高)
worksheet.set_row(0, 25)
worksheet.set_row(1, 25)
worksheet.set_row(2, 20)
workbook.close()