Printing mutiple HTML tables using tabulate in python

2024/10/11 4:33:22

I want to produce two HTML tables using tabulate package, but I am only able to produce one table and send mail.

Is it possible to put more than one html table into a message sent with smtplib and email? Whenever I use attach() for more than one thing, it only adds the first.

text = """
Hello, Friend.Here is your data:{table}Regards,Me
"""html = """
<html>
<body><p>Hello, Friend.</p><p>Here is your data:</p>{table}<p>Regards,</p><p>Me</p>
</body>
</html>
"""with open('input.csv') as input_file:reader = csv.reader(input_file)data = list(reader)text = text.format(table=tabulate(data, headers="firstrow", tablefmt="grid"))html = html.format(table=tabulate(data, headers="firstrow", tablefmt="html"))message = MIMEMultipart("alternative", None, [MIMEText(text), MIMEText(html,'html')])
Answer

Yes. First you have to change the number of fields you want to substitute as tables:

text = """ [...] {table1} {table2} [...] """

Then you have to pass the content to format as kwargs:

text = text.format(table1=tabulate(data, headers="firstrow", tablefmt="grid"),table2=your_new_table_tabulation)

Same you can do for html

String formatting in Python is no sort of attachment, but rather a substitution of content into a pre-formatted field. You can substitute as many fields as you'd like, putting the names you'd like, and with the content you'd like. Then, when you send the email, you will send a normal HTML file with the tables rendered into as HTML/text

I encourage you to check out: https://docs.python.org/3/library/string.html#string.Formatter.format

That is for Python's version 3.7. You have almost every Python's version available online.

https://en.xdnf.cn/q/118368.html

Related Q&A

Why am I getting presentation error in python list?

My code is following: d = [int(d) for d in input().split()]l = [] c = 1 for i in range(len(d)):if d[i] not in l:l.append(d[i])c += 1for i in range(len(l)):print(l[i], end=" ")Evaluation: Inpu…

Calculating BLEU score between two sumamries in python

prediction = "I am ABC. I have completed my bachelors degree in computer application at XYZ University and I am currently pursuing my masters degree in computer application through distance educat…

Pivot a dataframe with duplicate values in Index

I have a pandas dataframe like thissnapDate instance waitEvent AvgWaitInMs 0 2015-Jul-03 XX gc cr block 3-way 1 1 2015-Jun-29 YY gc current b…

Concatenate .txt files with same names in different folders with python

I have two folders containing many text files with matching file names. So I am concatenating folder1/file1.txt with folder2.file1.txt. My current code appends data from folder2/file1 to folder2/file1 …

Importing module via another module

In module A, I import module B. Then, in module C, I import module A. In module C, will I be able to use the content of module B implicitly via the import of module A, or will I have to explicitly impo…

Why time.time() gives 0.0?

I have a python program defined by a function myFunc(m,n)Basically, the function contains two for loops.def myFunc(m, n) : for i in range(m) : for j in range(n) : # do it ...return I would like to calc…

Function reads np.array - produces the mean for k nn to number p in np.array

I need to defina a function which reads a numpy array and produces the mean for k nearest points to number p in the array. Example: array= np.array([1, 2, 3, 4, 5, 6, 7, 50, 24, 32, 9, 11, 12, 10]) p= …

How to plot a line over a bar chart

I am trying to plot a line over a bar chart, but when I plotted the line the bar chart disappeared - and so did my x index values. Can anyone help me plot the line and the bar in the same figure? Than…

How to trim spaces between list elements in an f-string? [duplicate]

This question already has answers here:Print all items in a list with a delimiter(8 answers)Closed 2 months ago.I have a string I am formatting and printing lists using f-string and I need to eliminate…

Keeping name and score together while sorting

so I need to sort some high scores into order and here is the code I already have:def sortscores():namelist = []scorelist = []hs = open("hst.txt", "r")hscounter = 0for line in hs:if…