A, xlrd modules and module xlwt what
xlrd python third party module kit, for reading data in excel;
xlwt third party module python toolkit for writing data to excel in;
Second, the installation modules and xlwt module xlrd
pip install xlrd pip install xlwt
Three, Excel spreadsheet is structured as follows:
Fourth, the use xlrd module reads excel files
# Read excel data DEF read_excel (Self, excel_path, sheet_name): XLS = xlrd.open_workbook (excel_path, formatting_info = True) # first open existing tables, formatting_info = True expressed reservations about the original form of the style sheet = xls.sheet_by_name (sheet_name) # obtained sheet by sheet the object name dataList = [] for rows in Range (. 1, sheet.nrows): # circulation line tempList = [] for cols in Range (0, sheet.ncols-2): # cycle the column, because the last two results are written so Save 2 IF cols = = 0: # determines if the number of rows in the first column directly. tempList.append (rows) the else : tempList.append(sheet.cell_value(rows,cols)) dataList.append(tempList) return dataList
read_excel Method Parameters:
excel_path parameters for the path excel file,
sheet_name name parameter sheet excel file.
Fifth, the use xlrt module to write data to excel file
# Write data to excel DEF write_excel (Self, excel_path, SHEET_NAME, rows, cols, value): # the current system time CURRENT_TIME The time.strftime = ( " % D%% Y-M-% H:% M:% S " , time.localtime ()) # open existing tables, formatting_info = True expressed reservations about the original table style Book = xlrd.open_workbook (excel_path, formatting_info = True) WB = Copy (Book) # copy Excel sheet wb.get_sheet = (SHEET_NAME) # obtained sheet objects through the sheet name IF value == ' Fail ' : sheet.write(rows,cols,value,style=xlwt.easyxf('pattern: pattern solid,fore_colour red;')) # 引用样式 elif value == 'ignore': sheet.write(rows,cols,value,style=xlwt.easyxf('pattern: pattern solid,fore_colour yellow;')) # 引用样式 else: sheet.write(rows,cols,value) # Set the column width and the value of the time sheet.col (-cols. 1) = 5000 .width sheet.write (rows, cols -1 , CURRENT_TIME) # save wb.save (excel_path)
read_excel Method Parameters:
excel_path parameters for the path excel file,
sheet_name name parameter sheet excel file.
rows parameter to write the contents of the first few lines
cols parameter indicates the content is written to the first few columns
value parameter indicates the written content
Six, the following code is executed:
if __name__ == '__main__': I ExcelUtil = () #print (eu.read_excel (get_project_path () + "data / testdata.xls", "inquiry train")) eu.write_excel (get_project_path () + "data / testdata.xls", "inquiry train", 1,6, "pass") eu.write_excel (get_project_path () + "data / testdata.xls", "inquiry train", 2,6, "ignore") eu.write_excel (get_project_path () + "data / testdata.xls", "inquiry train", 3,6, "fail")
Seven lone walk alone near Public Bank Zhiyuan!
If you think this article helpful to you, if you have any questions about this article, if you can join the group of software testing technology for software testing, interface testing, automated testing, interviewing interested in the exchange of experience: 695 458 161, the group issued free Stuff is the essence of the author of more than a decade to test career. Oh, there are peers together.