1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| import xlrd import xlsxwriter
fileDemo = r'被读取的表格路径' insertData = r'数据写入的表格路径' data = xlrd.open_workbook(fileDemo)
first_sheet = data.sheets()[0]
content = first_sheet.row(3)
""" content:[empty:'', empty:'', text:'91330108MA27XWH96M', text:'杭州西西沃', text:'万里测试真实企业', empty:'', empty:'', empty:'', empty:'', empty:'',empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', empty:'', empty:''] 通过上面print内容可以看到,直接读出的内容,虽然是list,但每条数据前加了test字样,不能直接强转成tuple为我们所用, 于是自定义一个转换方法将其转换为可以用的list以便后面强转成tuple """ def rowToTuple(rowNum): data = [] for i in range(len(content)): data.append(first_sheet.cell_value(rowNum, i)) return tuple(data)
header = rowToTuple(2)
data1 = rowToTuple(3)
""" 如果数据量非常大,可以启用constant_memory,这是一种顺序写入模式,得到一行数据就立刻写入一行, 而不会把所有的数据都保持在内存中。 如果不启用此模式,当数据量巨大时,程序很大概率地卡死 """ workbook = xlsxwriter.Workbook(insertData, {'constant_menory': True})
new_sheet = workbook.add_worksheet()
startNum = 1
startValues = ['000000000000001','00000001']
col = ['C','D']
new_sheet.write_row('A' + str(startNum), header)
for i in range(1000000): print("正在生成第",i+1, "条数据...") new_sheet.write_row('A' + str(startNum + i + 1), data1) for m in col: length = len(startValues[col.index(m)]) content = str(int(startValues[col.index(m)]) + i) if (len(content) < length): content = '0'* (length - len(content)) + content new_sheet.write(m + str(startNum + i + 1), content) workbook.close()
|