如何在Python中生成HTML报告? [英] How to generate HTML report in Python?

查看:1007
本文介绍了如何在Python中生成HTML报告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来打印我所有的图形(从matplotlib中但已经保存为png文件)和HTML中的一些数据帧,就像我通常对R2HTML所做的那样.

I'm looking for a way to print out all my graphs (from matplotlib but already saved as png files) and some data frames in HTML, just like what I usually do with R2HTML.

但是,我找不到有关Python模块或执行此操作的函数的详细说明.有人可以给我一些建议吗?

However I couldn't find detailed descriptions about Python module or functions doing this. Could anyone give me some suggestions?

推荐答案

似乎存在一些工具.我一直专注于简单的html文本编写器,因为我将完全从头开始构建报告页面的结构.这可能与R2HTML有所不同,我想R2HTML对于希望填充到R对象的页面中的东西具有很多便利功能.

Looks like a couple of tools exist. I've focused on simple html text writers since I'll be crafting my report page structures completely from scratch. This may differ from the R2HTML which I would guess has a lot of convenience functionality for the sort of things one wishes to stuff into pages from R objects.

HTMLTags 这个家伙在这个ActiveState社区页面从头开始编写了一个模块: HTMLTags -在Python中生成HTML(Python食谱).在评论中,我发现了用于此答案的大多数工具.

HTMLTags This fella wrote a module from scratch at this ActiveState community page: HTMLTags - generate HTML in Python (Python recipe). In the comments I discovered most of the tools I enumerate for this answer.

htmlgen 这个看起来非常不错且基本.我不确定它是否与旧版文章中所述的模块相同.

htmlgen This package looks pretty good and basic. I'm not sure if its the same module described in this old article.

XIST 这个一个看起来很合法,并且还包含一个解析器.这是一些示例页面生成代码,使用的格式需要在各种html元素之间插入所有适当的python命令时使用的格式.另一种格式利用了一堆嵌套的函数调用,这将使python插入词充其量很尴尬.

XIST this one looks pretty legit and includes a parser as well. Here's some sample page generation code using the format that you'll need to use to interject all the appropriate python commands inbetween various html elements. The other format utilizes a bunch of nested function calls which will make the python interjection very awkward at best.

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" ) )

libxml2 通过Python绑定,有一个普通的 xmlwriter 模块,该模块可能太通用了,但很容易知道尽管如此.可以在此处找到该二进制文件的Windows二进制文件.

libxml2 through Python bindings there's a plain vanilla xmlwriter module, which is probably too generic but good to know about nonetheless. Windows binaries for this package can be found here.

这篇关于如何在Python中生成HTML报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