简体   繁体   English

基于 Python 的 Outlook 自动化使用来自 Excel 文件的数据作为邮件正文并添加签名

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

I'm trying to write a Python script code wherein I'll send email notifications to my team members on a daily basis.我正在尝试编写 Python 脚本代码,其中我将每天向我的团队成员发送 email 通知。

There are two excel sheets, let's say abc.xlsx and def.xlsx .有两个 excel 表,比如说abc.xlsxdef.xlsx

I already have a script that updates these files and saves them.我已经有一个脚本可以更新这些文件并保存它们。 (These files abc and def are deleted and recreated with the same name but with updated information.) (这些文件abcdef被删除并使用相同的名称重新创建,但具有更新的信息。)

Now my goal is to attach the file abc as an attachment in the mail and add the contents of def.xlsx in the email body .现在我的目标是将文件abc作为附件附加到邮件中,并将def.xlsx的内容添加到email 正文中。

I'm trying to achieve this:我正在努力实现这一目标:

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

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

Thanks and regards!

/// my outlook signature///

Here is my code:这是我的代码:

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()

Example: This type of output I'm expecting示例:我期待的这种类型的 output

Challenges:挑战:

  1. My signature will be a corrupted image with a "x" in it in the test email.在测试 email 中,我的签名将是带有“x”的损坏图像。
  2. My Excel sheet, which has to be on the body, won't have the same format.我的 Excel 表必须在身体上,不会有相同的格式。

I've copied all the codes from Stack overflow only.我只从堆栈溢出中复制了所有代码。 I did some of my research, but I'm not getting the expected output.我做了一些研究,但没有得到预期的 output。

First, you may try setting the BodyFormat property before setting up the HTMLBody property.首先,您可以在设置HTMLBody属性之前尝试设置BodyFormat属性。

Second, to get the signature added to the message body you need to call the Display method before setting up the HTMLBody property.其次,要获得添加到消息正文的签名,您需要在设置HTMLBody属性之前调用Display方法。

Third, the <figure> element is not supported in Outlook because Word is used as an email editor and applies its own business rules to message bodies.第三,在 Outlook 中不支持<figure>元素,因为 Word 用作 email 编辑器并将其自己的业务规则应用于消息正文。

Fourth, the HTMLBody property returns or sets a string which represents the message body, it is expected to get or set a full-fledged well-formed HTML document.第四, HTMLBody属性返回或设置一个表示消息正文的字符串,它期望获取或设置一个完整的格式良好的 HTML 文档。 Try to set up a well-formed HTML document and then set up a property.尝试设置格式良好的 HTML 文档,然后设置属性。

I modified it.我修改了它。 I'm still working on Challenge 2. I'll just go through the documentation that has been recommended and will share my final script.我仍在处理挑战 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.

相关问题 从邮件正文中的 Outlook 邮件中保存 excel 文件 - Save excel file from outlook mail that is in the message body 使用 PasteExcelTable 将 Excel 数据复制到带有 Python 的 Outlook 电子邮件正文 - Using PasteExcelTable to copy Excel data to the body of an Outlook email with Python 基于 Python 的脚本,用于从文件中删除在另一个文件中显示的行 - Python-based script to delete lines from file, that presented in another file 在 Python 中导入哪个库以从 Excel 文件中读取数据,以使用 Selenium 进行自动化测试? - Which library to import in Python to read data from an Excel file, for automation testing using Selenium? 如何在没有签名的情况下从 outlook 打印 email 正文 - Python - How to print email body from outlook without signature - Python 如何使用python获取收到的Outlook邮件的日期和时间并检查来自特定发件人的正文 - How to get date & time of received outlook mail and check body from particular sender using python 在基于 Python 的原始“浏览器”中使用用户的输入作为主机 - Using user's input as a host in a Python-based primitive "browser" 从基于Python的DSL中的异常自定义回溯 - Customize traceback from exceptions in Python-based DSL 使用MAPI Python解析Outlook邮件中的附件正文 - Parse the body of attachment in outlook mail with MAPI Python 如何使用python自动化向具有不同内容的多个用户发送Outlook邮件 - how to send outlook mail to multiple users with different content using python automation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM