简体   繁体   English

HTML在浏览器中渲染得很好,但在电子邮件中却没有(使用pandas dataframe生成HTML)

[英]HTML renders fine in browser but not in email (HTML is generated using pandas dataframe)

I am trying to send automated emails (containing tabled) in python using pandas dataframes. 我正在尝试使用pandas数据帧在python中发送自动电子邮件(包含tabled)。 When I generate an HTML for the table and open it with browser, everything works great. 当我为表生成HTML并使用浏览器打开它时,一切都很好。 When I try to render the same html in the email, some data is missing. 当我尝试在电子邮件中呈现相同的html时,缺少一些数据。

HTML rendered in browser 在浏览器中呈现HTML 在浏览器中呈现HTML

HTML rendered in Email 电子邮件中呈现的HTML 电子邮件中呈现的HTML

This is the code I'm using to create the HTML - 这是我用来创建HTML的代码 -

def csvToJinjaHTML(csvContent):

    print("Pandas: Set the max_colwidth to -1 for unlimited string length")
    pd.set_option("display.max_colwidth",-1)

    print("Pandas: Create a Pandas table from CSV content")
    pandasTable = pd.read_csv(StringIO(csvContent), index_col=False)

    # pandasTable is the dataframe that we want to beautify
    print(pandasTable)

    stylerObject = pandasTable.style

    styledHTML = (stylerObject
        .set_table_attributes('border="1" class="dataframe table table-hover table-bordered"')
        .set_properties(**{'font-size': '16pt', 'font-family': 'Calibri'})
        # .set_properties(subset=['6', '5'], **{'width': '300px'})
        .applymap(colour, subset=['ORGANIZATION'])
        .set_precision(3)
        .set_table_styles(
            [{'selector': 'tr:nth-of-type(odd)',
            'props': [('background', '#eee')]}, 
            {'selector': 'tr:nth-of-type(even)',
            'props': [('background', 'white')]},
            {'selector': 'th',
            'props': [('background', '#606060'), 
                        ('color', 'white'),
                        ('font-family', 'verdana')]},
            {'selector': 'td',
            'props': [('font-family', 'verdana')]},
            ]
        ).hide_index()
        .render()
    )

    with open('myJinjaTable.html', 'w') as f:
        print("Writing an HTML file to view the beautified Jinja table")
        f.write(styledHTML)

    return styledHTML

Figured it out. 弄清楚了。 The lengths of the HREF links were too long apparently. HREF链路的长度显然太长了。 I reduced the size of the links and it renders as expected. 我缩小了链接的大小,并按预期呈现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM