ArcPy ArcMap – Modifying Text Box in Layout View

arcmaparcpydatelayoutstext-element

I have the following textbox in a layout view:

enter image description here

Is it possible to modify the text through ArcPy?

I made that box as text, and it has no function added to it:

enter image description here

I just want to change the date so that later I can export it as PDF.

Best Answer

It is possible to change the text. Use the ListLayoutElements to find the object you want. Get the text from the element. Find and replace the text, then push the changed text back to the element. Finally refresh the view and save the mxd.

If your textbox is tagged with a name, then finding the correct layout element is easier. I have attached an example where I am running a find and replace on an element names "iso9001" textbox properties with named element

def updateISO9001(find_text, replace_text):
    # create map document object instance
    mxd = arcpy.mapping.MapDocument("CURRENT")
    mxd.activeView = "PAGE_LAYOUT"

    # Build a dictionary of just the TEXT_ELEMENTS
    element_dict = {}
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
        element_dict[elm.name] = elm


    # Is the expected tagged elemnt found
    if "iso9001" in element_dict:
        # Replace the string
        theString = element_dict["iso9001"].text
        theString = theString.replace(find_text, replace_text)
        element_dict["iso9001"].text = theString
    # Refresh the layout so the changes show
    arcpy.RefreshActiveView()
    # remove map document object instance
    del mxd
Related Question