接口自動化框架

ParseExcel類

#encoding=utf-8

import openpyxl

from openpyxl.styles import Border, Side, Font

import time

class ParseExcel(object):

? ? def __init__(self):

? ? ? ? self.workbook = None

? ? ? ? self.excelFile = None

? ? ? ? self.font = Font(color = None) # 設(shè)置字體的顏色

? ? ? ? # 顏色對應(yīng)的RGB值

? ? ? ? self.RGBDict = {'red': 'FFFF3030', 'green': 'FF008B00'}

? ? def loadWorkBook(self, excelPathAndName):

? ? ? ? # 將excel文件加載到內(nèi)存姻锁,并獲取其workbook對象

? ? ? ? try:

? ? ? ? ? ? self.workbook = openpyxl.load_workbook(excelPathAndName)

? ? ? ? except Exception, e:

? ? ? ? ? ? raise e

? ? ? ? self.excelFile = excelPathAndName

? ? ? ? return self.workbook

? ? def getSheetByName(self, sheetName):

? ? ? ? # 根據(jù)sheet名獲取該sheet對象

? ? ? ? try:

? ? ? ? ? ? # sheet = self.workbook.get_sheet_by_name(sheetName)

? ? ? ? ? ? sheet = self.workbook[sheetName]

? ? ? ? ? ? return sheet

? ? ? ? except Exception, e:

? ? ? ? ? ? raise e

? ? def getSheetByIndex(self, sheetIndex):

? ? ? ? # 根據(jù)sheet的索引號獲取該sheet對象

? ? ? ? try:

? ? ? ? ? ? # sheetname = self.workbook.get_sheet_names()[sheetIndex]

? ? ? ? ? ? sheetname = self.workbook.sheetnames[sheetIndex]

? ? ? ? except Exception, e:

? ? ? ? ? ? raise e

? ? ? ? # sheet = self.workbook.get_sheet_by_name(sheetname)

? ? ? ? sheet = self.workbook[sheetname]

? ? ? ? return sheet

? ? def getRowsNumber(self, sheet):

? ? ? ? # 獲取sheet中有數(shù)據(jù)區(qū)域的結(jié)束行號

? ? ? ? return sheet.max_row

? ? def getColsNumber(self, sheet):

? ? ? ? # 獲取sheet中有數(shù)據(jù)區(qū)域的結(jié)束列號

? ? ? ? return sheet.max_column

? ? def getStartRowNumber(self, sheet):

? ? ? ? # 獲取sheet中有數(shù)據(jù)區(qū)域的開始的行號

? ? ? ? return sheet.min_row

? ? def getStartColNumber(self, sheet):

? ? ? ? # 獲取sheet中有數(shù)據(jù)區(qū)域的開始的列號

? ? ? ? return sheet.min_column

? ? def getRow(self, sheet, rowNo):

? ? ? ? # 獲取sheet中某一行,返回的是這一行所有的數(shù)據(jù)內(nèi)容組成的tuple享幽,

? ? ? ? # 下標(biāo)從1開始,sheet.rows[1]表示第一行

? ? ? ? try:

? ? ? ? ? ? rows = []

? ? ? ? ? ? for row in sheet.iter_rows():

? ? ? ? ? ? ? ? rows.append(row)

? ? ? ? ? ? return rows[rowNo - 1]

? ? ? ? except Exception, e:

? ? ? ? ? ? raise e

? ? def getColumn(self, sheet, colNo):

? ? ? ? # 獲取sheet中某一列,返回的是這一列所有的數(shù)據(jù)內(nèi)容組成tuple窍箍,

? ? ? ? # 下標(biāo)從1開始,sheet.columns[1]表示第一列

? ? ? ? try:

? ? ? ? ? ? cols = []

? ? ? ? ? ? for col in sheet.iter_cols():

? ? ? ? ? ? ? ? cols.append(col)

? ? ? ? ? ? return cols[colNo - 1]

? ? ? ? except Exception, e:

? ? ? ? ? ? raise e

? ? def getCellOfValue(self, sheet, coordinate = None,

? ? ? ? ? ? ? ? ? ? ? rowNo = None, colsNo = None):

? ? ? ? # 根據(jù)單元格所在的位置索引獲取該單元格中的值,下標(biāo)從1開始,

? ? ? ? # sheet.cell(row = 1, column = 1).value丽旅,

? ? ? ? # 表示excel中第一行第一列的值

? ? ? ? if coordinate != None:

? ? ? ? ? ? try:

