Python Forum
Save image from outlook email - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Save image from outlook email (/thread-40131.html)



Save image from outlook email - cubangt - Jun-07-2023

So i have the following code that works great, updates my excel file and saves the file as expected.. BUT because we are merely updating the one excel file with the latest image, we are now wanting to archive the images when this script runs.

how can i modify this code to save off the image into the same folder with the date as the name so we can go back and look at past charts?

    # CHECK IF MESSAGE IS TODAY SO THAT THE MOST CURRENT SCREENSHOT IS UPDATED IN THE FILE.
    if edDate == tdDate:
        attachments = message.Attachments
        # THIS ENSURES THAT THE EMAIL HAS AT LEAST 2 ATTACHMENTS BEFORE IT TRIES TO DOWNLOAD FILES THAT DONT EXIST    
        if attachments.count >= 1:
            
            #LOOP THRU ALL ATTACHMENTS AND ONLY DOWNLOAD CSV FILES
            for att in attachments:
               p = Path(str(att).lower())
               
               newName = "{0}_{1}".format(p.stem,p.suffix)
               
               # CHECK IF ITS A PNG FILE BEFORE ATTEMPTING TO DOWNLOAD
               if p.suffix == ".png":
                  imgFileLoc = "C:\PricingVolume"+ '\\' + newName
                  # DOWNLOAD CSV FILE ONLY WITH THE NEW FILE NAME
                  figSaved  = att.SaveASFile(imgFileLoc)
                  #print(att)
                  # THIS IS THE MASTER EXCEL FILE THAT IS UPDATED AT THE END WITH THE FILTERED DATA
                  wrkb = openpyxl.Workbook()
                  ws = wrkb.worksheets[0]
                  img = openpyxl.drawing.image.Image(imgFileLoc)
                  img.height = 757
                  img.width= 1533
                  img.anchor = 'A1'
                  ws.add_image(img)
                  wrkb.save(PeakPricingVolumeFile)



RE: Save image from outlook email - cubangt - Jun-07-2023

Disregard Big Grin

Not sure how to delete or mark this complete, i completely missed that the image was already being saved, just need to update to include the date in the name so we have daily image back ups.