標籤:
#coding:utf-8#匯入相應模組import csv,xlwt,sys,os,fnmatch,xlrdfrom xlutils.copy import copy#對xls檔案中的絕對值資料求最大值並列表def max_excel(excel): rb1=xlrd.open_workbook(excel) #sheet0=rb1.sheets()[1] wb1=copy(rb1) #sheet頁通過sheet名稱得到的才能擷取行數和列數,但是不能進行寫操作,通過下標獲得的sheet可以進行寫操作,但是不能獲得行數和列數 sheet0=rb1.sheet_by_name("Absolute") sheet3=wb1.get_sheet(2) nrows1=sheet0.nrows ncols1=sheet0.ncols for k in range(0,ncols1,): sheet3.write(0,k,u‘炸點%d‘ %(k+1)) cols = sheet0.col_values(k) cmax=max(cols[1:]) sheet3.write(1,k, cmax) wb1.save(excel)#對xls檔案中的data資料求絕對值def abs_excel(excel): #print "3" rb=xlrd.open_workbook(excel) #sheet=rb.sheets()[0] wb=copy(rb) sheet = rb.sheet_by_name("data") #print sheet sheet2=wb.get_sheet(1) nrows=sheet.nrows ncols=sheet.ncols for i in range(nrows): for j in range(ncols): w=sheet.cell(i,j).value if (i==0): sheet2.write(i,j,w) else: v = float(w) sheet2.write(i,j,abs(v)) wb.save(excel) max_excel(excel)#另存新檔xls檔案def ex_file(mycsvfile): csvfile = open(mycsvfile,"rb") #csvfile = open("test.csv","rb") #建立excel檔案 myexcel = xlwt.Workbook() #建立sheet頁 mysheet1= myexcel.add_sheet("data") mysheet2= myexcel.add_sheet("Absolute") mysheet3= myexcel.add_sheet("MAX") #擷取csv的檔案名稱 portion = os.path.splitext(mycsvfile) #讀取csv中檔案資訊 reader = csv.reader(csvfile,dialect=‘excel‘) l = 0 #通過迴圈擷取單行資訊 for line in reader: r = 0 #通過雙重迴圈擷取單個單元資訊 for i in line: #通過雙重迴圈寫入excel表格 mysheet1.write(l,r,i) r+=1 l+=1 myexcel.save(portion[0]+".xls") excel = portion[0]+".xls" #print portion[0]+".xls" abs_excel(excel) def iterfindfiles(path, fnexp): for root, dirs, files in os.walk(path): for filename in fnmatch.filter(files, fnexp): yield os.path.join(root, filename) #批量處理if __name__=="__main__": mypath=raw_input("Please enter a path:") myfnexp=‘*.csv‘ #print 1 for filename in iterfindfiles(mypath,myfnexp): #print filename ex_file(filename) #abs_excel(filename) #max_excel(filename) raw_input (‘please enter to exit‘)
最後可以通過 python pyinstaller --console --onefile 絕對路徑\py檔案.py 命令生產成對應的exe檔案。
python操作csv-xls完善後的代碼