首页 > 解决方案 > 使用 Python 将 Outlook 电子邮件正文复制到 Excel

问题描述

我在 Outlook 电子邮件正文中有 3 个表格。Outlook 电子邮件可以用唯一的主题来识别,我希望将以上 3 个表格粘贴到 Excel 工作表的每个单元格中,而不是单个单元格中。

我试过下面的python代码,它将整个内容粘贴为单个单元格中的文本。

在此处输入图像描述

import win32com.client
import os
import openpyxl

# Call a Workbook() function of openpyxl 
# to create a new blank Workbook object 
wb = openpyxl.Workbook() 

# Get workbook active sheet 
# from the active attribute 
sheet = wb.active 

# Cell objects also have row, column 
# and coordinate attributes that provide 
# location information for the cell. 

# Note: The first row or column integer 
# is 1, not 0. Cell object is created by 
# using sheet object's cell() method. 
c1 = sheet.cell(row = 1, column = 1) 


outlook=win32com.client.Dispatch("Outlook.Application").GetNameSpace("MAPI")
inbox=outlook.GetDefaultFolder(6) #Inbox default index value is 6
message=inbox.Items
message2=message.GetLast()
subject=message2.Subject
body=message2.body
date=message2.senton.date()
sender=message2.Sender
attachments=message2.Attachments
for m in message:
    if m.Subject=="Sample Change Log ":# here in my requirement i will change the dates
        print(m.body)
        # writing values to cells 
        c1.value = m.body

wb.save("D:\\demo.xlsx")

标签: pythonexceloutlook

解决方案


c1 = sheet.cell(row = 1, column = 1) 

您只指定了一个单元格,因此它将所有数据放入一个单元格中。

如果您希望它位于多个位置,请为每封电子邮件指定一个单元格位置


推荐阅读