1

I have a large ArcPy script where 80 layouts (created from 1 template layout - "Layout_King") are created and exported into a PDF, but right now each layout has the same generic title so sometimes it is hard to know what data is being shown in the layout. This problem could be solved if the layout's title was Dynamic with the feature class that is being portrayed in the layout.

The following script is my first pass at achieving a layout title that would be "Dynamic" with a feature class' name:

import arcpy
from arcpy import env
import sys
import os

elm_name = "My Dynamic Title"
p = arcpy.mp.ArcGISProject(r"C:\arcGIS_Shared\Python\CenterHeatMaps.aprx")
for lyt in p.listLayouts("Layout_King") [0]:
    for elm in lyt.listElements("TEXT_ELEMENT"):
        if elm.name==elm_name:
            text_elm=elm
            break

for m in p.listMaps():
    for lyr in m.listLayers("BCBS*"):
        new_title=lyr.name
        elm.text=new_title
        lyt.exportToPDF(r"C:\arcGIS_Shared\Exports" + "\\BCBS" + elm.name[13:] + 
        ".pdf")
        print(elm.name[13:])

However, when I tested the code I got the following error:

Traceback (most recent call last):
  File "<string>", line 8, in <module>
TypeError: 'Layout' object is not iterable

What I'm doing wrong? Also, does it look like I'm at least on the right track with the code?

2

Yes, you can change any text element (title) within the layout, try something like this:

aprx = arcpy.mp.ArcGISProject(r"C:\Temp\myProject.aprx")
for lyt in aprx.listLayouts():
    for elm in lyt.listElements("TEXT_ELEMENT"):
        if elm.name == "title": # this is the element name set via text properties      
            # add your layout feature class check logic
            elm.text = "New Title" # changing title name

This example has title text element has a nema property set which we use to find the element then you change the value using the text() method.

  • This definitely looks like something close to what I need. However, it looks like, with the code you are suggesting, that I would have specify each layout title individually, and with 80 layouts that would take a long time. Do you know of a way for the Layout's title to take the same name as the feature class that is "on" or "active" (in the table of contents) automatically without user input? – Matt A. Dec 19 '17 at 15:10
  • Most likely, not sure what the logic of your script looks like. You could create a dictionary at the beginning of the script that has a key and value for each layout (key = layout name and value = title name). You could loop through the dictionary and use that as the way to update the elm.text to the current dict value. – artwork21 Dec 19 '17 at 15:39
  • I realized I never specified the fact that it is really just one layout, and it basically functions as a template for 80 PDFs – Matt A. Dec 19 '17 at 15:53
  • I would use the dictionary to look up against or to drive the export process. – artwork21 Dec 19 '17 at 16:01
1

I was able to get the results I was looking for using the following code:

import arcpy
from arcpy import env
import sys
import os
import datetime
elm_name = "My Dynamic Title" # the name you assign to the title (TEXT) element
p = arcpy.mp.ArcGISProject(r"C:\arcGIS_Shared\Python\CenterHeatMaps.aprx")
for lyt in p.listLayouts("Layout_King"): # get the element
    for elm in lyt.listElements("TEXT_ELEMENT"):
        if elm.name == elm_name:
            text_elm = elm
            break

for m in p.listMaps(): # loop through your layer / layouts / maps
    for lyr in m.listLayers("BCBS*"):
        new_title=lyr.name # do your logic with the layer
        elm.text=new_title
        lyt.exportToPDF(r"C:\arcGIS_Shared\Exports" + "\\BCBS_" + elm.name[13:] + ".pdf") # export the map
        print(elm.name[13:])

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.