采用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: #定義字符串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: #定義字符串2</span></td></tr> <tr><td class="diff_next"></td><td class="diff_header" id="from0_2">2</td><td nowrap="nowrap">This module provides classes and functions for comparing sequences.</td><td class="diff_next"></td><td class="diff_header" id="to0_2">2</td><td nowrap="nowrap">This module provides classes and functions for comparing sequences.</td></tr> <tr><td class="diff_next"></td><td class="diff_header" id="from0_3">3</td><td nowrap="nowrap">including HTML and context and unified diffs.</td><td class="diff_next"></td><td class="diff_header" id="to0_3">3</td><td nowrap="nowrap">including HTML and context and unified 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 document 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 document 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 string</td><td class="diff_next"></td><td class="diff_header" id="to0_5">5</td><td nowrap="nowrap">add 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"> Added </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代碼文件: