Download a csv file from gmail using python

后端 未结 4 1184
無奈伤痛
無奈伤痛 2020-12-17 06:40

I tried different python scripts for download a CSV attachment from Gmail. But I could not able to get it.Is this possible. If it is possible which python script should I us

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-17 07:27

    An up to date answer has been provided at Download attachment from mail using Python

    import os
    from imbox import Imbox # pip install imbox
    import traceback
    
    # enable less secure apps on your google account
    # https://myaccount.google.com/lesssecureapps
    
    host = "imap.gmail.com"
    username = "username"
    password = 'password'
    download_folder = "/path/to/download/folder"
    
    if not os.path.isdir(download_folder):
        os.makedirs(download_folder, exist_ok=True)
    
    mail = Imbox(host, username=username, password=password, ssl=True, ssl_context=None, starttls=False)
    messages = mail.messages() # defaults to inbox
    
    for (uid, message) in messages:
        mail.mark_seen(uid) # optional, mark message as read
    
        for idx, attachment in enumerate(message.attachments):
            try:
                att_fn = attachment.get('filename')
                download_path = f"{download_folder}/{att_fn}"
                print(download_path)
                with open(download_path, "wb") as fp:
                    fp.write(attachment.get('content').read())
            except:
                pass
                print(traceback.print_exc())
    
    mail.logout()
    
    
    """
    Available Message filters: 
    
    # Gets all messages from the inbox
    messages = mail.messages()
    
    # Unread messages
    messages = mail.messages(unread=True)
    
    # Flagged messages
    messages = mail.messages(flagged=True)
    
    # Un-flagged messages
    messages = mail.messages(unflagged=True)
    
    # Flagged messages
    messages = mail.messages(flagged=True)
    
    # Un-flagged messages
    messages = mail.messages(unflagged=True)
    
    # Messages sent FROM
    messages = mail.messages(sent_from='sender@example.org')
    
    # Messages sent TO
    messages = mail.messages(sent_to='receiver@example.org')
    
    # Messages received before specific date
    messages = mail.messages(date__lt=datetime.date(2018, 7, 31))
    
    # Messages received after specific date
    messages = mail.messages(date__gt=datetime.date(2018, 7, 30))
    
    # Messages received on a specific date
    messages = mail.messages(date__on=datetime.date(2018, 7, 30))
    
    # Messages whose subjects contain a string
    messages = mail.messages(subject='Christmas')
    
    # Messages from a specific folder
    messages = mail.messages(folder='Social')
    """
    

提交回复
热议问题