公司小花们整理Excel需要一个一个订单进行处理校对,然后整理价格收益等,项目开发阶段,没事帮助了一下小花们,助人为乐快乐多,哈哈哈
import xlrd from xlutils import copy ''' 帮助运营人员跑订单及金额相关数据 ''' #读取导出表的订单号等信息,在0,1,10 列,如需改列直接改方法,返回二维数组 def read_xls(file,index): book=xlrd.open_workbook(file) sheet1=book.sheet_by_index(index) lnow=[] for i in range(1,sheet1.nrows): lnow.append([sheet1.cell_value(i,0),sheet1.cell_value(i,1),sheet1.cell_value(i,10)]) return lnow #读取需要对应需要处理数据的订单号 def read_data(file,index,start,end,col): book = xlrd.open_workbook(file) sheet1 = book.sheet_by_index(index) li = [] for i in range(start, end): li.append(int(sheet1.cell_value(i, col))) return li #匹配2个Excel中数据,提取需要的数据拼接成二维数组 def compare_data(list1,list2): lnow_sh = [] for k in list1: a = True for j in list2: if int(k) == int(j[0]): lnow_sh.append(j) a = False break if a: lnow_sh.append([k, '', '']) return lnow_sh #把提取的需要的数据的二维数组写入原文件对应的列,且保存 def write_xls(file,sheetindex,list,start,row,index): book = xlrd.open_workbook(file) newbook = copy.copy(book) sheet1 = newbook.get_sheet(sheetindex) for col, t in enumerate(list, start): sheet1.write(col, row, t[index]) newbook.save(file) if __name__ == '__main__': x=read_xls("201949.xls",0) a=read_data("2019-3.xls",1,2,757,4) b=read_data("2019-3.xls",2,2,49,5) c=read_data("2019-3.xls",3,2,168,3) r=compare_data(a,x) t=compare_data(b,x) z=compare_data(c,x) write_xls("2019-3.xls",1,r,2,5,1) write_xls("2019-3.xls",1,r,2,6,2) write_xls("2019-3.xls",2,t,2,6,1) write_xls("2019-3.xls",2,t,2,9,2) write_xls("2019-3.xls",3,z,2,4,1) write_xls("2019-3.xls",3,z,2,7,2)