? ? ? ? ? ? ? ? return sheet[coordinate]

? ? ? ? ? ? except Exception, e:

? ? ? ? ? ? ? ? raise e

? ? ? ? elif coordinate is None and rowNo is not None and \

? ? ? ? ? ? ? ? ? ? ? ? colsNo is not None:

? ? ? ? ? ? try:

? ? ? ? ? ? ? ? return sheet.cell(row = rowNo, column = colsNo).value

? ? ? ? ? ? except Exception, e:

? ? ? ? ? ? ? ? raise e

? ? ? ? else:

? ? ? ? ? ? raise Exception("Insufficient Coordinates of cell !")

? ? def getCellOfObject(self, sheet, coordinate = None,

? ? ? ? ? ? ? ? ? ? ? ? rowNo = None, colsNo = None):

? ? ? ? # 獲取某個單元格的對象椰棘,可以根據(jù)單元格所在位置的數(shù)字索引,

? ? ? ? # 也可以直接根據(jù)excel中單元格的編碼及坐標(biāo)

? ? ? ? # 如getCellObject(sheet, coordinate = 'A1') or

? ? ? ? # getCellObject(sheet, rowNo = 1, colsNo = 2)

? ? ? ? if coordinate != None:

? ? ? ? ? ? try:

? ? ? ? ? ? ? ? # return sheet.cell(coordinate = coordinate)

? ? ? ? ? ? ? ? return sheet[coordinate]

? ? ? ? ? ? except Exception, e:

? ? ? ? ? ? ? ? raise e

? ? ? ? elif coordinate == None and rowNo is not None and \

? ? ? ? ? ? ? ? ? ? ? ? colsNo is not None:

? ? ? ? ? ? try:

? ? ? ? ? ? ? ? return sheet.cell(row = rowNo,column = colsNo)

? ? ? ? ? ? except Exception, e:

? ? ? ? ? ? ? ? raise e

? ? ? ? else:

? ? ? ? ? ? raise Exception("Insufficient Coordinates of cell !")

? ? def writeCell(self, sheet, content, coordinate = None,

? ? ? ? rowNo = None, colsNo = None, style = None):

? ? ? ? #根據(jù)單元格在excel中的編碼坐標(biāo)或者數(shù)字索引坐標(biāo)向單元格中寫入數(shù)據(jù)榄笙,

? ? ? ? # 下標(biāo)從1開始邪狞,參style表示字體的顏色的名字,比如red,green

? ? ? ? if coordinate is not None:

? ? ? ? ? ? try:

? ? ? ? ? ? ? ? # sheet.cell(coordinate = coordinate).value = content

? ? ? ? ? ? ? ? sheet[coordinate] = content

? ? ? ? ? ? ? ? if style is not None:

? ? ? ? ? ? ? ? ? ? sheet[coordinate].\

? ? ? ? ? ? ? ? ? ? ? ? font = Font(color = self.RGBDict[style])

? ? ? ? ? ? ? ? self.workbook.save(self.excelFile)

? ? ? ? ? ? except Exception, e:

? ? ? ? ? ? ? ? raise e

? ? ? ? elif coordinate == None and rowNo is not None and \

? ? ? ? ? ? ? ? ? ? ? ? colsNo is not None:

? ? ? ? ? ? try:

? ? ? ? ? ? ? ? sheet.cell(row = rowNo,column = colsNo).value = content

? ? ? ? ? ? ? ? if style:

? ? ? ? ? ? ? ? ? ? sheet.cell(row = rowNo,column = colsNo).\

? ? ? ? ? ? ? ? ? ? ? ? font = Font(color = self.RGBDict[style])

? ? ? ? ? ? ? ? self.workbook.save(self.excelFile)

? ? ? ? ? ? except Exception, e:

? ? ? ? ? ? ? ? raise e

? ? ? ? else:

? ? ? ? ? ? raise Exception("Insufficient Coordinates of cell !")

? ? def writeCellCurrentTime(self, sheet, coordinate = None,

? ? ? ? ? ? ? ? rowNo = None, colsNo = None):

? ? ? ? # 寫入當(dāng)前的時間茅撞,下標(biāo)從1開始

? ? ? ? now = int(time.time())? #顯示為時間戳

? ? ? ? timeArray = time.localtime(now)

? ? ? ? currentTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)

? ? ? ? if coordinate is not None:

? ? ? ? ? ? try:

? ? ? ? ? ? ? ? sheet.cell(coordinate = coordinate).value = currentTime

