Dynamic Text in Layout in ArcGIS Pro

04-21-2022 04:47 AM
New Contributor II


I have a custom print service that prints only the selected polygon in Portal for ArcGIS's Web AppBuilder.

I want it to print a custom layout that has a dynamic text that only prints the value of the attribute of the selected feature. Meaning, for example, if I selected a polygon where the value of its OBJECTID is 3, I want the text to read "OBJECTID: 3", if I select another one, it gives another value, etc...

I'm using ArcGIS Pro to create the layout.

Any tips would be appreciated!



0 Kudos
2 Replies
Esri Regular Contributor


Do you want the text to appear 1) in a static location on the layout OR 2) as a label in the map as if you were labeling only selected features? 

Here is a solution for #1.  It first ensures there is only 1 selected feature. You may have a different scenario.

p = arcpy.mp.ArcGISProject('current')
m = p.listMaps('Map')[0]
l = m.listLayers('GreatLakes')[0]

#Ensure one feature is selected, then get value
numRows = int(arcpy.GetCount_management(l).getOutput(0))
if numRows == 1:
    table = arcpy.SearchCursor(l, ['OBJECTID'])
    for row in table:
        value = row.getValue('OBJECTID')

#Reference layout and set text value
lyt = p.listLayouts()[0]
t = lyt.listElements('TEXT_ELEMENT', "MyText")[0]
t.text = value


Depending on the details, for #2 an easier route might be to have a secondary layer for labeling and then apply a def query to that layer to match the selected features.  I guess this only works if a def query was used to create the selection in the first place.  Devil is in the details.

Jeff - Layout and arcpy.mp Teams


Here is code that will work with the easier scenario: static text location on a layout.  I first ensure there is only one selected feature.  Devil is in the details.

0 Kudos
New Contributor II

Thank you very much for your response. Yes, I want the text to appear in a static location on the layout. But, I don't completely understand, where I will incorporate this Python script you provided me. Would it be in the Printing tool Python script? I'm sorry, I'm just unsure what the workflow is to incorporate this script.

Any explanation would be appreciated!


0 Kudos