Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.

Error while importing openpyxl package in python tool

saachitalwar
8 - Asteroid

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. 

5 REPLIES 5
nickmartella
7 - Meteor

I would try writing "pip install openpyxl" inside the python tool itself in a separate workflow and run it - that may fix this problem. 

saachitalwar
8 - Asteroid
File "<ipython-input-2-b10d2cebd184>", line 3
    pip install openpyxl
        ^
SyntaxError: invalid syntax

 

this error is popping up when I ran this in a different python tool

Note: you may need to restart the kernel to use updated packages.
 
'c:\program' is not recognized as an internal or external command,
operable program or batch file.

 this is the error if I run this in the same tool as the rest of the code

nickmartella
7 - Meteor

Ah ok, do you have access to your computers command prompt or powershell? I would open one of those and try "pip install openpyxl" in there instead. If you dont have access, you can re try in the python tool with:

 

import os
os.system('pip install openpyxl')
apathetichell
19 - Altair

close alteryx.

reopen alteryx using run as admin

drag a python tool to your canvas

 

read this section:

# List all non-standard packages to be imported by your
# script here (only missing packages will be installed)
from ayx import Package
#Package.installPackages(['pandas','numpy'])

 

uncomment the Package.installpackages line and write:

Package.installPackages('openpyxl')

 

run workflow.

exit alteryx in run as admin.

return to your original workflow.

PhilipMannering
16 - Nebula
16 - Nebula

Inside the Jupyter Notebook

!pip install openpyxl

...should also work.

Labels
Top Solution Authors