? ? ? ? ? ? ? ? self.workbook.save(self.excelFile)

? ? ? ? ? ? except Exception, e:

? ? ? ? ? ? ? ? raise e

? ? ? ? elif coordinate == None and rowNo is not None \

? ? ? ? ? ? ? ? and colsNo is not None:

? ? ? ? ? ? try:

? ? ? ? ? ? ? ? sheet.cell(row = rowNo, column = colsNo

? ? ? ? ? ? ? ? ? ? ? ? ).value = currentTime

? ? ? ? ? ? ? ? self.workbook.save(self.excelFile)

? ? ? ? ? ? except Exception, e:

? ? ? ? ? ? ? ? raise e

? ? ? ? else:

? ? ? ? ? ? raise Exception("Insufficient Coordinates of cell !")

if __name__ == '__main__':

? ? # 測試代碼

? ? pe = ParseExcel()

? ? pe.loadWorkBook(r'D:\ProgramSourceCode\Python Source Code\WorkSpace\InterfaceFrame2018\inter_test_data.xlsx')

? ? sheetObj = pe.getSheetByName(u"API")

? ? print "通過名稱獲取sheet對象的名字:", sheetObj.title

? ? # print help(sheetObj.rows)

? ? print "通過index序號獲取sheet對象的名字:", \

? ? ? ? pe.getSheetByIndex(0).title

? ? sheet = pe.getSheetByIndex(0)

? ? print type(sheet)

? ? print pe.getRowsNumber(sheet)? #獲取最大行號

? ? print pe.getColsNumber(sheet)? #獲取最大列號

? ? rows = pe.getRow(sheet, 1)? #獲取第一行

? ? for i in rows:

? ? ? ? print i.value

? ? # # 獲取第一行第一列單元格內(nèi)容

? ? # print pe.getCellOfValue(sheet, rowNo = 1, colsNo = 1)

? ? # pe.writeCell(sheet, u'我愛祖國', rowNo = 10, colsNo = 10)

? ? # pe.writeCellCurrentTime(sheet, rowNo = 10, colsNo = 11)

data_store類

#encoding=utf-8

from config.public_data import REQUEST_DATA,RESPONSE_DATA

class RelyDataStore(object):

? ? def __init__(self):

? ? ? ? pass

? ? @classmethod

? ? def do(cls, storePoint, apiName, caseId, request_source={}, response_source={}):

? ? ? ? # print apiName, request_source, response_source

? ? ? ? for key, value in storePoint.items():

? ? ? ? ? ? if key == "request":

? ? ? ? ? ? ? ? # 說明存儲的數(shù)據(jù)來自請求參數(shù)

? ? ? ? ? ? ? ? for i in value:

? ? ? ? ? ? ? ? ? ? if request_source.has_key(i):

? ? ? ? ? ? ? ? ? ? ? ? if not REQUEST_DATA.has_key(apiName):

? ? ? ? ? ? ? ? ? ? ? ? ? ? # 說明存儲數(shù)據(jù)的結(jié)構(gòu)還未生成帆卓,需要指明數(shù)據(jù)存儲結(jié)構(gòu)

? ? ? ? ? ? ? ? ? ? ? ? ? ? REQUEST_DATA[apiName] = {str(caseId):{i:request_source[i]}}

? ? ? ? ? ? ? ? ? ? ? ? else:

? ? ? ? ? ? ? ? ? ? ? ? ? ? # 說明存儲數(shù)據(jù)結(jié)構(gòu)中最外層結(jié)構(gòu)完整

? ? ? ? ? ? ? ? ? ? ? ? ? ? if REQUEST_DATA[apiName].has_key(str(caseId)):

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? REQUEST_DATA[apiName][str(caseId)][i] = request_source[i]

? ? ? ? ? ? ? ? ? ? ? ? ? ? else:

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? REQUEST_DATA[apiName][str(caseId)] = {i:request_source[i]}

? ? ? ? ? ? ? ? ? ? else:

? ? ? ? ? ? ? ? ? ? ? ? print "請求參數(shù)中不存在字段" + i

? ? ? ? ? ? elif key == "response":

? ? ? ? ? ? ? ? # 說明存儲的數(shù)據(jù)來自響應(yīng)body

? ? ? ? ? ? ? ? for j in value:

? ? ? ? ? ? ? ? ? ? if response_source.has_key(j):

? ? ? ? ? ? ? ? ? ? ? ? if not RESPONSE_DATA.has_key(apiName):

