最近由于經常要用到Excel,需要根據Excel表格中的內容對一些apk進行處理,手動處理很麻煩,于是決定寫腳本來處理。首先貼出網上找來的讀寫Excel的腳本。
1.讀取Excel(需要安裝xlrd):
#-*- coding: utf8 -*-import xlrd fname = "reflect.xls"bk = xlrd.open_workbook(fname)shxrange = range(bk.nsheets)try: sh = bk.sheet_by_name("Sheet1")except: print "no sheet in %s named Sheet1" % fname#獲取行數nrows = sh.nrows#獲取列數ncols = sh.ncolsprint "nrows %d, ncols %d" % (nrows,ncols)#獲取第一行第一列數據 cell_value = sh.cell_value(1,1)#print cell_value row_list = []#獲取各行數據for i in range(1,nrows): row_data = sh.row_values(i) row_list.append(row_data)2.寫入Excel(需安裝pyExcelerator)
from pyExcelerator import *w = Workbook() #創建一個工作簿ws = w.add_sheet('Hey, Hades') #創建一個工作表ws.write(0,0,'bit') #在1行1列寫入bitws.write(0,1,'huang') #在1行2列寫入huangws.write(1,0,'xuan') #在2行1列寫入xuanw.save('mini.xls') #保存3.再舉個自己寫的讀寫Excel的例子
讀取reflect.xls中的某些信息進行處理后寫入mini.xls文件中?!?br />
#-*- coding: utf8 -*-import xlrdfrom pyExcelerator import * w = Workbook() ws = w.add_sheet('Sheet1') fname = "reflect.xls"bk = xlrd.open_workbook(fname)shxrange = range(bk.nsheets)try: sh = bk.sheet_by_name("Sheet1")except: print "no sheet in %s named Sheet1" % fnamenrows = sh.nrowsncols = sh.ncolsprint "nrows %d, ncols %d" % (nrows,ncols) cell_value = sh.cell_value(1,1)#print cell_value row_list = []mydata = []for i in range(1,nrows): row_data = sh.row_values(i) pkgdatas = row_data[3].split(',') #pkgdatas.split(',') #獲取每個包的前兩個字段 for pkgdata in pkgdatas: pkgdata = '.'.join((pkgdata.split('.'))[:2]) mydata.append(pkgdata) #將列表排序 mydata = list(set(mydata)) print mydata #將列表轉化為字符串 mydata = ','.join(mydata) #寫入數據到每行的第一列 ws.write(i,0,mydata) mydata = [] row_list.append(row_data[3])#print row_listw.save('mini.xls')4.現在我需要根據Excel文件中滿足特定要求的apk的md5值來從服務器獲取相應的apk樣本,就需要這樣做:
#-*-coding:utf8-*-import xlrdimport osimport shutil fname = "./excelname.xls"bk = xlrd.open_workbook(fname)shxrange = range(bk.nsheets)try: #打開Sheet1工作表 sh = bk.sheet_by_name("Sheet1")except: print "no sheet in %s named Sheet1" % fname#獲取行數nrows = sh.nrows#獲取列數ncols = sh.ncols#print "nrows %d, ncols %d" % (nrows,ncols)#獲取第一行第一列數據cell_value = sh.cell_value(1,1)#print cell_value row_list = []#range(起始行,結束行)for i in range(1,nrows): row_data = sh.row_values(i) if row_data[6] == "HXB": filename = row_data[3]+".apk" #print "%s %s %s" %(i,row_data[3],filename) filepath = r"./1/"+filename print "%s %s %s" %(i,row_data[3],filepath) if os.path.exists(filepath): shutil.copy(filepath, r"./myapk/")補充一個使用xlwt3進行Excel文件的寫操作。
import xlwt3if __name__ == '__main__': datas = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h']]#二維數組 file_path = 'D://test.xlsx' wb = xlwt3.Workbook() sheet = wb.add_sheet('test')#sheet的名稱為test #單元格的格式 style = 'pattern: pattern solid, fore_colour yellow; '#背景顏色為黃色 style += 'font: bold on; '#粗體字 style += 'align: horz centre, vert center; '#居中 header_style = xlwt3.easyxf(style) row_count = len(datas) col_count = len(datas[0]) for row in range(0, row_count): col_count = len(datas[row]) for col in range(0, col_count): if row == 0:#設置表頭單元格的格式 sheet.write(row, col, datas[row][col], header_style) else: sheet.write(row, col, datas[row][col]) wb.save(file_path)輸出的文件內容如下圖:

注:以上代碼在Python 3.x版本測試通過。
好了,python操作Excel就這么!些了,簡單吧
新聞熱點
疑難解答
圖片精選