[英]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 中处理实体的主要方式有以下三种:
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 . 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.