Replace TextElement

630
4
Jump to solution
07-29-2020 02:43 PM
JaredPilbeam2
MVP Regular Contributor

I have a text element that I can't seem to find. I'm using this support document: TextElement—ArcGIS Pro | Documentation  I can find it if I use the the text Name (Text 37), but in my case I have a multitude of maps and Text 37 might have irrelevant text in another map. So, in short I need to find and replace the Text.

Here's the script I'm using but it doesn't change the Text. I think maybe it's because "Lawrence M. Walsh Will County Executive" is in two lines. Can anyone see what the issue may be?

import arcpy, os

#workspace folder
arcpy.env.workspace = ws = r"pathto\TestFolder"
#print the number of aprx files in the workspace folder
aprxlist = arcpy.ListFiles("*.aprx")
print("Currently {0} maps to be updated.".format(len(aprxlist)))

#list the aprxs of the workspace folder
for aprxname in arcpy.ListFiles("*.aprx"):
    print("changing text: {}\t".format(aprxname))
    aprx = arcpy.mp.ArcGISProject(ws + "\\" + aprxname)

#replace title date that occurs in the document
    for lyt in aprx.listLayouts():
        for elm in lyt.listElements('TEXT_ELEMENT'):
            if elm.text == 'Lawrence M. Walsh': #text
                elm.text = 'Denise Winfrey' #new text
                print(elm.text)
    aprx.save()
    #do not include this delete statement inside the above loop or it will
    #delete the mxd object inside the loop. Make sure to dedent.
    del aprx
print("----done----")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor
t = 'Lawrence M. Walsh\nblah blah'

'Lawrence M. Walsh' in t

True

... sort of retired...

View solution in original post

4 Replies
DanPatterson
MVP Esteemed Contributor
t = 'Lawrence M. Walsh\nblah blah'

'Lawrence M. Walsh' in t

True

... sort of retired...
JaredPilbeam2
MVP Regular Contributor

Thanks Dan. That crossed my mind..

0 Kudos
HannahDean
New Contributor II

I'm having the same issue - I used the in function, and the script will run (it takes forever - I'm running it in arcpro using a notebook - i don't know why it's suddenly taking 10-15 minutes to run the lines below) - 

so, it completes running, but no text is actually changed in the layouts.  I even tried printing the results - again no dice, even though printing the results claims replacement has occurred, when i go to the layout, no replacement has occurred.

oldtext = "Intermountain Region Geographic Resources Division*"
newtext = "Park_Name"

#allow search of all layouts in this Current Project, allowing the code to overwrite
#NOTE: Replace pathway in first line of this block as needed, or specify "CURRENT" if you are in the project
aprx = arcpy.mp.ArcGISProject(r"O:\Professional Services\GIS Information\GIS\Documentation\Pro_Templates_2020\Pro_Templates_2020.aprx")
for lyt in aprx.listLayouts():
    for elm in lyt.listElements("TEXT_ELEMENT"):
        if oldtext in elm.text:
            elm.text = newtext
   
aprx.save()
del aprx


#THEN I TRIED IT FOR ONE LAYOUT AMONG THE LIST OF LAYOUTS
#TESTING CELL FOR ONE LAYOUT
oldtext = "Intermountain Region"
newtext = "Park_Name"
aprx = arcpy.mp.ArcGISProject(r"O:\Professional Services\GIS Information\GIS\Documentation\Pro_Templates_2020\Pro_Templates_2020.aprx")
lyt_1 = aprx.listLayouts()[0]
for elm in lyt_1.listElements("TEXT_ELEMENT"):
    print (elm.name + ":" + elm.text)
    if oldtext in elm.text:
        elm.text = newtext
    print (elm.text)
aprx.save()
del aprx 
0 Kudos
JaredPilbeam2
MVP Regular Contributor

Hannah,

I think maybe your problem is here:

        if oldtext in elm.text:
            elm.text = newtext

As the help doc points out, you have to find the text with the Equal To operator. It doesn't look like you tried that. Try this:

for lyt in aprx.listLayouts():
    for elm in lyt.listElements("TEXT_ELEMENT"):
        if elm.text == oldtext:
            elm.text = newtext
    ‍‍‍