将 Word 内容复制到 Outlook [英] Copy Word contents into Outlook

查看:240
本文介绍了将 Word 内容复制到 Outlook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含格式化文本、图像和表格的 Word 文档.您可以手动复制其内容并毫无问题地插入到 Outlook 中.如何在 Python 中做到这一点?

I have a Word document with formatted text, images, and tables. You can manually copy its contents and insert into Outlook with no problems. How to do it in Python?

我的代码:

import win32com.client

word = win32com.client.Dispatch("Word.Application")
doc = word.Documents.Open(your_doc_path)
contents = 'What here?'  # doc.Content?

outlook = win32com.client.Dispatch("Outlook.Application")
# Create a new MailItem object
msg = outlook.CreateItem(0)

msg.Body = 'What here?'  # `contents` throws pywintypes.com_error
msg.Display(False)

最接近的问题:

将 Word 格式复制到 Outlook 邮件中

word 文档的内容作为电子邮件的正文

如何从 Microsoft Word 粘贴到 Outlook

将富文本导出到 Outlook 并保持格式

不起作用:

1-将 Word 文档另存为 HTML(类似 RTF)和

1-Saving a Word doc as HTML (analogously RTF) and

with open(html_path, 'r', errors='ignore') as f:
    # Possible UnicodeDecodeError
    doc_body = f.read()

    msg.BodyFormat = 2  # olFormatHTML
    msg.Body = doc_body

2-读取压缩后的 Word 文档中的 document.xml.

2-Reading document.xml in the zipped Word document.

可能的方式:

Inspector 类的 WordEditor 属性返回表示消息正文的 Word 文档.

The WordEditor property of the Inspector class returns an instance of the Word Document which represents the message body.

如何通过 win32com 应用 WordEditor?

How to apply WordEditor via win32com?

推荐答案

在Alina Li的帮助下,这里是最终的解决方案:

With the help of Alina Li, here is the final solution:

import win32com.client

word = win32com.client.Dispatch("Word.Application")
doc = word.Documents.Open(word_path)
doc.Content.Copy()
doc.Close()

outlook = win32com.client.Dispatch("Outlook.Application")
# Create a new MailItem object
msg = outlook.CreateItem(0)
msg.GetInspector.WordEditor.Range(Start=0, End=0).Paste()

msg.Display(False)

图像、表格、格式 - 一切都很好.

Images, tables, format - everything is good.

这篇关于将 Word 内容复制到 Outlook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