python 输出复合表头的表格文件
XlsxWriter 库制作带复合表头的表格
Demo
import xlsxwriter
workbook = xlsxwriter.Workbook('time.xlsx')
worksheet = workbook.add_worksheet("sheet1")
worksheet.set_column('A:G', 20)
cell_format = workbook.add_format({'align': 'center',
'valign': 'vcenter',
'border': 1})
# header
worksheet.merge_range('A1:A2', 'network', cell_format)
worksheet.merge_range('B1:D1', 'BGP', cell_format)
worksheet.merge_range('E1:G1', 'OSPF', cell_format)
worksheet.write('B2', 'snapshot', cell_format)
worksheet.write('C2', 'link failure', cell_format)
worksheet.write('D2', 'localPref change', cell_format)
worksheet.write('E2', 'snapshot', cell_format)
worksheet.write('F2', 'link failure', cell_format)
worksheet.write('G2', 'linkCost change', cell_format)
# content
index = 3
for network, res in self.result_data.items():
data = [network, res['bgp_snapshot'], res['bgp_lf'], res['bgp_lp'],
res['ospf_snapshot'], res['ospf_lf'], res['ospf_lc']]
loc = 'A'+str(index)
worksheet.write_row(loc, data, cell_format)
index += 1
workbook.close()
输出效果
纵使疾风起,人生不言弃!