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)
python使用xlwt写入excel
最新推荐文章于 2024-07-24 10:15:00 发布