python - 如何使用for循环win32 python在邮件正文中附加多个嵌入图像
问题描述
我正在从 excel 中导出图像,并且我想将 Outlook 电子邮件中的所有图像作为嵌入图像附加。我的代码在导出图像的下方,但是在添加到邮件正文时,仅附加了最后一张图像。作为附件发送时,所有图像都被选中。
from PIL import ImageGrab
import win32com.client as win32
import xlrd
from win32com import client
import base64
path=
excel =win32.DispatchEx('Excel.Application')
workbook = excel.Workbooks.Open("path to file")
RNC = xlrd.open_workbook("path to file")
outlook = client.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail = outlook.CreateItem(0)
mail.Display() # required to paste chart
mail.To = 'xyz'
mail.Subject = 'Daily Report'
#exporting images from excel
l=[]
RNC_list = RNC.sheet_by_name('Sheet1')
for x in range(1,RNC_list.nrows):
for cell in RNC_list.row(x):
l.append(cell.value)
for sheet in workbook.Worksheets:
if sheet.name in l:
for i, shape in enumerate(sheet.Shapes):
if shape.Name.startswith('Picture'):
shape.Copy()
image = ImageGrab.grabclipboard()
image.save(path + r"\Pic" + sheet.name+str(i) + ".png")
filename = path + r"\Pic" + sheet.name+str(i) + ".png"
attachment= mail.Attachments.Add(filename)
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "{}".format(r"\Pic" + sheet.name+str(i)))
mail.HTMLBody = "<br><br><h4> <img src=""cid:{}"" height=""300"" width=""870""><br>".format(r"\Pic" + sheet.name+str(i))
workbook.Close()
del workbook
del excel
print("Done")
目前,仅嵌入最后一张图像。我需要将所有导出的图像嵌入邮件正文中。
解决方案
推荐阅读
- woocommerce - 使用 WooCommerce wc_get_products 按价格范围过滤产品
- javascript - 添加到 Bull 队列后,对象不再是 Class 的实例
- flutter - 如何触发另一个类的有状态小部件中的函数(当 onUnityAdsFinish 时)?
- azure-devops - 无法从工作帐户管理 Azure DevOps,即使我是组织所有者
- docker - 如果我已经有一个旧的 xx 图像推送,如何获得带有 xx 标签的图像的最后一次推送?
- amazon-web-services - Dynamodb 同时更新项目
- oracle - 如何解决 Oracle SQL 中的 FROM 关键字错误?
- visual-studio-code - Visual Studio Code 状态栏文本颜色
- javascript - 将值从跨度标签传递到输入标签
- bash - 管尾输出入柱