Опит за запазване на прикачен файл от Outlook чрез pythonCOM

Преглеждам пощата си в Outlook, за да извлека всички прикачени файлове и да ги запазя в компютъра си.

Това е моят код:

import win32com.client
import os, sys

class OutlookLib:

    def __init__(self, settings={}):
        self.settings = settings

    # Gets all messages in outlook   
    def get_messages(self):      
        outlook = win32com.client.Dispatch("Outlook.Application")

        # This allows us to access the "folder" hierarchy accessible within
        # Outlook. You can see this hierarchy yourself by opening Outlook
        # manually and bringing up the folder menu
        # (which typically says "Inbox" or "Outlook Today" or something).
        ns = outlook.GetNamespace("MAPI")

        all_inbox = ns.GetDefaultFolder(6).Items
        return all_inbox

    def get_body(self, msg):
        return msg.Body

    def get_subject(self, msg):
        return msg.Subject

    def get_sender(self, msg):
        return msg.SenderName

    def get_recipient(self, msg):
        return msg.To

    def get_attachments(self, msg):
        return msg.Attachments

# Gets an attachment
# Return true if clean
# Otherwise, return false
def checkAttach(fileAtt):
    pass # TODO something here

def Main():
    global attach

    outlook = OutlookLib()
    messages = outlook.get_messages()

    # Loop all messages
    msg = messages.GetFirst()
    while msg:
        #print msg.Subject
        if not len(msg.Attachments) is 0:
            attach.append((msg.Attachments, msg.Subject))
        msg = messages.GetNext()

    for attachTuple in attach:
        print "Checking attachments under " + attachTuple[1]
        for fileAtt in attachTuple[0]:
            fileAtt.SaveAsFile(r"C:\Users\Lidor\Desktop\Dina\scan-mail")
            if checkAttach(fileAtt):
                print fileAtt.FileName + " was found as malicous."

attach = []

if __name__ == "__main__":
    Main()

Сега това е грешката, която получавам:

Traceback (most recent call last):
  File "C:\Users\Lidor\Desktop\Dina\scan-mail\mailScanner.py", line 67, in <module>
    Main()
  File "C:\Users\Lidor\Desktop\Dina\scan-mail\mailScanner.py", line 60, in Main
    fileAtt.SaveAsFile(r"C:\Users\Lidor\Desktop")
  File "<COMObject <unknown>>", line 2, in SaveAsFile
com_error: (-2147352567, 'Exception occurred.', (4096, u'Microsoft Outlook', u"Cannot save the attachment. You don't have appropriate permission to perform this operation.", None, 0, -2147024891), None)

Това е странно, защото аз съм администратор и имам разрешения за pst и изходната папка. Също така се опитах да го стартирам в администраторски CMD и все още получавам същата грешка.

Между другото, ако знаете начин да прехвърлите прикачения файл в обект на файл на Python, вместо да го запишете на моя компютър, ще бъде още по-добре.

Благодаря.


person user3016694    schedule 13.05.2014    source източник


Отговори (1)


Трябва да посочите пълното име на прикачения файл, включително частта за името. Вие само посочвате името на директорията, трябва да свържете имената на директорията и файловете:

fileAtt.SaveAsFile(r"C:\Users\Lidor\Desktop\Dina\scan-mail\" + fileAtt.FileName)
person Dmitry Streblechenko    schedule 13.05.2014
comment
Получих същата грешка, използвайки Microsoft.Office.Interop.Outlook в C# и това решение работи и там!! Много благодаря. - person Jeff; 04.12.2018