Ext.: https://www.jianshu.com/p/6c5d328bf390
#-*-Coding:utf-8-*-import xlsxwriterdef Get_format (wd, option={}): Return wd.add_format (option) # Set Center Def Get_format _center (wd,num=1): Return Wd.add_format ({' Align ': ' Center ', ' valign ': ' vcenter ', ' Border ': num}) def set_border_ (WD, num =1): Return Wd.add_format ({}). Set_border (num) # Write Data def _write_center (worksheet, cl, data, WD): Return Worksheet.write (CL, data, Get_format_center (wd)) Workbook = Xlsxwriter. Workbook (' report.xlsx ') worksheet = Workbook.add_worksheet ("Test total condition") Worksheet2 = Workbook.add_worksheet ("Test Details") def init (worksheet): # Set the column row's width height worksheet.set_column ("a:a", "b:b", Worksheet.set_colum) Worksheet.set_column N ("C:c", Worksheet.set_column ("D:d") worksheet.set_column ("E:e") worksheet.set_column ("f:f", +) W Orksheet.set_row (1, Worksheet.set_row) Worksheet.set_row (2, 3) Worksheet.set_row (4,) Worksheet.s Et_row (5, +) # Worksheet.set_row (0, $) define_format_h1 = Get_format (workbook, {' bold ': True, 'Font_size ':) define_format_h2 = Get_format (workbook, {' bold ': True, ' font_size ': +}) Define_format_h1.set_borde R (1) define_format_h2.set_border (1) define_format_h1.set_align ("center") define_format_h2.set_align ("center") Define_format_h2.set_bg_color ("Blue") Define_format_h2.set_color ("#ffffff") # Create a new Chart object. Worksheet.merge_range (' a1:f1 ', ' Test Report overview ', DEFINE_FORMAT_H1) worksheet.merge_range (' a2:f2 ', ' Test summary ', DEFINE_FORMAT_H2 ) Worksheet.merge_range (' A3:a6 ', ' put pictures here ', Get_format_center (Workbook)) _write_center (worksheet, "B3", ' Project name ', WORKBO OK) _write_center (worksheet, "B4", ' interface version ', workbook) _write_center (worksheet, "B5", ' scripting language ', workbook) _write_cent ER (worksheet, "B6", ' Test Network ', workbook) data = {"Test_name": "IQ", "test_version": "v2.0.8", "TEST_PL": "Android", "Test_ NET ":" WiFi "} _write_center (worksheet," C3 ", data[' Test_name '], workbook) _write_center (worksheet," C4 ", data[' Test_ Version '], workbook) _write_centeR (Worksheet, "C5", data[' Test_pl '], workbook) _write_center (worksheet, "C6", data[' test_net '], workbook) _write_cent ER (worksheet, "D3", "total number of interfaces", Workbook) _write_center (worksheet, "D4", "Total through", workbook) _write_center (worksheet, "D5" , "Total failures", workbook) _write_center (worksheet, "D6", "Test Date", workbook) data1 = {"Test_sum": +, "test_success": 80, " Test_failed ":", "test_date": "2018-10-10 12:10"} _write_center (worksheet, "E3", data1[' Test_sum '], workbook) _writ E_center (worksheet, "E4", data1[' test_success '], workbook) _write_center (worksheet, "E5", data1[' test_failed '), Workbook) _write_center (worksheet, "E6", data1[' Test_date '], workbook) _write_center (worksheet, "F3", "Score", workbook ) Worksheet.merge_range (' F4:f6 ', ' Max ', Get_format_center (Workbook)) Pie (workbook, worksheet) # Generate pie chart def pie (workbook , worksheet): Chart1 = Workbook.add_chart ({' type ': ' Pie '}) chart1.add_series ({' Name ': ' Interface Test statistics ', ' Catego Ries ': ' = Test total condition! $D $4: $D ', ' ValUEs ': ' = Test total condition! $E $4: $E $ ',}) chart1.set_title ({' Name ': ' Interface Test Statistics '}) Chart1.set_style (Worksheet.insert_char) T (' A9 ', Chart1, {' X_offset ': +, ' Y_offset ': ten}) def test_detail (worksheet): # Set the width height of column rows worksheet.set_column ("A:a", 3 0) Worksheet.set_column ("B:b") worksheet.set_column ("C:c") worksheet.set_column ("D:d") Worksheet.s Et_column ("E:e", "Worksheet.set_column") worksheet.set_column ("F:f", "G:g") worksheet.set_column ("H:H", Worksheet.set_row (1, 2) Worksheet.set_row (3, Worksheet.set_row) Worksheet.set_row (4,) wor Ksheet.set_row (5, 6) Worksheet.set_row (7, Worksheet.set_row) worksheet.merge_range (' a1:h1 ', ' Test Details ', ge T_format (workbook, {' bold ': True, ' font_size ':, ' align ': ' Center ', ' valign ': ' vcenter ', ' bg_color ': ' Blue ', ' Font_ ' Color ': ' #ffffff '})) _write_center (worksheet, "A2", ' Use case ID ', workbook) _write_center (worksheet, "B2", ' Interface name ', Workboo k) _write_center (worksheet,"C2", ' Interface protocol ', workbook) _write_center (worksheet, "D2", ' URL ', Workbook) _write_center (worksheet, "E2", ' Parameters ', Workboo k) _write_center (worksheet, "F2", ' expected value ', workbook) _write_center (worksheet, "G2", ' actual value ', Workbook) _write_center ( Worksheet, "H2", ' Test result ', workbook) data = {"Info": [{"t_id": "1001", "T_name": "Login", "T_method": "Post", "T_url": "http ://xxx?login "," T_param ":" {user_name:test,pwd:111111} "," T_hope ":" {code:1,msg: Login successful} "," T_actual ":" {code:0,msg: Password error} "," T_result ":" Failed "}, {" t_id ":" 1002 "," T_name ":" Product List "," T_method ":" Get "," T_url ":" Http://XXX? " Getfoodlist "," T_param ":" {} "," T_hope ":" {code:1,msg: Success, INFO:[{NAME:123,DETAL:DFADFA,IMG:PRODUCT/1.P Ng},{name:456,detal:dfadfa,img:product/1.png}]} "," T_actual ":" {code:1,msg: successful, info:[{name:123,detal:dfadfa,img: Product/1.png},{name:456,detal:dfadfa,img:product/1.png}]} "," T_result ":" Success "}]," test_sum ": +," test_success " : "test_failed": + temp = 4 for Item in data["Info"]: _write_center (worksheet, "A" +str (temp), item["t_id"], Workbook) _write_center (workshe ET, "B" +str (temp), item["T_name"], Workbook) _write_center (worksheet, "C" +str (temp), item["T_method"], Workbook) _write_center (worksheet, "D" +str (temp), item["T_url"], Workbook) _write_center (worksheet, "E" +str (temp), item[ "T_param"], Workbook) _write_center (worksheet, "F" +str (temp), item["T_hope"], Workbook) _write_center (Worksh "G" +str (temp), item["T_actual"], Workbook) _write_center (worksheet, "H" +str (temp), item["T_result"], Workbook) temp = temp-1init (worksheet) Test_detail (Worksheet2) workbook.close ()
Python Automated test report (Excel)