from xlrd import open_workbook
from xlutils.copy import copy
import time
import os
import random
curtime = time.strftime('%Y%m%d%H%M%S', time.localtime())
old_name = '一个客户.xls'
new_name = 'myCustomer.xls'
my_excel = open_workbook(old_name)
new_excel = copy(my_excel)
sheet = new_excel.get_sheet(0)
old_sheet = my_excel.sheet_by_index(0)
for i in range(1, 20):
for j in range(3):
if j == 2:
# 客户名不能重复
num = random.randint(0, 10000)
value = "test"+curtime+str(num)
else:
# 其他列同第一行的数据 读取旧表中的值写入新表
value = old_sheet.cell_value(1, j)
sheet.write(i, j, value)
new_excel.save(new_name)
os.remove(old_name)
os.rename(new_name, old_name)