Outlook email not showing the desired html table format (border collapse not working)

  css, html, outlook, python-3.x, pywin32

I used df.to_html(classes="wide", escape=False, index=False) to convert two pandas dataframe into html with same css style, and I inserted them into my mail.HTMLbody. But when I sent out the email the first table has border collapsed, but the second table has border separate.

Here’s my function to convert pandas dataframe into html

def insert_html_table(df) -> str:
    """
    Write an entire dataframe to an HTML file with nice formatting.
    """

    result = """
    <style>
        table {
            margin-left:  auto;
            margin-right: auto;
            table-layout: fixed;
            width:1000px;
        }
        table, th, td {
            border: 1px solid DeepSkyBlue;
            border-collapse: collapse;   
        }
        th {
            background-color: DeepSkyBlue;
            color: white;
            text-align: center;
        }
        td:nth-child(1) {
            text-align: center;
            font-family: Calibri, sans-serif;
            font-size: 11.0pt;
        }
        td:nth-child(2) {
            text-align: center;
            font-family: Calibri, sans-serif;
            font-size: 11.0pt;
            word-wrap: break-word;
        }

        table tbody tr:hover {
            background-color: #dddddd;
        }

    </style>
    """

    result += df.to_html(classes="wide", escape=False, index=False)

    return result

And here’s how I inserted them into my mail.HTMLbody

mail.HTMLbody = mail.HTMLbody.replace("{df1}", insert_html_table(df1)).replace("{df2}",insert_html_table(df2))

Which means the two dataframes applied same style, but after I send out the first table will have border collapsed while the second table is not

This is how the tables look like in sent email.
sent emails

Please let me know how can I make the two tables in same border format. Thank you!!

Source: Python-3x Questions

LEAVE A COMMENT