Loading

生成美观的对比HTML格式文档

采用HtmlDIff()类的make_file()方法就可以生存美观的HTML文档。示例:

对 https://www.cnblogs.com/hwlong/articles/9087658.html示例一simple1.py中的代码按以下进行修改

d = difflib.Differ()            #创建Differ()对象
diff = d.compare(text1_lines,text2_lines) #采用compare方法对字符串进行比较
print('\n'.join(list(diff)))

替换成:

d = difflib.HtmlDiff()            #创建HtmlDiffer()对象
print(d.make_file(text1_lines,text2_lines)) #采用make_file方法对字符串进行比较

#simple2.py代码(修改后的代码)

#!/usr/bin/env python

import difflib

text1 = """text1:   #定义字符串1
This module provides classes and functions for comparing sequences.
including HTML and context and unified diffs.
difflib document v7.4
add string"""

text1_lines = text1.splitlines()                #以行进行分割,以便进行对比
text2 = """text2:           #定义字符串2
This module provides classes and functions for comparing sequences.
including HTML and context and unified diffs.
difflib document v7.5
add string"""

text2_lines = text2.splitlines()
d = difflib.HtmlDiff()            #创建HtmlDiffer()对象
print(d.make_file(text1_lines,text2_lines))        #采用make_file方法对字符串进行比较

运行python3 simple2.py > diff.html,在使用浏览器打开diff.html文件,如下图所示,HTML文档包括了行号、差异标志、图例等信息,可读性增强了很多。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

<head>
    <meta http-equiv="Content-Type"
          content="text/html; charset=utf-8" />
    <title></title>
    <style type="text/css">
        table.diff {font-family:Courier; border:medium;}
        .diff_header {background-color:#e0e0e0}
        td.diff_header {text-align:right}
        .diff_next {background-color:#c0c0c0}
        .diff_add {background-color:#aaffaa}
        .diff_chg {background-color:#ffff77}
        .diff_sub {background-color:#ffaaaa}
    </style>
</head>

<body>

    <table class="diff" id="difflib_chg_to0__top"
           cellspacing="0" cellpadding="0" rules="groups" >
        <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup>
        <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup>

        <tbody>
            <tr><td class="diff_next" id="difflib_chg_to0__1"><a href="#difflib_chg_to0__1">n</a></td><td class="diff_header" id="from0_1">1</td><td nowrap="nowrap"><span class="diff_sub">text1:&nbsp;&nbsp;&nbsp;#定义字符串1</span></td><td class="diff_next"><a href="#difflib_chg_to0__1">n</a></td><td class="diff_header" id="to0_1">1</td><td nowrap="nowrap"><span class="diff_add">text2:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#定义字符串2</span></td></tr>
            <tr><td class="diff_next"></td><td class="diff_header" id="from0_2">2</td><td nowrap="nowrap">This&nbsp;module&nbsp;provides&nbsp;classes&nbsp;and&nbsp;functions&nbsp;for&nbsp;comparing&nbsp;sequences.</td><td class="diff_next"></td><td class="diff_header" id="to0_2">2</td><td nowrap="nowrap">This&nbsp;module&nbsp;provides&nbsp;classes&nbsp;and&nbsp;functions&nbsp;for&nbsp;comparing&nbsp;sequences.</td></tr>
            <tr><td class="diff_next"></td><td class="diff_header" id="from0_3">3</td><td nowrap="nowrap">including&nbsp;HTML&nbsp;and&nbsp;context&nbsp;and&nbsp;unified&nbsp;diffs.</td><td class="diff_next"></td><td class="diff_header" id="to0_3">3</td><td nowrap="nowrap">including&nbsp;HTML&nbsp;and&nbsp;context&nbsp;and&nbsp;unified&nbsp;diffs.</td></tr>
            <tr><td class="diff_next"><a href="#difflib_chg_to0__top">t</a></td><td class="diff_header" id="from0_4">4</td><td nowrap="nowrap">difflib&nbsp;document&nbsp;v7.<span class="diff_chg">4</span></td><td class="diff_next"><a href="#difflib_chg_to0__top">t</a></td><td class="diff_header" id="to0_4">4</td><td nowrap="nowrap">difflib&nbsp;document&nbsp;v7.<span class="diff_chg">5</span></td></tr>
            <tr><td class="diff_next"></td><td class="diff_header" id="from0_5">5</td><td nowrap="nowrap">add&nbsp;string</td><td class="diff_next"></td><td class="diff_header" id="to0_5">5</td><td nowrap="nowrap">add&nbsp;string</td></tr>
        </tbody>
    </table>
    <table class="diff" summary="Legends">
        <tr> <th colspan="2"> Legends </th> </tr>
        <tr> <td> <table border="" summary="Colors">
                      <tr><th> Colors </th> </tr>
                      <tr><td class="diff_add">&nbsp;Added&nbsp;</td></tr>
                      <tr><td class="diff_chg">Changed</td> </tr>
                      <tr><td class="diff_sub">Deleted</td> </tr>
                  </table></td>
             <td> <table border="" summary="Links">
                      <tr><th colspan="2"> Links </th> </tr>
                      <tr><td>(f)irst change</td> </tr>
                      <tr><td>(n)ext change</td> </tr>
                      <tr><td>(t)op</td> </tr>
                  </table></td> </tr>
    </table>
</body>

</html>
生成的diff.html代码

在浏览器中带开diff.html代码文件:

posted @ 2018-05-25 12:00  KubeSec  阅读(4448)  评论(0编辑  收藏  举报