Issue
I created this code that goes inside outlook in some subfolders, and I want to transform the attachments, that are csv files, into a pandas dataframe in order to append it after in a list.
But I believe python isn't able to read the attachments as csv ?!? Bit lost here
import win32com.client
from win32com.client import Dispatch
import pandas as pd
list_dataframes=[]
list_dataframes_names=[]
outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
root_folder = outlook.Folders.Item(1)
folder = root_folder.Folders['Project'].Folders['CLIENT XXX']
for item in root_folder.Folders['Inbox'].Items:
if 'Client XXX' in str(item.subject):
for attachment in item.Attachments:
list_dataframes_names.append(attachment.FileName)
pandas_data_frame=pd.read_csv(attachment, sep=';',encoding='latin-1', on_bad_lines='skip')
list_dataframes.append(pandas_data_frame)
the error comes from this line of code:
pandas_data_frame=pd.read_csv(attachment, sep=';',encoding='latin-1', on_bad_lines='skip')
ValueError: Invalid file path or buffer object type: <class 'win32com.client.CDispatch'>
Is there a way to make this work? or some workaround? I don't want to download the files to a local folder and after upload then, I believe that would work, but that's last resource.
Solution
Attachment.FileName
is just that - the file name (e.g. "myfile.txt"), there is no path component. It does not exist on the file system, hence you cannot read it as a file. You need to save it as a file first (Attachment.SaveAsFile
) specifying the full path and the file name, and only after that you can read the file contents.
Answered By - Dmitry Streblechenko
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.