Help with Python Toolbox

953
4
Jump to solution
07-19-2018 10:53 AM
MarkBinder
Occasional Contributor

I am developing a Python Toolbox to take some user inputs before creating an output that gets saved as an Excel file. I'm trying to have it add the '.xls' extension after the user enters a file name but then it shows errors in the dialog box:

The errors go away once the file name is entered and it does automatically add the file extension. How do I write the code so that there are no errors to begin with?

This is my first attempt at a Python Toolbox but I think I will be developing more so I'm willing to take any help that I can get. Here is my source code:

import arcpy

def getList():
    planList=['Greentree', 'Sunset','Sherwood']
    return planList

class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = "Toolbox"
        self.alias = ""

        # List of tool classes associated with this toolbox
        self.tools = [Tool]

class Tool(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Tool"
        self.description = ""
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""

        planBuff=arcpy.Parameter(
            name='planBuff',
            displayName='Buffer Distance',
            direction='Input',
            datatype='GPDouble',
            parameterType='Required')
        planBuff.value=50

        units=arcpy.Parameter(
            name='units',
            displayName='Units',
            direction='Input',
            datatype='GPString',
            parameterType='Required')
        units.filter.type = "ValueList"
        units.filter.list = ['feet','meters']
        units.value='feet'

        plan=arcpy.Parameter(
            name='plan',
            displayName='Plan Name',
            direction='Input',
            datatype='GPString',
            parameterType='Required')
        plan.filter.type='ValueList'
        plan.filter.list=getList()

        folder=arcpy.Parameter(
            name='folder',
            displayName='Folder to save file',
            direction='Input',
            datatype='DEFolder',
            parameterType='Required')

        fileName=arcpy.Parameter(
            name='fileName',
            displayName='Excel file name',
            direction='Input',
            datatype='GPString',
            parameterType='Required')

        params = [planBuff,units,plan,folder,fileName]
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""

        if not parameters[4].valueAsText.endswith('xls'):
            parameters[4].value=parameters[4].valueAsText+'.xls'

        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return

    def execute(self, parameters, messages):
        """The source code of the tool."""
        return
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Alum

Try this at line 80 in your updateParameters section (.endswith can't be used unless there is text in the parameter):

        if parameters[4].valueAsText is not None:
            if not parameters[4].valueAsText.endswith('xls'):
                parameters[4].value=parameters[4].valueAsText+'.xls'
‍‍‍

View solution in original post

4 Replies
DanPatterson_Retired
MVP Emeritus

have you tried a default name?

0 Kudos
MarkBinder
Occasional Contributor

Having a default filename worked but I'm not sure that I would like to go that route.

0 Kudos
RandyBurton
MVP Alum

Try this at line 80 in your updateParameters section (.endswith can't be used unless there is text in the parameter):

        if parameters[4].valueAsText is not None:
            if not parameters[4].valueAsText.endswith('xls'):
                parameters[4].value=parameters[4].valueAsText+'.xls'
‍‍‍
MarkBinder
Occasional Contributor

That did the trick. I combined both if statements into one if statement though.

0 Kudos