我在 windows
平台上使用 win32com.client
、python 2.7.x
和 outlook 2013
。
我需要将 HTML
文件的内容发布到 outlook
消息的正文中。
我关注了帖子 here , here和 here关于如何将 excel 保存为 HTML
并在 outlook
中粘贴数据。
但是,当我通过 win32com.client.Dispatch
读取文件时,我看到的不是消息,而是 HTML
代码。
这是使用 win32.com
将处理过的 xlsx
文件转换为 html
格式的代码。
#Section to convert excel workbook to html
myfile = os.getcwd()+ "\\" + outfile
newfile = os.getcwd()+ "\\" + "emailData.html"
xl = EnsureDispatch('Excel.Application')
#xl.Visible = True
wb3 = xl.Workbooks.Open(myfile)
wb3WorkSheet = wb3.Worksheets(1)
wb3WorkSheet.Activate()
wb3.SaveAs(newfile, constants.xlHtml)
wb3.Close(True)
xl.Workbooks.Close()
xl.Quit()
del xl
上面的输出是newfile
,它基本上是导出保存为 html 的 xlsx 文件。然后通过 mail.body
处理程序打开,该处理程序应读取并显示 outlook 中的实际内容。
这是它的代码。
from win32com.client.gencache import EnsureDispatch
from win32com.client import constants, Dispatch
#Create and open mail message
def Emailer(text, subject, recipient):
outlook = Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = recipient
mail.Subject = subject
mail.HtmlBody = text
#mail.HtmlBody = open(newfile).read()
mail.body = open(newfile).read()
attachment1 = os.getcwd()+"//"+outfile
mail.Attachments.Add(attachment1)
mail.Display(True)
Emailer(pageTemplate,
"test subject",
"abc@yahoo.com"
)
因此,当我使用 mail.body = open(newfile).read()
打开 newfile
(html 文件)时,它会粘贴 html 内容
在新的 Outlook 电子邮件正文中。
当我使用 mail.HtmlBody = open(newfile).read()
打开 newfile
(html 文件)时,它在 outlook
中出现以下错误> 邮件正文
错误:此页面使用框架,但您的浏览器不支持它们。
对这种行为有什么想法吗?
我基本上想在 outlook 电子邮件中复制/粘贴 html
文件(这是 xlsx
的导出)。
不确定上述方法是否正确或是否有其他选择。
有没有办法将 HTML 框架粘贴/呈现到 Outlook 电子邮件正文中?
感谢任何指点。
最佳答案
您需要设置 HTMLBody 属性,但请记住,Outlook 中的 HTML 是由 Word 而非 IE 呈现的,并且不支持内联框架。
关于windows - Outlook 2013 显示 HTML 代码而不是实际数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36875791/