Python: How to format HTML from HtmlDiff in difflib when text is too long.
If you get too long lines in HTML then you can use wrapcolumn= in HtmlDiff to split lines in many rows.
import difflib import webbrowser d = difflib.HtmlDiff()#wrapcolumn=10) lines1 = ['12345678901234567890123456789012345'] lines2 = ['AbcdefghijAbcdefghijAbcdefghijAbcdefghij'] html = d.make_file(lines1, lines2) # save in file with open('output.html', 'w') as fh: fh.write(html) # open in web browser webbrowser.open('output.html')
Without wrapcolumn

With wrapcolumn=10

In source code of diff you can see hidden variables _file_template, _table_template, _styles
which you can see also with
import difflib d = difflib.HtmlDiff() print(d._file_template) print(d._table_template) print(d._styles)
_file_template:
<!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=%(charset)s" />
<title></title>
<style type="text/css">%(styles)s
</style>
</head>
<body>
%(table)s%(legend)s
</body>
</html>
_table_template:
<table class="diff" id="difflib_chg_%(prefix)s_top"
cellspacing="0" cellpadding="0" rules="groups" >
<colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup>
<colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup>
%(header_row)s
<tbody> %(data_rows)s </tbody>
</table>
_styles:
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}
You can replace them in HtmlDiff to generate different HTML.
d = HtmlDiff()
d._file_template = """new file template with %(charset)s %(styles)s %(table)s %(legend)s"""
d._table_template = """new table template with %(prefix)s """
d._styles = """new styles"""
# ... code ...
Example which add classes to existing styles:
import difflib import webbrowser d = difflib.HtmlDiff() d._styles = d._styles + """ table.diff { margin: 30px; padding:10px; background-color: gold; box-shadow: 5px 5px 5px #aaa; } td { background-color: white; } """ lines1 = ['12345678901234567890123456789012345'] lines2 = ['AbcdefghijAbcdefghijAbcdefghijAbcdefghij'] html = d.make_file(lines1, lines2) # save in file with open('output.html', 'w') as fh: fh.write(html) # open in web browser webbrowser.open('output.html')

But sometimes this may not be enough. Some code is generated in hidden function. For example _format_line() generate line in table and it adds nowrap="nowrap" which can make problem to use responsible size (which will be change table width on smaller screens)
In next example I use HtmlDiff to create own class with new _format_line() without nowrap="nowrap" and now I can add styles with width and word-break
import difflib import webbrowser # --- classes --- class MyHTML(difflib.HtmlDiff): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # append new styles inside new class self._styles = self._styles + """ table.diff {width: 300px} .diff_sub {display: inline-block; word-break: break-word;} .diff_add {display: inline-block; word-break: break-word;} """ # function from source code - I removed only `nowrap="nowrap"` def _format_line(self,side,flag,linenum,text): try: linenum = '%d' % linenum id = ' id="%s%s"' % (self._prefix[side],linenum) except TypeError: # handle blank lines where linenum is '>' or '' id = '' # replace those things that would get confused with HTML symbols text = text.replace("&","&").replace(">",">").replace("<","<") # make space non-breakable so they don't get compressed or line wrapped text = text.replace(' ',' ').rstrip() # vvv ---- removed `nowrap="nowrap"` --- vvv return '<td class="diff_header"%s>%s</td><td>%s</td>' \ % (id,linenum,text) # --- main --- d = MyHTML() lines1 = ['12345678901234567890123456789012345'] lines2 = ['AbcdefghijAbcdefghijAbcdefghijAbcdefghij'] html = d.make_file(lines1, lines2) # save in file with open('output.html', 'w') as fh: fh.write(html) # open in web browser webbrowser.open('output.html')
Notes:
Stackoverflow: Change the width of difflib.make_file() HTML Table?