You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法将DataFrame转为HTML表格:变量嵌入HTML失败问题

Fix: Unable to Convert DataFrame to HTML Table When Embedding with a String Variable

Let's break down what's going wrong here and fix it step by step:

The Root of the Problem

Your line .format((Name,Body).to_html()) has two critical issues:

  1. You’re wrapping Name (a string) and Body (a pandas DataFrame) into a tuple, then trying to call to_html() on that tuple. Tuples don’t have a to_html() method—this method only exists for pandas DataFrames.
  2. The format() method expects separate arguments for each placeholder in your string, but you’re passing a single (invalid) value instead of matching the two placeholders in your template.

The Correct Approach

First, convert your DataFrame to an HTML table string on its own, then pass both the name and this HTML string to format():

Name = "Tom"
Body = Email_Body_Data_Frame

# Convert the DataFrame to a properly formatted HTML table string
body_html = Body.to_html()

# Insert both variables into your HTML template
html = """\
<html> <head> </head> <body>
Hi {0} <br> <br> TEXT TEXT TEXT TEXT TEXT <br> <br> {1} <br>
TEXT TEXT TEXT TEXT TEXT TEXT
</body> </html>
""".format(Name, body_html)

What This Does

  • Body.to_html() takes your DataFrame and converts it into a complete HTML table snippet (with <table>, <tr>, <td> tags, etc.) that can be directly embedded into your email’s HTML structure.
  • The format() method now receives two separate arguments: Name fills the {0} placeholder, and body_html fills {1}—exactly matching the two placeholders in your template.

I also removed the extra <br> after </body> in your template—this was invalid HTML, as body tags shouldn’t have content after they’re closed.

内容的提问来源于stack exchange,提问作者JoeDainton123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 19:57:36