Select to view content in your preferred language

pd.read_excel gets error when script tool runs

1630
8
Jump to solution
03-07-2023 06:08 PM
Aнастасия88
Regular Contributor

Hello people,

I cam across an error, 

AttributeError: ValueObject: Get attribute __class__ does not exist

 when I run the script tool, I will get the error for the line, "df = pd.read_excel(spreadsheet)"

This simple code to read the spreadsheet has not problem in Python IDE but it will fail with the error in script tool.

As I have not figure out the error message, any advice is much appreciated.

Thanks in advance for your help!

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

if you are providing the path, did you try

spreadsheet = arcpy.GetParameterAsText(0)

... sort of retired...

View solution in original post

8 Replies
DanPatterson
MVP Esteemed Contributor

Have you specified the full path to "spreadsheet".

perhaps the script tool hasn't specified the workspace/folder where it is  to be found


... sort of retired...
0 Kudos
Aнастасия88
Regular Contributor

Thanks for our reply!

I am having this problem when running script tool on ArcGIS Pro. The full path is to be defined via the tool with parameter setting as below.

A0328_0-1678246462790.png

And the code for the script tool is simple as below.

import pandas as pd
import arcpy

def ScriptTool(spreadsheet):
    arcpy.AddMessage('Start')
    df = pd.read_excel(spreadsheet)
    name_values = df["Name"].unique()
    query = "Name IN {}".format(tuple(name_values))
    arcpy.AddMessage(query)

if __name__ == '__main__':
    spreadsheet = arcpy.GetParameter(0)
    ScriptTool(spreadsheet)

 

0 Kudos
Mahdi_Ch
Regular Contributor

I'm new to Arcpy, but from scripting point of view, I can suggest you to put a print statement after this line 

    spreadsheet = arcpy.GetParameter(0)

 to print(spreadsheet) and double check what you are passing to pandas.read_excel(). This should be either a str or path object, maybe arcpy.GetParameter(0) is not returning what you expect. 

0 Kudos
Aнастасия88
Regular Contributor

Thanks Mahgi_Ch for your reply!

It was a just my careless mistake in the code.

0 Kudos
DanPatterson
MVP Esteemed Contributor

if you are providing the path, did you try

spreadsheet = arcpy.GetParameterAsText(0)

... sort of retired...
Aнастасия88
Regular Contributor

Thanks Dan for pointing out!

That was my careless mistake, I excluded AsText somehow - it works now.

Apart from that, I have a question. A code using GetParameter(0) works in both Python window and Python IDE. But this does not work it the code is embedded in a Script tool. Do you know why this occurs?

0 Kudos
DanPatterson
MVP Esteemed Contributor

I am not sure, I use custom toolboxes rather than python toolboxes, so I can specify my parameter types there and I always use GetParameterAsText and let arcpy's behind the scenes handle any stuff from arcobjects conversions


... sort of retired...
0 Kudos
Aнастасия88
Regular Contributor

I see, thanks Dan!

0 Kudos