Home > OS >  Copy and format excel table to outlook body using python
Copy and format excel table to outlook body using python

Time:04-02

I am trying to send an outlook email with content from a excel file pasted in the message body of outlook email. I have to use smtplib only

I used the code from enter image description here

I expect my output to be like as shown below (as a table and not as a image in outlook body)

enter image description here

CodePudding user response:

You might want to add a border=1 attribute to your table HTML element. Sadly, tabulate doesn't support such a thing. One way to add the border attribute is to skip tabulate.tabulate and write your own table formatter like so:

def html_tabulate(table):
    table_it = iter(table)
    result = []
    result.append("<table border=1>")
    result.append("<thead>")
    headers = next(table_it)
    result.append("<tr><th>"   "</th><th>".join(headers)   "</th></tr>")
    result.append("</thead>")
    for row in table_it:
        result.append("<tr><td>"   "</td><td>".join(row)   "</td></tr>")
    result.append("</table>")
    return "\n".join(result) 

and then use your new function when you create the HTML for your emaill:

html = html.format(table=html_tabulate(data))
  • Related