将字典数据写入表格

#接上篇
import xlwt
#设置表格样式
def set_style(name,height,bold=False):
    style = xlwt.XFStyle()
    font = xlwt.Font()
    font.name = name
    font.bold = bold
    font.color_index = 4
    font.height = height
    style.font = font
    return style
#写Excel
def write_excel(dict1):
    f = xlwt.Workbook()
    sheet1 = f.add_sheet('data',cell_overwrite_ok=True)
    row0 = ["number","classname"]
    colum0 = list(dict1.keys())
    colum1 = list(dict1.values())
    #写表头
    for i in range(0,len(row0)):
        sheet1.write(0,i,row0[i],set_style('Times New Roman',220,True))
    #写第一列数据
    for i in range(0,len(colum0)):
        sheet1.write(i+1,0,colum0[i],set_style('Times New Roman',220,True))
    #写第二列数据
    for i in range(0,len(colum1)):
        sheet1.write(i+1,1,colum1[i],set_style('Times New Roman',220,True))
    f.save('test2.xls')
if __name__ == '__main__':

    write_excel(gt_dict)

你可能感兴趣的:(python)