Overview
- The make_file() method of HtmlDiff class returns the differences between two lists of lines in HTML format. The two HTML tables highlights the differences including added, changed and deleted lines.
Example
# Example Python program that compares two lists of strings
# i.e, lines of text, and writes the differences to a
# table in Hypertext Markup Language (HTML) format
import difflib
# Create a HTMLDiff object
htmlDiffer = difflib.HtmlDiff()
# From lines as a list
fromLines = ["Water, water, every where,",
"And all the boards did shrink;"]
# To lines as a list
toLines = ["Water, water, every where,",
"Nor any drop to drink."]
# Compare fromLines vs to toLines
diffInHTML = htmlDiffer.make_file(fromLines, toLines,
fromdesc="From text", todesc = "To text")
print("The differences as HTML table:")
print(diffInHTML)
# Write HTML to a file
f = open("./diff.html", "w")
f.write(diffInHTML)Output - HTML visualized

Output - HTML text
<!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: Menlo, Consolas, Monaco, Liberation Mono, Lucida Console, monospace; 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>
<thead><tr><th class="diff_next"><br /></th><th colspan="2" class="diff_header">From text</th><th class="diff_next"><br /></th><th colspan="2" class="diff_header">To text</th></tr></thead>
<tbody>
<tr><td class="diff_next" id="difflib_chg_to0__0"><a href="#difflib_chg_to0__0">f</a></td><td class="diff_header" id="from0_1">1</td><td nowrap="nowrap">Water, water, every where,</td><td class="diff_next"><a href="#difflib_chg_to0__0">f</a></td><td class="diff_header" id="to0_1">1</td><td nowrap="nowrap">Water, water, every where,</td></tr>
<tr><td class="diff_next"><a href="#difflib_chg_to0__top">t</a></td><td class="diff_header" id="from0_2">2</td><td nowrap="nowrap"><span class="diff_sub">And all the boards did shrink;</span></td><td class="diff_next"><a href="#difflib_chg_to0__top">t</a></td><td class="diff_header" id="to0_2">2</td><td nowrap="nowrap"><span class="diff_add">Nor any drop to drink.</span></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>