簡體   English   中英

基於 Python 的 Outlook 自動化使用來自 Excel 文件的數據作為郵件正文並添加簽名

[英]Python-based automation of Outlook using data from an Excel file for the mail body and adding a signature

我正在嘗試編寫 Python 腳本代碼,其中我將每天向我的團隊成員發送 email 通知。

有兩個 excel 表,比如說abc.xlsxdef.xlsx

我已經有一個腳本可以更新這些文件並保存它們。 (這些文件abcdef被刪除並使用相同的名稱重新創建,但具有更新的信息。)

現在我的目標是將文件abc作為附件附加到郵件中,並將def.xlsx的內容添加到email 正文中。

我正在努力實現這一目標:

Hello All,
Please find the pending lists here as follows:

///The info from def.xlsx sheet comes here///

Thanks and regards!

/// my outlook signature///

這是我的代碼:

import win32com.client as win32
import pandas as pd

# reading a file, which needs to be on mail body
df1 = pd.read_excel('def.xlsx')

html_table = df1.to_html(index=False)

outlook = win32.gencache.EnsureDispatch('Outlook.Application')
mail = outlook.CreateItem(0)
mail.To = 'mail@me.com'
mail.CC = 'mail@me.com'
mail.Subject = 'Test mail'

# path to signature should be User\AppData\Roaming\Microsoft\Signatures\signature.htm
pathToIMage = r'path_to_my_signature'
attachment = mail.Attachments.Add(pathToIMage)
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "MyId1")

#  modify the mail body as per need
mail.Attachments.Add(Source="C:\..abc.xlsx")
body = "<p>Hi All, Please find the updates pending updates below:" + html_table + " <br>Thanks and regards <p> <figure><img src=""cid:MyId1""</figure>"
mail.HTMLBody = (body)
mail.Send()

示例:我期待的這種類型的 output

挑戰:

  1. 在測試 email 中,我的簽名將是帶有“x”的損壞圖像。
  2. 我的 Excel 表必須在身體上,不會有相同的格式。

我只從堆棧溢出中復制了所有代碼。 我做了一些研究,但沒有得到預期的 output。

首先,您可以在設置HTMLBody屬性之前嘗試設置BodyFormat屬性。

其次,要獲得添加到消息正文的簽名,您需要在設置HTMLBody屬性之前調用Display方法。

第三,在 Outlook 中不支持<figure>元素,因為 Word 用作 email 編輯器並將其自己的業務規則應用於消息正文。

第四, HTMLBody屬性返回或設置一個表示消息正文的字符串,它期望獲取或設置一個完整的格式良好的 HTML 文檔。 嘗試設置格式良好的 HTML 文檔,然后設置屬性。

我修改了它。 我仍在處理挑戰 2。我將通過推薦的文檔只 go 並將分享我的最終腳本。

import win32com.client as win32
import pandas as pd
import os
import codecs

df1 = pd.read_excel('mail_body.xlsx')
html_table = df1.to_html(index=False)

# below is the coding logic for signature
sig_files_path = 'AppData\Roaming\Microsoft\Signatures\\' + 'signature_file_name' + '_files\\'
sig_html_path = 'AppData\Roaming\Microsoft\Signatures\\' + 'signature_file_name' + '.htm'

signature_path = os.path.join((os.environ['USERPROFILE']), sig_files_path)
html_doc = os.path.join((os.environ['USERPROFILE']), sig_html_path)
html_doc = html_doc.replace('\\\\', '\\')

html_file = codecs.open(html_doc, 'r', 'utf-8', errors='ignore')
signature_code = html_file.read()

signature_code = signature_code.replace(('signature_file_name' + '_files/'), signature_path)
html_file.close()

outlook = win32.gencache.EnsureDispatch('Outlook.Application')
mail = outlook.CreateItem(0)
mail.To = 'mail@me.com'
mail.CC = 'mail@me.com'
mail.Subject = 'TEST EMAIL'
mail.Attachments.Add(Source=r"C:\..abc.xlsx")

#  modify the mail body as per need
mail.BodyFormat = 2
body = "<p>Hi All, Please find the updates pending updates below:" + html_table + " <br>Thanks and regards <br><br>"
mail.Display()
mail.HTMLBody = body + signature_code
mail.Send()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM