Generating side-by-side differences in HTML

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


The difflib.py module and the diff.py 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
changed.

Example usages:

<DOS_CMD_LINE>
c:/python2x/Tools/Scripts/diff.py -m file1.txt file2.txt > some.html
</DOS_CMD_LINE>

<SOME_PYTHON_SCRIPT>
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')
f.write(html)
f.close()
</SOME_PYTHON_SCRIPT>

'tabsize' defaults to 8
'wrapcolumn' defaults to None (no line wrapping)
'context' defaults to False (shows full file vs. just showing diffs +
context)
'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:
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=914575&group_id=5470

Enjoy,
Dan Gass



More information about the Python-list mailing list