Python outlook win32 - ошибка при получении каждого сообщения из outlook, возвращающего «скрипт» - PullRequest
0 голосов
/ 22 ноября 2018

Я пишу скрипт для получения каждого вложения изображения от отправителя в python 3.7 с помощью pypiwin32.

import win32com.client
import click
import os
import logging

@click.command()
@click.option('-s', '--sender', 'sender', help="Sender's mail")
@click.option('-p', '--path', 'path', help="Save path")
def main(sender: str, path: str) -> None:

"""Save all attachments from a sender"""

# Get all messages from the inbox
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6) # 6 is the inbox folder
messages = inbox.Items
message = messages.GetFirst()

# Get all files from the path
files_list = os.listdir(path)

message_name_list = []
message_counter = 0

while message:

    print(message)

    # Check if the message is from the sender
    if message.SenderEmailAddress == sender:

        # Check if the message name have been already used
        if message not in message_name_list:

            # Add message name to the list
            message_name_list.append(message)

            # Add one to msg counter
            message_counter += 1

            # Get the message date
            mail_date = message.SentOn.date()

            # Get all attachments from the message and get a count
            attachments = message.Attachments
            att_count = attachments.Count

            # Check if the message have attachments
            if att_count >= 1:

                # Check if there is more than one attachments
                if att_count > 1:

                    for att in range(att_count):

                        # Get attachments one by one
                        nb_att = att + 1
                        attachment = attachments.Item(nb_att)

                        # Set the filename for the attachment
                        filename = "img_" + str(mail_date) + "_" + str(message_counter) + "_" + str(nb_att) + ".jpg"

                        #check if file is already saved and save if not already saved
                        if filename not in files_list:
                            print (filename)
                            attachment.SaveAsFile(path + "\\" + filename)

                    # Go to a next message
                    message = messages.GetNext()

                else:
                    # Get the attachment 
                    attachment = attachments.Item(1)

                    # Set the filename for the attachment
                    filename = 'img_' + str(mail_date) + "_" + str(message_counter) + '.jpg'

                    #check if file is already saved and save if not already saved
                    if filename not in files_list:
                        print (filename)
                        attachment.SaveAsFile(path + "\\" + filename)

                    #Go to a next message
                    message = messages.GetNext()

if __name__ == "__main__":
    main()

Обычно в терминале я должен видеть имя почты и все сохраненные вложения (называемыеимя файла в моем сценарии).Но на самом деле я вижу только то, что в терминале:

скрипт

скрипт

скрипт

скрипт

и другие "скрипт"

Кто-то знает, почему не печатаются никакие сообщения и почему печатается" скрипт "?и как это исправить?

...