2

I'm looking for a python solution to create a static .html that can be sent out via email, either attached or embedded in the email (ignore this latter option if it requires a lot more work). I do not have requirements for what regards the layout of the .html. The focus here is in identifying the less painful solution for to generate an offline .html.

A potential solution could be along the lines of the following pseudo-code.

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

Where 'newsletter.html' can be opened in a whatever browser. Just to provide a bit more context, this .html is supposed to be sent out to a few selected people inside my company and contains sensible data. I'm using plotly to generate interactive charts to be inserted in the .html.

MLguy
  • 1,776
  • 3
  • 15
  • 28

3 Answers3

2

Possible solution here

Seems package in that answer is exactly you want. Docs: http://www.yattag.org/

Another pretty nice package here.

Michael
  • 1,170
  • 2
  • 14
  • 35
2

Start your python module with by importing sys module and redirect stdout to newsletter.html

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

This will redirect any output generated to the html file. Now, just use the print command in python to transmit html tags to the file. For eg try:

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

This code snippet will create a basic HTML file. Now, you can open this file in any browser. For sending email you can use email and smtplib modules of python.

Andrii Krupka
  • 4,276
  • 3
  • 20
  • 41
1

The Dominate package looks like it provides a simple and intuitive way to create HTML pages. https://www.yattag.org/

Jeff1265344
  • 123
  • 9