As part of one of the processes we run we receive an email (in Outlook) that has a csv attachment within a .msg attachment that we need to save down.
I'm trying to save the .csv file but cannot figure out how to get to the attachment in the attachment. I am also brand new to Python so go easy on me. I have made the query generic but obviously the below only saves down the first attachment - I tried creating a variable for att.Attachment but that didn't work. HELP!
So far I have this:
import win32com.client
from datetime import datetime
from pathlib import Path
import csv
import os
outlook = win32com.client.Dispatch('outlook.application').GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages=inbox.Items
messages.Sort("[ReceivedTime]", True)
for i in list(messages):
if "Hello" in str(i.subject):
for att in i.Attachments:
att.SaveAsFile(r'/C:/Users/el/Documents/' + att.FileName)
The Outlook object model doesn't provide any methods for processing attached files beside saving them to the disk. So, you can save attached files to the disk by using the Attachment.SaveAsFile method. Then you you can open the saved files by automating their target applications, for example, the .msg file can be run to be opened in the default application (Outlook). In that case you will get a new Inspector window opened because Outlook is singleton, only one application can be run at the same time on the system. For the .csv file Excel can be automated to deal with a file content.
Related
Is there a way to convert some MBOX files to individual EML files using Python?
I have a client that downloaded his emails from GMail as MBOX collection. But I need to convert them to individual EML files so I can upload them to his new email client/server - which does not accept MBOX files.
Thanks in advance for any feedback.
UPDATE:
I have no code yet. I am wondering if it's even possible to do this via a small app, if not I'll purchase one of those available online. Just thought it would be cool to try it myself first.
Also, from my understanding, an mbox is a collection of emails (.eml files) and a single email would be just an .eml file.
Using the ezgmail library, I am sending an E-mail with an Excel file attached.
I am able to send the E-mail with the attached Excel file, but when I open the Excel file from the sent E-mail, it seems it becomes corrupt for some reason because I get this message:
We found a problem with some content in 'attached_file.xlsx'. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes.
When I click "Yes" I get:
Excel cannot open the file 'attached_file.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.
The excel file that I attach opens fine from my desktop, or if I E-mail it manually. I get these prompts only after E-mailing it via my python script which leverages ezgmail. Does anyone have ideas?
I am currently trying to figure out how to grab attachments from emails in a specific folder. Every Thursday, an email will arrive in my weekly reports folder and I would like to have a script that will grab the attachments of the email and save it to a specific file location.
This is the code I have so far which I believe is correct to instantiate everything.
import win32com.client
import os
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case the inbox. You can change that number to reference
messages = inbox.Items
message = messages.GetFirst()
However, I am unsure of how to procede as I have been unable to find good documentation of this module. What would be the next steps in order to grab from a specific folder, in this case it is one below the inbox folder, and save the first email's attachments to a specific location?
If it makes any difference, there are 4 csv files in the weekly email
Thanks
I have a pandas dataframe "df" in the code. I want to be able to send df.to_csv('filename') as an attachment in the email.
How do I do that? I dont want to write the file in a temp location and then attach that in the email. Is there a way I could attach the csv within in-memory?
I am trying to download an excel file from a OneDrive location. My code works okay to get the file, but the file is corrupt (I get an error message):
import urllib2
data = urllib2.urlopen("enter url here")
with open('C:\\Video.xlsx', 'wb') as output:
output.write(data.read())
output.close()
print "done"
I use the guest access to the excel file so that I don't have to work with authentication. The resulting file seems to be 15KB, the original is 22KB.
I got it. The url has the format below:
'https://onedrive.live.com/view.aspx?cid=.....app=Excel'
So, all that I had to do was change "view" to "download" at that url, and used the code below:
import urllib.request
url = 'https://onedrive.live.com/view.aspx?cid=.....app=Excel'
urllib.request.urlretrieve(url, "test.xlsx")
You can't just download the Excel file directly from OneDrive using a URL. Even when you would share the file without any authorization, you'll probably still get a link to an intermediate HTML page, rather than the Excel binary itself.
To download items from your OneDrive, you'll first need to authenticate and then pass the location of the file you're after. You'll probably want to use the OneDrive REST API. The details on how to do that are documented on the OneDrive's SDK for Python GitHub page with some examples to get you started.