拆分html表并在python中有单个比较列

时间:2017-10-30 10:47:37

标签: python

我将规划文档与JSON报告进行比较,并在HTML中生成比较报告,但格式对齐未正确显示, 这是当前的输出

                VLANs           
Attribute   SPD Value   DT Value    Result
VLANID      10          10          PASS
Name        lan1       lan1         PASS
VLANID      20         20           PASS
Name        lan2       lan2         PASS
VLANID      30         30           PASS
Name        lan3       lan3         PASS
VLANID      40         40           PASS

我期待HTML报告中的以下输出表

    SPD           DT Value      Compare
ID  Description ID  Description Results
10  lan1        10  lan1         Pass
20  lan2        20  lan2         Pass
30  lan3        30  lan3         Pass
40  lan4        40  lan4         Pass
41  lan5        41  lan5         Pass
300 lan6       300  lan6         Pass
309 lan7       310  lan7         Fail

以下是使用的代码段

import os
import io #io.StringIO or io.BytesIO
import logger

class HtmlGenerator:

    def __init__(self,comobj):
        #print ("Inside HTML Generator init.........")
        self.scalarlist = comobj.scalarlist
        self.allitems = comobj.allitems
        self.vlanlist = comobj.vlanlist
        self.cfglist = comobj.cfglist
        self.intlist = comobj.intlist
        self.spdfile = comobj.spdfile
        self.sheetname = comobj.sheetname
        #print ("scalarlist : ",self.scalarlist)
        #print("allitems : ",self.allitems)

    def htmlbuilder(self):
        file_str = io.StringIO()
        file_str.write("<html><head><style type='text/css'> table th {background-color:LightBlue}  #pass {background-color:green} #fail {background-color:red} #warning {background-color:yellow} #ignored {background-color:violet} </style></head>")
        file_str.write("<body  style='font-family: calibri;font-size:90%'' >")
        file_str.write("<h1 style='text-align:center'>SPD["+str(self.spdfile)+"],Sheet Name["+ self.sheetname +"]and Data Transformed Report Comparison </h1>")
        file_str.write("<hr>")
        file_str.write("<h2 id='top' '>Overall Summary </h2>")
        file_str.write("<hr>")
        file_str.write("<p>Total = " + str(self.allitems['attrcnt']) + "</p>")
        file_str.write("<p>Executed= " + str(self.allitems['execnt']) + "</p>")
        file_str.write("<p>Passed= " + str(self.allitems['passcnt']) + "</p>")
        file_str.write("<p>Failed= " + str(self.allitems['failcnt']) + "</p>")
        file_str.write("<p>Logpath= " + str("C:\\Users\\608944\\Automation\\Logs") + "</p>")
        file_str.write("<hr>")
        file_str.write("<h2>Switch Properties Summary</h2>")
        file_str.write("<table width='100%' border=1><tr><th  colspan='5'>Switch Properties</th></tr><tr><th>Attribute </th><th>SPD Value</th><th>Data Transformed Value</th><th>Result</th></tr>")
        for list in self.scalarlist:
            file_str.write("<tr>")
            for key in list.keys():
                #print (list[key])
                file_str.write("<td>")
                if list[key] == 'PASS':
                    file_str.write("<p style='align:center;background-color:green;font-weight:bold'>"+ str(list[key])+"</p>")
                elif list[key] == 'FAIL':
                    file_str.write("<p style='align:center;background-color:red;font-weight:bold'>"+str(list[key])+"</p>")
                elif list[key] == 'NOT FOUND':
                    file_str.write("<p style='align:center;background-color:orange;font-weight:bold'>"+str(list[key])+"</p>")
                else:
                    file_str.write("<p style='align:center'>"+str(list[key])+"</p>")
                file_str.write("</td>")
            file_str.write("</tr>")
        file_str.write("</table>")
        file_str.write("<br></br>")
        file_str.write("<hr>")
        file_str.write("<h2>Tabular Summary</h2>")
        file_str.write("<table width='100%' border=1><tr><th  colspan='5'>VLANs </th></tr><tr><th>Attribute</th><th>SPD Value</th><th>Data Transformed Value</th><th>Result</th></tr>")
        for list in self.vlanlist:
            file_str.write("<tr>")
            for key in list.keys():
                # print (list[key])
                file_str.write("<td>")
                if list[key] == 'PASS':
                    file_str.write("<p style='background-color:green;font-weight:bold'>" + str(list[key]) + "</p>")
                elif list[key] == 'FAIL':
                    file_str.write("<p style='background-color:red;font-weight:bold'>" + str(list[key]) + "</p>")
                elif list[key] == 'NOT FOUND':
                    file_str.write("<p style='background-color:orange;font-weight:bold'>" + str(list[key]) + "</p>")
                else:
                    file_str.write(str(list[key]))
                file_str.write("</td>")
            file_str.write("</tr>")
        file_str.write("</table>")
        file_str.write("<br></br>")

        file_str.write("<hr>")
        #file_str.write("<h2>Tabular Attributes Summary</h2>")
        file_str.write("<table width='100%' border=1><tr><th  colspan='5'>Stack Unit Info</th></tr><tr><th>Attribute</th><th>SPD Value</th><th>Data Transformed Value</th><th>Result</th></tr>")
        for list in self.cfglist:
            file_str.write("<tr>")
            for key in list.keys():
                # print (list[key])
                file_str.write("<td>")
                if list[key] == 'PASS':
                    file_str.write("<p style='background-color:green;font-weight:bold'>" + str(list[key]) + "</p>")
                elif list[key] == 'FAIL':
                    file_str.write("<p style='background-color:red;font-weight:bold'>" + str(list[key]) + "</p>")
                elif list[key] == 'NOT FOUND':
                    file_str.write("<p style='background-color:orange;font-weight:bold'>" + str(list[key]) + "</p>")
                else:
                    file_str.write(str(list[key]))
                file_str.write("</td>")
            file_str.write("</tr>")
        file_str.write("</table>")
        file_str.write("<br></br>")

        file_str.write("<hr>")
        file_str.write("<table width='100%' border=1><tr><th  colspan='5'>Interface</th></tr><tr><th>Attribute</th><th>SPD Value</th><th>Data Trasformed Value</th><th>Result</th></tr>")
        for list in self.intlist:
            file_str.write("<tr>")
            for key in list.keys():
                # print (list[key])
                file_str.write("<td>")
                if list[key] == 'PASS':
                    file_str.write("<p style='background-color:green;font-weight:bold'>" + str(list[key]) + "</p>")
                elif list[key] == 'FAIL':
                    file_str.write("<p style='background-color:red;font-weight:bold'>" + str(list[key]) + "</p>")
                elif list[key] == 'NOT FOUND':
                    file_str.write("<p style='background-color:orange;font-weight:bold'>" + str(list[key]) + "</p>")
                else:
                    file_str.write(str(list[key]))
                file_str.write("</td>")
            file_str.write("</tr>")
        file_str.write("</table>")
        file_str.write("<br></br>")

        file_str.write("</body></html>")
        #print(file_str.getvalue())
        return(file_str.getvalue())

    def htmlgenerator(self,loggerloadprofile,reportpath):
        #print (os.getcwd())
        #os.chdir("C:\\Users\\608944\\.PyCharmCE2017.1\\Automation\\Reports")
        os.chdir(reportpath)
        htmlfilename = self.spdfile+"_"+self.sheetname+"_"+'spdjson_compare_output.html'
        htmlfilepath = os.path.join(os.getcwd(), htmlfilename)
        loggerloadprofile.info("HTML file path in framework: %s",htmlfilepath)
        with open(htmlfilepath, 'w') as j:
            k = self.htmlbuilder()
            j.write(k)
            #print(k)

0 个答案:

没有答案