1

I have a script that cycles through features based on attributes and makes a PDF centered around each feature. I would like to be able to include both a dynamic text element for the date as well as a title that is derived from the python script. I'm currently using the arcpy.mapping.ListLayoutElements(thisMap, "TEXT_ELEMENT") command and then updating the text element for each iteration of the loop. The problem is that not only does the title get changed but the dynamic date also gets changed to the text meant for the title. How would I construct the code to only target a specific text element instead of all of the text elements in the map? I have tried using the wildcard, but the problem with this is that the text gets changed from every previous iteration, so the wildcard will only identify the text from the first iteration.

with arcpy.da.SearchCursor(fc, ("SHAPE@", "UNIQUE_ID")) as searchCur:
for row in searchCur:
        myDF.extent = row[0].extent
        myDF.scale = mapScale

        for elm in arcpy.mapping.ListLayoutElements(thisMap, "TEXT_ELEMENT"):
            elm.text = str(row[1])

        arcpy.RefreshActiveView()
        arcpy.mapping.ExportToPDF(thisMap, str(pdfLoc) + "\Map_" + str(row[1]) + ".pdf", resolution=300, image_quality="Best")   #***test output location variable structure
        arcpy.AddMessage("Output --->  Map_" + str(row[1]) + ".pdf was created successfully!")
        arcpy.AddMessage(str(cycleCount) + "/" + str(recordCount))
        cycleCount += 1
3

You should name your elements in the map so that they can be referenced by the element.name property. See the text element docs

for elm in arcpy.mapping.ListLayoutElements(thisMap, "TEXT_ELEMENT"):
    if elm.name == 'TestName'  #reference by named element here
        elm.text = str(row[1])  #do work here

enter image description here

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.