I have an mxd with multiple titles and I'm trying to write a script that iterates a process that activates a layer, changes one of the titles, then exports it. I've got it all working except for changing the title. I can change the title but because there are multiple titles, it changes all of the titles to the same thing. Anyone know of a way to just target one of the titles within a mxd layout?

import arcpy

inputList = arcpy.GetParameterAsText(0)
bckGrndLyrs = arcpy.GetParameterAsText(1)
outputLocation = arcpy.GetParameterAsText(2)

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]

for lyrs in arcpy.mapping.ListLayers(mxd, "", df):
    if lyrs.name in inputList:
        lyrs.visible = False
    elif lyrs.name in bckGrndLyrs:
        lyrs.visible = True  
        lyrs.visible = False

for inFC in arcpy.mapping.ListLayers(inputList, "", df):
    if inFC.name in inputList:
        arcpy.AddMessage("Activating " + inFC.name + " Layer")
        inFC.visible = True

        for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
          mxd.dataDrivenPages.currentPageID = pageNum
          for titleItem in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
            arcpy.AddMessage("...Changing title to " + inFC.name + " " + str(pageNum))
            titleItem.text = inFC.name 
          arcpy.AddMessage("......Refreshing Dataframe")
          arcpy.AddMessage(".........Exporting " + inFC.name + " Page " + str(pageNum) + " as PDF")
          arcpy.mapping.ExportToPDF(mxd, outputLocation + " " + inFC.name + " " + str(pageNum) + ".pdf")
        inFC.visible = False

del mxd
  • What have you tried? Please edit your question and include a snippet of the code you've tried that isn't working. – Midavalo Jun 11 '16 at 2:45
  • Hey Midavalo, I added the script to the question. – S. Price Jun 11 '16 at 2:52

You can name your Text Elements (double-click on your title element, click the Size and Position tab, and in Element Name field, type in a unique name).

enter image description here

Then in your script, you can reference that unique name in order to modify the specific title text element.

for titleItem in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
    if titleItem.name == 'MyUniqueName':
        arcpy.AddMessage("...Changing title to " + inFC.name + " " + str(pageNum))
        titleItem.text = inFC.name 

Then you're only modifying the title that matches the element name.

See Text Element - ArcGIS Desktop Help for more info.

  • 1
    This is what I call "pseudo-dynamic text" and is an invaluable coding pattern when doing On Demand PDF Maps. – PolyGeo Jun 11 '16 at 4:27

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.