python使用xlwt写入excel

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_28893679/article/details/84540250
def write_list_to_xls(file_name, sheet_name, message_list, firstcolumn_list, param_list):
    # file_name 文件名
    # sheet_name 表单名
    # message_list要写入excel文件的参数list
    # firstcolumn_list第一行参数中的值
    # param_list,message_list中每个元素的key值
    workbook = xlwt.Workbook()
    sheet = workbook.add_sheet(sheet_name, cell_overwrite_ok=True)
    # 往第一行中写数据
    first_column_count = 0
    for prompt in firstcolumn_list:
        sheet.write(0, first_column_count, prompt.decode('UTF-8'))
        first_column_count += 1
    # 开始写后面的数据
    row_count = 1
    for message in message_list:
        # 第几列
        temp_column_count = 0
        for key in param_list:
            sheet.write(row_count, temp_column_count, message.get(key))
            temp_column_count += 1
        # 行号递增
        row_count += 1
    workbook.save(file_name)

猜你喜欢

转载自blog.csdn.net/qq_28893679/article/details/84540250
今日推荐