- # 引入两个库
- import xlrd
- from xlutils.copy import copy
- # 打开工作簿和工作表
- wb = xlrd.open_workbook('LIZI.xls')
- ws = wb.sheet_by_name('清单')
- # 复制工作簿
- nwb = copy(wb)
- nws = nwb.get_sheet('汇总')
- n = 1
- d = dict()
- # 循环累加
- while n < ws.nrows-1:
- n += 1
- l = ws.row_values(n)
- if (l[17],l[10],l[33]) in d.keys():
- d[l[17], l[10], l[33]] += int(l[20])
- else:
- d[l[17], l[10], l[33]] = int(l[20])
- r = 2
- # 写入工作表
- for val in d:
- nws.write(r, 0, r-1)
- nws.write(r, 1, val[0])
- nws.write(r, 2, val[1])
- nws.write(r, 3, val[2])
- nws.write(r, 4, d[val])
- r += 1
- nwb.save('LIZI.xls')
复制代码 |