Generating side-by-side differences in HTML

Dan Gass dan.gass at
Fri Sep 3 15:22:40 CEST 2004

The module and the tools script in Python 2.4 alpha
3 now support generating side by side (with intra line differences) in
HTML format.  I have found this useful for performing build release
comparisons (I use a script to generate a main page showing all the
files that were changed in a build with hyperlinks to side by side
differences for each of those files).  I also find it useful for
generating HTML test reports to show differences between actual and
expected log files.  In addition I use it for supplementing Doxygen
generated HTML with side by side differences of files that were

Example usages:

c:/python2x/Tools/Scripts/ -m file1.txt file2.txt > some.html

import difflib
file1 = 'file1.txt'
file2 = 'file2.txt'
lines1 = open(file1).readlines()
lines2 = open(file2).readlines()
differ = difflib.HtmlDiff(tabsize=4,wrapcolumn=80)
html =  differ.make_file(lines1,lines2,file1,file2,context=True,numlines=10)
f = open('some.html','w')

'tabsize' defaults to 8
'wrapcolumn' defaults to None (no line wrapping)
'context' defaults to False (shows full file vs. just showing diffs +
'numlines' defaults to 5

You can try out the code either by installing Python 2.4 alpha 3 or
you can obtain the patched files (and example output) from:

Dan Gass

More information about the Python-list mailing list