改变 difflib.make_file() HTML Table 的宽度?
Change the width of difflib.make_file() HTML Table?
我正在使用 python difflib 和 make_file() 为法律文件创建 diff 文件,它输出 HTML 包含 Table 和 Diffs 的文件。
但是输出 table 太宽了,无法读取。
有什么方法可以改变 table 的宽度或从我的函数调用中添加其他 css 吗?我在文档中找不到任何相关内容。
万一不可能,有什么方法可以解决我的“table 宽度太宽”问题?
我已经尝试添加 bootstrap 并将 table 放入响应式 div 中,但没有成功。
您可以在 HtmlDiff
中使用 wrapcolumn=
import difflib
import webbrowser
d = difflib.HtmlDiff(wrapcolumn=10)
lines1 = ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa']
lines2 = ['XXXXXXXXXXXXXaaaaaaaaaYYYaaaaaaZZZZZZZZZZZaaaaZZZZZZZZ']
html = d.make_file(lines1, lines2)
with open('output.html', 'w') as fh:
fh.write(html)
webbrowser.open('output.html')
没有wrapcolumn=
和wrapcolumn=10
编辑:
我没有测试它但是在 diff you can see hidden variables _styles, _table_template, _file_template which you could replace in HtmlDiff 的源代码中生成不同的 HTML。这样您就可以添加 classes 和样式来格式化它。
可能还需要将隐藏_format_line改为移除nowrap="nowrap"
编辑:
我使用 HtmlDiff
创建自己的 class 新的 _format_line()
没有 nowrap="nowrap"
现在我可以使用 width
和 [=22= 添加样式]
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 remove only `nowrap="nowrap"`
def _format_line(self,side,flag,linenum,text):
"""Returns HTML markup of "from" / "to" text lines
side -- 0 or 1 indicating "from" or "to" text
flag -- indicates if difference on line
linenum -- line number (used for line number column)
text -- line text to be marked up
"""
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()
return '<td class="diff_header"%s>%s</td><td>%s</td>' \
% (id,linenum,text)
# --- main ---
d = MyHTML()
# >>> changing styles after creating `MyHTML` or in original `HtmlDiff <<<
#d._styles = d._styles + """
#table.diff {width: 300px}
#.diff_sub {display: inline-block; word-break: break-word;}
#.diff_add {display: inline-block; word-break: break-word;}
#"""
#d._file_template = """new template"""
lines1 = ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa']
lines2 = ['XXXXXXXXXXXXXaaaaaaaaaYYYaaaaaaZZZZZZZZZZZaaaaZZZZZZZZ']
html = d.make_file(lines1, lines2)
with open('output.html', 'w') as fh:
fh.write(html)
webbrowser.open('output.html')
较短的版本
import difflib
import webbrowser
d = HtmlDiff(wrapcolumn=10)
d._file_template = """new template with %(charset)s %(styles)s %(table)s %(legend)s"""
#d._styles = """new styles"""
lines1 = ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa']
lines2 = ['XXXXXXXXXXXXXaaaaaaaaaYYYaaaaaaZZZZZZZZZZZaaaaZZZZZZZZ']
html = d.make_file(lines1, lines2)
with open('output.html', 'w') as fh:
fh.write(html)
webbrowser.open('output.html')
我正在使用 python difflib 和 make_file() 为法律文件创建 diff 文件,它输出 HTML 包含 Table 和 Diffs 的文件。
但是输出 table 太宽了,无法读取。
有什么方法可以改变 table 的宽度或从我的函数调用中添加其他 css 吗?我在文档中找不到任何相关内容。
万一不可能,有什么方法可以解决我的“table 宽度太宽”问题?
我已经尝试添加 bootstrap 并将 table 放入响应式 div 中,但没有成功。
您可以在 HtmlDiff
wrapcolumn=
import difflib
import webbrowser
d = difflib.HtmlDiff(wrapcolumn=10)
lines1 = ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa']
lines2 = ['XXXXXXXXXXXXXaaaaaaaaaYYYaaaaaaZZZZZZZZZZZaaaaZZZZZZZZ']
html = d.make_file(lines1, lines2)
with open('output.html', 'w') as fh:
fh.write(html)
webbrowser.open('output.html')
没有wrapcolumn=
和wrapcolumn=10
编辑:
我没有测试它但是在 diff you can see hidden variables _styles, _table_template, _file_template which you could replace in HtmlDiff 的源代码中生成不同的 HTML。这样您就可以添加 classes 和样式来格式化它。
可能还需要将隐藏_format_line改为移除nowrap="nowrap"
编辑:
我使用 HtmlDiff
创建自己的 class 新的 _format_line()
没有 nowrap="nowrap"
现在我可以使用 width
和 [=22= 添加样式]
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 remove only `nowrap="nowrap"`
def _format_line(self,side,flag,linenum,text):
"""Returns HTML markup of "from" / "to" text lines
side -- 0 or 1 indicating "from" or "to" text
flag -- indicates if difference on line
linenum -- line number (used for line number column)
text -- line text to be marked up
"""
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()
return '<td class="diff_header"%s>%s</td><td>%s</td>' \
% (id,linenum,text)
# --- main ---
d = MyHTML()
# >>> changing styles after creating `MyHTML` or in original `HtmlDiff <<<
#d._styles = d._styles + """
#table.diff {width: 300px}
#.diff_sub {display: inline-block; word-break: break-word;}
#.diff_add {display: inline-block; word-break: break-word;}
#"""
#d._file_template = """new template"""
lines1 = ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa']
lines2 = ['XXXXXXXXXXXXXaaaaaaaaaYYYaaaaaaZZZZZZZZZZZaaaaZZZZZZZZ']
html = d.make_file(lines1, lines2)
with open('output.html', 'w') as fh:
fh.write(html)
webbrowser.open('output.html')
较短的版本
import difflib
import webbrowser
d = HtmlDiff(wrapcolumn=10)
d._file_template = """new template with %(charset)s %(styles)s %(table)s %(legend)s"""
#d._styles = """new styles"""
lines1 = ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa']
lines2 = ['XXXXXXXXXXXXXaaaaaaaaaYYYaaaaaaZZZZZZZZZZZaaaaZZZZZZZZ']
html = d.make_file(lines1, lines2)
with open('output.html', 'w') as fh:
fh.write(html)
webbrowser.open('output.html')