Dynamic Text using non-index layer

09-07-2021 08:52 AM
Labels (1)
New Contributor II


I have created a set of maps using data driven pages that are driven by a layer named "map_frame". I am trying to add text elements that will automatically update based on the attributes of a layer named "Boundaries" that are present within each data driven page. I understand that I can do dynamic text using data driven page attributes, but I need to pull attribute for the text from layers that are not driving the the data driven pages.

I followed the example in this Q/A posted in the community: Extending Dynamic Text

I was able to get the code to work, but the text elements did not update when I would navigate to the next data driven page in the series. I think this is due to the code pulling from the first attribute in the layer's table, but I don't know how to change the given code to adjust for my desired outcome. I'm new to Python and the arcpy.mapping module. Does anyone have any guidance?


ArcMap 10.8.1

Example Code:

import arcpy

# get the reference to the map document and the layer named "Boundaries" in the map document
mxd = arcpy.mapping.MapDocument("CURRENT")
mapLyr = arcpy.mapping.ListLayers(mxd, "Boundaries")[0]

# get the reference to the text elements in the map document. I have 2 text elements with the name ID and county
idElem = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "ID")[0]
countyElem = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "County")[0]

# create a cursor and grab the first row in the feature class for the layer specified above
rows = arcpy.SearchCursor(mapLyr.dataSource)
row = rows.next()

# set the text property of the text element equal to the value from the fields named "ID" and "County"
idElem.text = row.getValue("ID")
countyElem.text = row.getValue("County")

# save the map document and delete the reference to the row, cursor and map object
del mxd, row, rows,



Thank you!

0 Kudos
2 Replies
MVP Regular Contributor

@HayleySmith have you looked into setting a "page query" on your data? You'd need a field in the data where the dynamic text is taken from, and identify the pages that those features occur in. Then when you assign dynamic text, it will only display the value of the feature inside each map series.

..Maps with no limits..
0 Kudos
New Contributor II

Thanks @David_Brooks 

I attempted this, but it still displayed the first attribute in the table and not the attribute that was being displayed.

0 Kudos