【问题标题】:How to generate a static .html with python如何使用 python 生成静态 .html
【发布时间】:2018-06-12 09:00:26
【问题描述】:

我正在寻找一种 python 解决方案来创建可以通过电子邮件发送的静态 .html,可以附加或嵌入在电子邮件中(如果需要更多工作,请忽略后一个选项)。我对 .html 的布局没有要求。此处的重点是确定生成离线 .html 的痛苦较少的解决方案。

一个潜在的解决方案可能类似于以下伪代码。

from some_unknown_pkg import StaticHTML

# Initialise instance
newsletter = StaticHTML()

# Append charts, tables and text to blank newsletter.
newsletter.append(text_here)
newsletter.append(interactive_chart_generated_with_plotly)
newsletter.append(more_text_here)
newsletter.append(a_png_file_loaded_from_local_pc)



# Save newsletter to .html, ready to be sent out.
newsletter.save_to_html('newsletter.html')

'newsletter.html' 可以在任何浏览器中打开。只是为了提供更多上下文,这个 .html 应该发送给我公司内部的一些选定人员,并包含敏感数据。我正在使用 plotly 生成要插入到 .html 中的交互式图表。

【问题讨论】:

    标签: python html plotly static-html


    【解决方案1】:

    可能解决方案here

    似乎该答案中的包正是您想要的。文档:http://www.yattag.org/

    另一个非常好的包here

    【讨论】:

      【解决方案2】:

      通过导入sys 模块启动您的python 模块并将标准输出重定向到newsletter.html

      import sys
      sys.stdout = open('newsletter.html','w')
      

      这会将生成的任何输出重定向到 html 文件。现在,只需在 python 中使用print 命令将 html 标签传输到文件。例如尝试:

      print "<html>"
      print "<p> This is my NewsLetter </p>"
      print "</html>"`
      

      这段代码 sn-p 将创建一个基本的 HTML 文件。现在,您可以在任何浏览器中打开此文件。对于发送电子邮件,您可以使用 python 的emailsmtplib 模块。

      【讨论】:

        【解决方案3】:

        Dominate 包看起来提供了一种简单直观的方式来创建 HTML 页面。 https://www.yattag.org/

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-26
          相关资源
          最近更新 更多