all Python 3difflibterm
difflib.HtmlDiff
diff formats
A class that builds a two-column HTML diff table: make_file() returns a full page, make_table() only the table.
Syntax
difflib.HtmlDiff(tabsize=8, wrapcolumn=None).make_file(a, b, fromdesc='', todesc='', context=False, numlines=5)
By default the whole file is shown; for long files pass context=True and numlines. Lines are not wrapped unless wrapcolumn is set, so wide code stretches the table. The result is a string that you write to a file with encoding='utf-8' (the page charset is utf-8 by default too).
Examples
import difflib
old = ['a = 1\n', 'b = 2\n']
new = ['a = 1\n', 'b = 3\n']
html = difflib.HtmlDiff(wrapcolumn=60).make_file(old, new, 'old.py', 'new.py', context=True)
with open('diff.html', 'w', encoding='utf-8') as f:
f.write(html)
print('<!DOCTYPE html' in html, 'old.py' in html) # → True True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy