Hello,
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
mxd.save()
del mxd, row, rows,
Thank you!