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