看起来有几个工具存在。我专注于简单的 html 文本编写器,因为我将完全从头开始制作我的报告页面结构。这可能与 R2HTML 不同,我猜 R2HTML 对于人们希望从 R 对象填充到页面中的东西有很多便利的功能。
HTMLTags 这个家伙在 ActiveState 社区页面上从头开始编写了一个模块:HTMLTags - 在 Python 中生成 HTML(Python 配方)。在评论中,我发现了我为此答案列举的大部分工具。
htmlgen 这个包看起来相当不错而且很基础。我不确定它是否与这篇旧文章中描述的模块相同。
XIST 这个看起来相当合法,并且还包含一个解析器。下面是一些示例页面生成代码,使用您需要用来在各种 html 元素之间插入所有适当的 python 命令的格式。另一种格式利用了一堆嵌套函数调用,这最多会使 python 感叹词变得非常尴尬。
with xsc.build() :
with xsc.Frag() as myHtmlReport :
+xml.XML()
+html.DocTypeXHTML10transitional()
with html.html() :
reportTitle = "My report title"
with html.head() :
+meta.contenttype()
+html.title( reportTitle )
with html.body() :
# Insert title header
+html.h1( reportTitle )
with html.table() :
# Header Row
with html.tr() :
with html.td() :
+xsc.Text( "Col 1 Header" )
with html.td() :
+xsc.Text( "Col 2 Header" )
# Data Rows
for i in [ 1, 2, 3, 4, 5 ] :
with html.tr() :
with html.td() :
+xsc.Text( "data1_" + str(i) )
with html.td() :
+xsc.Text( "data2_" + str(i) )
# Write the report to disk
with open( "MyReportfileName.html" , "wb" ) as f:
f.write( myHtmlReport.bytes( encoding="us-ascii" ) )
Run Code Online (Sandbox Code Playgroud)
libxml2 通过 Python 绑定有一个普通的xmlwriter模块,它可能太通用了,但了解一下还是有好处的。可以在此处找到此包的 Windows 二进制文件。