I'm attempting to take a user input in a script tool to modify the text within text elements on a layout. The following is the code I am using:

aprx = arcpy.mp.ArcGISProject(r"S:/Initiatives/Mailings/MailOuts.aprx")
lyt = aprx.listLayouts("Buffer Map")[0]

mapType = lyt.listElements("TEXT_ELEMENT", "Map Type")[0]
mapType.text = arcpy.GetParameterAsText(0)

location = lyt.listElements("TEXT_ELEMENT", "Location")[0]
location.text = arcpy.GetParameterAsText(1)

schedule = lyt.listElements("TEXT_ELEMENT", "Schedule")[0]
schedule.text = arcpy.GetParameterAsText(2)

I don't receive an error when I run the script tool but the text elements remain empty when given user input.


There's 1 problem in your tool for sure. You don't actually save the changes you've made. If the code you've supplied is the entire tool, you're operating on a APRX on disk (not the active session). As such, your code could possibly be working 100% correctly, however when you open the APRX after the tool has run, you don't see the changes because you didn't save it.

You can try adding this as the last line:


or just aprx.save().

The other possible issue is your code is not actually finding the elements. For example, if you don't have a TEXT_ELEMENT named Map Type, the setting of the mapType.text property isn't going to do anything. (Although I'd expect if it didn't find anything, you'd get an error trying to do this step). I bring up this point as you may want to add some AddMessage to help understand the behavior. For example:

mapType = lyt.listElements("TEXT_ELEMENT", "Map Type")[0]
arcpy.AddMessage("The Map Type element is currently: {}, and will be updated to: {}".format(mapType.text, arcpy.GetParameterAsText(0))
mapType.text = arcpy.GetParameterAsText(0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.