? ? ? ? ? ? ? ? ? ? ? ? ? ? # 說明存儲數(shù)據(jù)的結(jié)構(gòu)還未生成,需要指明數(shù)據(jù)存儲結(jié)構(gòu)

? ? ? ? ? ? ? ? ? ? ? ? ? ? RESPONSE_DATA[apiName] = {str(caseId):{j:response_source[j]}}

? ? ? ? ? ? ? ? ? ? ? ? else:

? ? ? ? ? ? ? ? ? ? ? ? ? ? # 說明存儲數(shù)據(jù)結(jié)構(gòu)中最外層結(jié)構(gòu)完整

? ? ? ? ? ? ? ? ? ? ? ? ? ? if RESPONSE_DATA[apiName].has_key(str(caseId)):

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? RESPONSE_DATA[apiName][str(caseId)][j] = response_source[j]

? ? ? ? ? ? ? ? ? ? ? ? ? ? else:

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? RESPONSE_DATA[apiName][str(caseId)] = {j:response_source[j]}

? ? ? ? ? ? ? ? ? ? else:

? ? ? ? ? ? ? ? ? ? ? ? print "響應(yīng)body中不存在字段" + j

? ? ? ? print 'REQUEST_DATA:',REQUEST_DATA

? ? ? ? print 'RESPONSE_DATA:',RESPONSE_DATA

if __name__ == '__main__':

? ? r = {"username":"srwcx01","password":"wcx123wac1","email":"wcx@qq.com"}

? ? s = {"request":["username","password"],"response":["userid"]}

? ? res = {"userid":12,"code":"00"}

? ? RelyDataStore.do( s, "register", 1,r, res)

? ? print REQUEST_DATA

? ? print RESPONSE_DATA

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末米丘,一起剝皮案震驚了整個濱河市剑令,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌拄查,老刑警劉巖吁津,帶你破解...
    沈念sama閱讀 211,123評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異堕扶,居然都是意外死亡碍脏,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,031評論 2 384
  • 文/潘曉璐 我一進店門稍算,熙熙樓的掌柜王于貴愁眉苦臉地迎上來典尾,“玉大人,你說我怎么就攤上這事邪蛔〖崩瑁” “怎么了?”我有些...
    開封第一講書人閱讀 156,723評論 0 345
  • 文/不壞的土叔 我叫張陵侧到,是天一觀的道長勃教。 經(jīng)常有香客問我,道長匠抗,這世上最難降的妖魔是什么故源? 我笑而不...
    開封第一講書人閱讀 56,357評論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮汞贸,結(jié)果婚禮上绳军,老公的妹妹穿的比我還像新娘印机。我一直安慰自己,他們只是感情好门驾,可當(dāng)我...
    茶點故事閱讀 65,412評論 5 384
  • 文/花漫 我一把揭開白布射赛。 她就那樣靜靜地躺著,像睡著了一般奶是。 火紅的嫁衣襯著肌膚如雪楣责。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,760評論 1 289
  • 那天聂沙,我揣著相機與錄音秆麸,去河邊找鬼。 笑死及汉,一個胖子當(dāng)著我的面吹牛沮趣,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播坷随,決...
    沈念sama閱讀 38,904評論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼房铭,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了甸箱?” 一聲冷哼從身側(cè)響起育叁,我...
    開封第一講書人閱讀 37,672評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎芍殖,沒想到半個月后豪嗽,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,118評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡豌骏,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,456評論 2 325
  • 正文 我和宋清朗相戀三年龟梦,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片窃躲。...
    茶點故事閱讀 38,599評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡计贰,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出蒂窒,到底是詐尸還是另有隱情躁倒,我是刑警寧澤,帶...
    沈念sama閱讀 34,264評論 4 328
  • 正文 年R本政府宣布洒琢,位于F島的核電站秧秉,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏衰抑。R本人自食惡果不足惜象迎,卻給世界環(huán)境...
    茶點故事閱讀 39,857評論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望呛踊。 院中可真熱鬧砾淌,春花似錦啦撮、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,731評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至骄崩,卻和暖如春聘鳞,著一層夾襖步出監(jiān)牢的瞬間薄辅,已是汗流浹背要拂。 一陣腳步聲響...
    開封第一講書人閱讀 31,956評論 1 264
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留站楚,地道東北人脱惰。 一個月前我還...
    沈念sama閱讀 46,286評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像窿春,于是被迫代替她去往敵國和親拉一。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,465評論 2 348

推薦閱讀更多精彩內(nèi)容