首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从outlook下载附件,请使用Python 3.10

无法从outlook下载附件,请使用Python 3.10
EN

Stack Overflow用户
提问于 2022-11-08 06:25:20
回答 2查看 48关注 0票数 0

我正在尝试从outlook下载一个带有特定主题行的附件。它显示已完成,但没有附件被下载。下面附上的是我的代码,如果我遗漏了什么,请帮忙。

代码语言:javascript
复制
# import libraries
import win32com.client
import re
import datetime
import  pathlib2 as pathlib

# set up connection to outlook
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
message = messages.GetFirst()
today_date = str(datetime.date.today())
while True:
  try:
    current_sender = str(message.Sender).lower()
    current_subject = str(message.Subject).lower()
    # find the email from a specific sender with a specific subject
    # condition
    if re.search('AllSalons was executed at',current_subject) != None:
    #if re.search('AllSalons was executed at',current_subject) != None and    re.search(sender_name,current_sender) != None:
      print(current_subject) # verify the subject
      print(current_sender)  # verify the sender
      attachments = message.Attachments
      attachment = attachments.Item(1)
      attachment_name = str(attachment).lower()
      attachment.SaveASFile(pathlib.path + 'C:\\Users\\UserTest\\Desktop\\Folder\\Subject Line\\Nov' + attachment_name)
    else:
      pass
    message = messages.GetNext()
  except:
    message = messages.GetNext()
  break 
print("Finished")
EN

回答 2

Stack Overflow用户

发布于 2022-11-08 11:49:33

首先,需要确保向SaveAsFile类的Attachment方法传递有效的文件路径:

代码语言:javascript
复制
attachment.SaveASFile(pathlib.path + 'C:\\Users\\UserTest\\Desktop\\Folder\\Subject Line\\Nov' + attachment_name)

确保文件夹存在于磁盘上,并且文件名中不包含禁止的符号。Windows和Linux不允许文件名中的某些符号,有关更多信息,请参见Windows和Linux目录名中禁止哪些字符?

其次,不需要迭代Outlook文件夹中的所有项并检查Subject属性值,而是需要使用Items类的Find/FindNextRestrict方法。因此,在这种情况下,您只能处理与搜索条件相对应的项,并对它们进行迭代。在我为技术博客撰写的文章中,可以了解更多关于这些方法的内容:

票数 0
EN

Stack Overflow用户

发布于 2022-11-09 18:55:46

您正在连接两个路径,其中一个是完全限定的路径("c:\".)。它唯一能工作的方法是如果pathlib.path是空的。

阿洛斯,把线换掉

代码语言:javascript
复制
attachment_name = str(attachment).lower()

使用

代码语言:javascript
复制
attachment_name = str(attachment.FileName).lower()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74356466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档