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

Python实现Outlook自动化发邮件:Excel嵌入及签名异常求助

Fixes for Outlook Email Script Issues

Issue 1: Broken Signature Image

Manually attaching signature images and using CID is error-prone because Outlook signatures depend on properly structured HTML and relative image paths. Instead, let Outlook handle the signature automatically:

  • When creating a new MailItem, Outlook inserts your default signature into mail.HTMLBody by default.
  • Prepend your custom content (greeting, table, closing) to this existing signature HTML to preserve all signature formatting and images.

Issue 2: Poorly Formatted Table in Body

Pandas' default to_html() output generates a plain, unstyled table. Add CSS to improve readability and match expected formatting.


Corrected Code

import win32com.client as win32
import pandas as pd

# Read the Excel file for the email body
df1 = pd.read_excel('def.xlsx')

# Generate styled HTML table: add CSS class and exclude index column
html_table = df1.to_html(index=False, classes='styled-table')

# Initialize Outlook and create a new mail item (loads default signature automatically)
outlook = win32.gencache.EnsureDispatch('Outlook.Application')
mail = outlook.CreateItem(0)

# Set email recipients and subject
mail.To = 'mail@me.com'
mail.CC = 'mail@me.com'
mail.Subject = 'Pending Updates Notification'

# Attach the abc.xlsx file (replace with full correct file path)
mail.Attachments.Add(Source=r"C:\full\path\to\abc.xlsx")

# Build custom email content with CSS styling for the table
custom_body = f"""
<style>
.styled-table {{
    border-collapse: collapse;
    margin: 20px 0;
    font-size: 0.9em;
    font-family: Arial, sans-serif;
    min-width: 500px;
}}
.styled-table thead tr {{
    background-color: #2c3e50;
    color: #ffffff;
    text-align: left;
}}
.styled-table th, .styled-table td {{
    padding: 12px 15px;
    border: 1px solid #ddd;
}}
.styled-table tbody tr {{
    border-bottom: 1px solid #ddd;
}}
.styled-table tbody tr:nth-child(even) {{
    background-color: #f8f9fa;
}}
.styled-table tbody tr:last-of-type {{
    border-bottom: 2px solid #2c3e50;
}}
</style>
<p>Hello All,</p>
<p>Please find the pending lists here as follows:</p>
{html_table}
<p>Thanks and regards!</p>
"""

# Combine custom content with Outlook's default signature
mail.HTMLBody = custom_body + mail.HTMLBody

# Send the email
mail.Send()

Key Improvements Explained

  1. Signature Fix:

    • Creating the MailItem first lets Outlook load your default signature into mail.HTMLBody automatically.
    • Appending custom content to the signature ensures all signature images and formatting are retained without manual CID handling.
  2. Table Formatting Fix:

    • Added a <style> block to define a styled-table class with:
      • Collapsed borders for clean, continuous lines
      • Styled header row with dark background and white text
      • Padding for cell content to avoid cramped text
      • Alternating row colors for better readability
      • Bold bottom border for the last row to wrap up the table
    • Applied the style class directly to the pandas-generated table using classes='styled-table'.
  3. Other Cleanups:

    • Fixed typo in greeting text
    • Used f-string for cleaner, more readable HTML body construction
    • Added proper paragraph tags (<p>) for natural line breaks

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 11:54:25