Hi,
I have written the code to download a csv file from outlook mailbox and save it in a desired location. I need to convert it into an xlsx file in order to perform some alteryx operations on it. The reason for this conversion is because the csv file is giving the error "Too many fields in record#1".
The code till downloading the csv file is working perfectly fine. When I try to convert it into xlsx, I have used the openpyxl package but it can't be imported.
from ayx import Package
from ayx import Alteryx
import openpyxl
import pandas as pd
import win32com.client
import os
print("hello
")
def getFileExtension(attachment_name):
return attachment_name.lower().split('.')[-1]
def save_outlook_attachments(folder_name, subject_line, save_location, attachment_types):
outlook_application = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
outlook_mailbox = outlook_application.Folders.Item(2)
print(outlook_application)
print(outlook_mailbox)
#this is default mailbox. if there are multiple mailboxes in outlook try increasing this number (2,3,4 etc.)
#finds the correct outlook folder by searching for a folder that is equal to folder_name
for folder in outlook_mailbox.Folders:
if folder.Name == folder_name:
found_folder = folder
print('Folder Searched:' + found_folder.Name)
# sorts all emails in folder by date (descending)
folder_emails = found_folder.Items
folder_emails.Sort("[ReceivedTime]", True)
# looks for correct email by searching for an email containing subject_line
for email in folder_emails:
if subject_line in email.Subject: #if exact subject_line match is needed, switch "in" to "=="
print('Found email with subject ' + email.Subject + ' sent on ' + email.SentOn.strftime('%m-%d-%y'))
found_email = email
break
# gets email attachment from email and saves to file location
num_email_attachments = len([x for x in found_email.attachments])+1
for attachment_num in range(1, num_email_attachments):
attachment = found_email.attachments.Item(attachment_num)
attachment_name = str(attachment)
file_extension = getFileExtension(attachment_name)
if (file_extension in attachment_types):
print('Attachment Number ' + str(attachment_num) + ' - File Name : ' + attachment_name)
save_file_path = os.path.join(save_location, attachment_name)
attachment.SaveAsFile(save_file_path)
print('Attachment ' + str(attachment) + ' saved to ' + save_file_path)
df = pd.read_csv(save_file_path)
new_file_name = str(attachment_name.split(".")[0]) + ".xlsx"
df.to_excel(new_file_name, index = False)
folder = "Inbox"
subject = "Hello"
location = r"\\path\Input"
save_outlook_attachments(folder_name = folder, subject_line = subject, save_location = location, attachment_types = ['csv'])
The error is in this line - df.to_excel(new_file_name, index = False) as follows -
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-4-f5ea1cbb6934> in <module>
----> 1 import openpyxl
ModuleNotFoundError: No module named 'openpyxl'
Any help is appreciated.
PS - when I save the file, the python code gets deleted back to default python tool settings.