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

如何为sendmail邮件正文中的CSV转HTML表格添加文件名及间距?

解决方案:给邮件中的表格添加标题和间距

当然可以实现!这里有两种简单的方法来满足你的需求,一种是直接修改Python脚本生成带标题和间距的HTML内容,另一种是在合并邮件内容时通过Shell命令插入这些元素,下面分别详细说明:

方式一:修改Python脚本(推荐)

把两个脚本合并成一个,就能统一控制输出的HTML结构,直接为每个表格添加对应的文件名标题和间距样式,生成的内容更规整,也省去了后续Shell处理的麻烦。

修改后的合并脚本示例:

#!/usr/bin/python
import pandas as pd

# 定义要处理的CSV文件与对应输出的映射(这里可以按需扩展更多文件)
csv_files = [('users.csv', 'output.html'), ('groups.csv', 'output1.html')]

# 拼接带标题和间距的HTML内容
combined_html = ""
for csv_path, _ in csv_files:
    # 读取CSV并生成表格HTML
    df = pd.read_csv(csv_path)
    table_html = df.to_html()
    # 插入文件名标题和间距样式
    combined_html += f"""
    <h3>{csv_path}</h3>
    {table_html}
    <div style="margin-bottom: 20px;"></div>
    """

# 将合并后的内容写入统一文件
with open('combined_output.html', 'wt') as f:
    f.write(combined_html)

执行完这个脚本后,只用一条命令就能发邮件:

cat mailheader combined_output.html | sendmail -t

如果你不想合并脚本,也可以单独修改每个脚本,在输出表格前添加标题和间距:
比如修改处理users.csv的脚本:

#!/usr/bin/python
import pandas as pd

df = pd.read_csv('users.csv')
with open('output.html','wt') as f:
    f.write(f"<h3>users.csv</h3>\n")
    f.write(df.to_html())
    f.write("\n<div style='margin-bottom:20px;'></div>")

处理groups.csv的脚本同理替换文件名即可。

方式二:通过Shell命令插入标题和间距

如果不想修改Python脚本,也可以在合并邮件内容时,手动插入标题和间距元素:

{
  cat mailheader;
  echo "<h3>users.csv</h3>";
  cat output.html;
  echo "<div style='margin-bottom:20px;'></div>";
  echo "<h3>groups.csv</h3>";
  cat output1.html;
} | sendmail -t

这种方式不需要单独合并HTML文件,直接按顺序拼接所有内容后发送邮件。

样式调整说明

  • 标题用<h3>标签,你可以根据需求改成<h2>或自定义样式(比如添加颜色:<h3 style="color:#2c3e50;">users.csv</h3>)
  • 间距用<div style="margin-bottom:20px;"></div>,20px可以按需调整为15px或其他数值,也可以用<br><br>实现简单换行间距,但用margin的排版更可控。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 03:49:16