python 写入execl记录

记录代码中关于写execl的操作

# 创建execl
workbook = xlwt.Workbook(encoding='utf8')
# 创建样式实例
style = xlwt.XFStyle()
# 创建位置样式
alignment = xlwt.Alignment()
# 设置居中
alignment.vert = xlwt.Alignment.VERT_CENTER
# 将位置样式绑定到样式实例
style.alignment = alignment

for instance_id, items in result.items():
    # 新建sheet表
    sheet = workbook.add_sheet(instance_id)
    # 设置列宽度
    sheet.col(0).width = 15000
    sheet.col(1).width = 10000
    # 写入单元格
    sheet.write(0, 0, label='Theme')
    sheet.write(0, 1, label='AppId/Address')
    row_index = 1
    for theme, clients in items.items():
        # count = len(clients)
        # 写入合并单元格 [row_start, row_end, col_start, col_end, content, style]
        # sheet.write_merge(row_index, row_index + count - 1, 0, 0, theme, style)
        for client in clients:
            # 写入单元格  [row, col, content]
            sheet.write(row_index, 0, theme)
            sheet.write(row_index, 1, client)
            row_index += 1
# 保存到文件
workbook.save('file.xls')
原文地址:https://www.cnblogs.com/Peter2014/p/10558639.html