使用Python将Outlook电子邮件正文复制到Excel

2024-04-19 09:12:05 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

enter image description here

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")

Tags: theimportobjectiscellbodycolumnsheet
1条回答
网友
1楼 · 发布于 2024-04-19 09:12:05
c1 = sheet.cell(row = 1, column = 1) 

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

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

相关问题 更多 >