简体   繁体   English

使用 PasteExcelTable 将 Excel 数据复制到带有 Python 的 Outlook 电子邮件正文

[英]Using PasteExcelTable to copy Excel data to the body of an Outlook email with Python

Formatted Excel range copied to a Word file格式化的 Excel 范围复制到 Word 文件

This copies a range of cells from Excel and pastes them into a Word document with formatting preserved.这会从 Excel 复制一系列单元格并将它们粘贴到 Word 文档中,并保留格式。 The code works for this.该代码适用于此。 However, I also want to paste the data into the body of an email with the cell styles.但是,我还想将数据粘贴到带有单元格样式的电子邮件正文中。

import sys
from pathlib import Path
import win32com.client as win32


excel_path = str(Path.cwd() / 'input.xlsx')
excel = win32.gencache.EnsureDispatch('Excel.Application')
excel.Visible = False
excel.DisplayAlerts = False
wb = excel.Workbooks.Open(excel_path)
ws = wb.Worksheets(1)
ws.Range("A1:B2").Copy()
wb.Close()

word_path = str(Path.cwd() / 'output.docx')
word = win32.gencache.EnsureDispatch('Word.Application')
doc = word.Documents.Open(word_path)
doc.Content.PasteExcelTable(False, False, True)
doc.Close()

# That works, but now I want to paste the copied Excel range into the body of
# an email. The solution may look something like the following with the "X"
# as a "Selection" or "Range" object for the PasteExcelTable method.

outlook = win32.gencache.EnsureDispatch('Outlook.Application')
new_mail = outlook.CreateItem(0)
new_mail.To = 'person@email.com'

new_mail.Body = ws.Range("A1:B2").PasteExcelTable(False, False, True)

new_mail.Send()
sys.exit()

I believe [PasteExcelTable] 2 can be used to make this happen with Outlook as it does for Word files and perhaps someone knows how this can be done.我相信[PasteExcelTable] 2可用于在 Outlook 中实现这一点,就像对 Word 文件一样,也许有人知道如何做到这一点。

I'm not defining the range correctly.我没有正确定义范围。 Here's an example of one of my attempts:这是我的尝试之一的示例:

    new_mail.Body = ws.Range("A1:B2").PasteExcelTable(False, False, True)

I know this could be accomplished with VBA, but I want to use Python and to see whether PasteExcelTable will work for this.我知道这可以用 VBA 来完成,但我想使用 Python 并查看 PasteExcelTable 是否适用于此。 If it is not possible, I will capture an image of the data range and paste that to the email.如果不可能,我将捕获数据范围的图像并将其粘贴到电子邮件中。

After hours of research and experimentation I found out how to use PasteExcelTable in Outlook.经过数小时的研究和实验,我发现了如何在 Outlook 中使用 PasteExcelTable。 I hope this can be helpful.我希望这会有所帮助。 You only need to have the Excel Table in the clipboard.您只需要在剪贴板中有 Excel 表格。

import win32com.client


outlook = win32com.client.Dispatch('Outlook.Application')
mail = outlook.CreateItem(0)
mail.To = "person@email.com"
mail.Subject = "Subject"
mail.Display()
inspector = outlook.ActiveInspector()
word_editor = inspector.WordEditor
word_range = word_editor.Application.ActiveDocument.Content
word_range.PasteExcelTable(False, False, True)

Hope this will help...希望这会有所帮助...

import win32com.client as win32
import pandas as pd

df = pd.read_excel('fullexelpath.xlsx', index_col=False, nrows = 5,  usecols = "A:D")

body = df.to_html()

outlook = win32.gencache.EnsureDispatch('Outlook.Application')
mail = outlook.CreateItem(0)
mail.To = 'abc@gmail.com'
mail.HTMLBody = (body)
mail.Send()

This copies a range of cells from Excel and pastes them into a Word document with formatting preserved.这会从 Excel 复制一系列单元格并将它们粘贴到 Word 文档中,并保留格式。 The code works for this.该代码适用于此。 However, I also want to paste the data into the body of an email with the cell styles.但是,我还想将数据粘贴到带有单元格样式的电子邮件正文中。

There are three main ways for working with bodies in Outlook:在 Outlook 中处理实体的主要方式有以下三种:

  1. Body - a plain text representing the message body. 车身-表示邮件正文中的纯文本。
  2. HTMLBody . HTML 正文
  3. Word editor.文字编辑器。 Outlook uses Word as an email editor. Outlook 使用 Word 作为电子邮件编辑器。 The Inspector class provides the WordEditor property which returns an instance of the Document class from the Word object model which represents the message body. Inspector 类提供WordEditor属性,该属性从表示消息正文的 Word 对象模型返回 Document 类的实例。 Or just use the [GetInspector] method of the MailItem class to retrieve an instance of the Inspector class.或者只是使用MailItem类的 [GetInspector] 方法来检索Inspector类的实例。 You can read more about that in the Chapter 17: Working with Item Bodies .您可以在第 17 章:使用项目主体中阅读更多相关内容。

So, basically you can re-use the same codebase used for Word in case of Outlook.因此,基本上您可以在 Outlook 的情况下重复使用用于 Word 的相同代码库。

I was unable to use PasteExcelTable, but I was able to accomplish this with HTMLBody using the following code:我无法使用 PasteExcelTable,但我可以使用以下代码通过 HTMLBody 完成此操作:

import sys
from pathlib import Path
import win32com.client as win32
from PIL import ImageGrab

excel_path = str(Path.cwd() / 'input.xlsx')
excel = win32.gencache.EnsureDispatch('Excel.Application')
excel.Visible = False
excel.DisplayAlerts = False
wb = excel.Workbooks.Open(excel_path)
ws = wb.Worksheets(1)

win32c = win32.constants
ws.Range("A1:B2").CopyPicture(Format=win32c.xlBitmap)
img = ImageGrab.grabclipboard()
image_path = str(Path.cwd() / 'test.png')
img.save(image_path)

outlook = win32.gencache.EnsureDispatch('Outlook.Application')
new_mail = outlook.CreateItem(0)
new_mail.To = 'person@email.com'
new_mail.Attachments.Add(Source=image_path)
body = "<h1>Email text...</h1><br><br> <img src=test.png>"
new_mail.HTMLBody = (body)
new_mail.Send()
wb.Close()
sys.exit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 python 提取 Outlook 电子邮件正文和收件人电子邮件地址 - Extract outlook email body and recipient email address using python 如何使用python复制Excel工作表的内容并将其粘贴到电子邮件正文中(不作为附件)。 - how to copy the contents of an excel sheet and paste it in the body of an email.(Not as an attachment) using Python 基于 Python 的 Outlook 自动化使用来自 Excel 文件的数据作为邮件正文并添加签名 - Python-based automation of Outlook using data from an Excel file for the mail body and adding a signature 使用python通过outlook发送html文件作为电子邮件正文 - Send html file as body of email through outlook using python 如何使用 Python 在 Outlook 中提取 email 主体的一小部分? - How to extract small part of the email body in Outlook using Python? 使用 python 从 Outlook 响应 email 中提取正文 - Extracting body from Outlook response email using python 使用 Python 将 Outlook 组复制到 Excel - Copy Outlook Groups to Excel with Python 使用 Python 从 Outlook 电子邮件正文中提取数字 - Extracting numbers from outlook email body with Python 使用 Python 在 Outlook 上打印电子邮件的正文 - Print email's body on Outlook with Python 如何使用 Python 为 Excel 的每一行创建新的 Outlook 电子邮件 - How to create a new Outlook email for every row of Excel using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM