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

如何在Python程序的HTML邮件中嵌入DataFrame与Series数据?

如何在Python HTML邮件中嵌入DataFrame/空值检查Series数据

嘿,刚好我之前做过类似的需求,这就一步步教你实现:

第一步:处理数据并生成空值统计Series

首先,用pandas读取CSV文件,生成你需要的两个空值统计Series——列维度的空值数和行维度的空值数:

import pandas as pd

# 读取目标CSV文件
df = pd.read_csv('your_data.csv')

# 统计每列的空值数量(列维度)
col_null_counts = df.isna().sum()

# 统计每行的空值数量(行维度)
row_null_counts = df.isna().sum(axis=1)

第二步:将Series转换为美观的HTML表格

直接用Series的to_html()生成的表格可能不够友好,建议先把Series转成DataFrame,再添加内联CSS样式(邮件客户端大多不支持外部样式,内联样式更稳妥):

# 处理列空值统计:转成DataFrame并添加清晰列名
col_null_df = col_null_counts.to_frame(name='空值数量')
# 生成带内联样式的HTML表格
col_null_html = col_null_df.to_html(
    border=0,
    justify='center'
).replace('<table', '<table style="border-collapse: collapse; width: 80%; margin: 10px auto; border: 1px solid #ddd;">')\
 .replace('<th', '<th style="background-color: #f5f5f5; padding: 8px; text-align: left; border-bottom: 1px solid #ddd;">')\
 .replace('<td', '<td style="padding: 8px; border-bottom: 1px solid #ddd;">')

# 处理行空值统计:如果行数太多,建议只展示有空值的行,避免邮件过长
row_null_df = row_null_counts[row_null_counts > 0].to_frame(name='每行空值数量')
row_null_html = row_null_df.to_html(
    border=0,
    justify='center'
).replace('<table', '<table style="border-collapse: collapse; width: 80%; margin: 10px auto; border: 1px solid #ddd;">')\
 .replace('<th', '<th style="background-color: #f5f5f5; padding: 8px; text-align: left; border-bottom: 1px solid #ddd;">')\
 .replace('<td', '<td style="padding: 8px; border-bottom: 1px solid #ddd;">')

第三步:构建完整的HTML邮件内容

把表格嵌入到结构化的HTML正文中,加上标题和说明文字,让报告更易读:

html_content = f"""
<html>
<head>
    <meta charset="UTF-8">
</head>
<body style="font-family: Arial, sans-serif; max-width: 1000px; margin: 0 auto; padding: 20px;">
    <h2 style="color: #2c3e50; text-align: center;">数据空值检查自动报告</h2>
    
    <h3 style="color: #34495e; margin-top: 30px;">一、各列空值统计</h3>
    <p style="color: #666; margin-bottom: 10px;">以下为每列的空值数量统计:</p>
    {col_null_html}
    
    <h3 style="color: #34495e; margin-top: 40px;">二、含空值的行统计</h3>
    <p style="color: #666; margin-bottom: 10px;">仅展示存在空值的行及其空值数量:</p>
    {row_null_html}
</body>
</html>
"""

第四步:发送HTML邮件

用Python的smtplib和email库发送邮件,这里以QQ邮箱为例(其他邮箱只需修改SMTP地址和端口):

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# 邮件配置信息
sender_email = 'your_email@qq.com'
sender_password = 'your_app_auth_code'  # QQ邮箱需用授权码,而非登录密码
receiver_email = 'target_email@example.com'
smtp_server = 'smtp.qq.com'
smtp_port = 465  # SSL加密端口

# 创建邮件对象
msg = MIMEMultipart('alternative')
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = '【自动报告】数据空值检查结果'

# 添加HTML正文
msg.attach(MIMEText(html_content, 'html', 'utf-8'))

# 发送邮件
try:
    with smtplib.SMTP_SSL(smtp_server, smtp_port) as server:
        server.login(sender_email, sender_password)
        server.send_message(msg)
    print("邮件发送成功!")
except Exception as e:
    print(f"邮件发送失败:{str(e)}")

额外小提示

  • 如果需要展示完整的原始DataFrame,直接用df.to_html()生成表格后嵌入即可,样式可以参考上面的方法调整
  • 测试时先给自己发邮件,检查不同客户端(如Outlook、Gmail)的显示效果,避免样式失效

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 04:32:18