没啥说的,直接上代码。。。
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import xlwt
import time
import random
book = xlwt.Workbook(encoding='utf-8')
sheet = book.add_sheet('sheet1', cell_overwrite_ok=True)
row_col_list = [9, 10]
def set_title():
title_kwgs = {
"colnum1": "colnum1_name", "colnum2": "colnum2_name", "colnum3": "colnum3_name",
"colnum4": "colnum4_name", "colnum5": "colnum5_name",
"colnum6": "colnum6_name", "colnum7": "colnum7_name", "colnum8": "colnum8_name",
"colnum9": "colnum9_name", "colnum10": "colnum10_name"
}
for i in range(1):
for j in range(row_col_list[1]):
sheet.write(i, j, title_kwgs["colnum%s" % str(j + 1)])
def excel_data_generator():
col_kwgs = {
"colnum1":"colnum1_value", "colnum2":"colnum2_value", "colnum3":"colnum3_value",
"colnum4":"colnum4_value", "colnum5":"colnum5_value",
"colnum6":"colnum6_value", "colnum7":"colnum7_value", "colnum8":"colnum8_value",
"colnum9":"colnum8_value", "colnum10":"colnum10_value"
}
colnum5_begin = int(str(random.Random.random(random.Random())).replace('0.', ''))
for i in range(1, row_col_list[0] + 1):
for j in range(row_col_list[1]):
if j == 4:
sheet.write(i, j, str(colnum5_begin))
colnum5_begin += 1
else:
sheet.write(i, j, col_kwgs["colnum%s" % str(j + 1)])
book.save("D:\\testdata\\haha\\test%s.xls" % str(time.time()).replace('.', ''))
# time.sleep(1)
# print('function executed!')
if __name__ == '__main__':
for i in range(10):
set_title()
excel_data_generator()