简体   繁体   English

无法使用 Python 从 outlook 下载附件 || Python 3.10

[英]Unable to download attachments from outlook using Python || Python 3.10

I am trying to download an attachment from outlook with a specific Subject line.我正在尝试从 outlook 下载带有特定主题行的附件。 It shows finished, but no attachment is getting downloaded.它显示已完成,但没有下载附件。 Below attached is my code, kindly help if I am missing something.下面附上我的代码,如果我遗漏了什么,请帮忙。

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

First of all, you need to make sure that a valid file path is passed to the SaveAsFile method of the Attachment class:首先,您需要确保将有效的文件路径传递给Attachment class 的SaveAsFile方法:

attachment.SaveASFile(pathlib.path + 'C:\\Users\\UserTest\\Desktop\\Folder\\Subject Line\\Nov' + attachment_name)

Make sure the folder exists on the disk and the file name doesn't contain forbidden symbols in its name.确保该文件夹存在于磁盘上并且文件名中不包含禁止的符号。 Windows and Linux don't allow some symbols in file names, see What characters are forbidden in Windows and Linux directory names? Windows 和 Linux 不允许在文件名中使用某些符号,请参阅What characters are forbidden in Windows and Linux directory names? for more information.想要查询更多的信息。

Second, instead of iterating over all items in the Outlook folder and checking the Subject property value you need to use the Find / FindNext or Restrict methods of the Items class. So, in that case you can deal only with items that correspond to your search criteria and iterate over them.其次,无需遍历 Outlook 文件夹中的所有项目并检查Subject属性值,您需要使用Items class 的Find / FindNextRestrict方法。因此,在这种情况下,您只能处理与您的搜索相对应的项目标准并迭代它们。 Read more about these methods in the articles that I wrote for the technical blog:在我为技术博客撰写的文章中阅读有关这些方法的更多信息:

You are concatenating two paths, one of them is aa fully qualified path ( "c:\" ..).您正在连接两条路径,其中一条是完全限定路径( "c:\" ..)。 The only way it can ever work is if pathlib.path is empty.它唯一可行的方法是pathlib.path为空。

Also, replace the line另外,更换线

attachment_name = str(attachment).lower()

with

attachment_name = str(attachment.FileName).lower()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM