Select to view content in your preferred language

How to: Alter text elements in multiple MXDs simultaneously using a Python script

299
4
Jump to solution
11-07-2024 09:26 AM
JaredPilbeam2
MVP Regular Contributor

https://support.esri.com/en-us/knowledge-base/how-to-alter-text-elements-in-multiple-mxds-simultaneo...

Is there an updated version of this for Pro? When I use the script with Pro-related updates there are no errors in the tool, but no changes are made. I'm currently debugging and the couple things I'm questioning whether they're actually doing their job are the counter and replace().

import arcpy
import os
arcpy.env.overwriteOutput = True

#workspace folder
arcpy.env.workspace = ws = arcpy.GetParameterAsText(0)
Old_Text = arcpy.GetParameterAsText(1)
oldList = Old_Text.split(', ') #set the comma as separator for multiple inputs
New_Text = arcpy.GetParameterAsText(2)
newList = New_Text.split(', ')

f = arcpy.ListFiles("*.aprx")
for aprxname in f:
    aprx_path = os.path.join(ws, aprxname)
    aprx = arcpy.mp.ArcGISProject(aprx_path)
    for lyt in aprx.listLayouts():
        for elm in lyt.listElements('TEXT_ELEMENT'):
            counter = 0
            for text in oldList:
                if text in elm.text:
                    elm.text.replace(text,newList[counter])
                    arcpy.AddMessage(f'{aprxname} | updated to: {elm.text}')
                    counter = counter + 1
                else:
                    counter = counter + 1
    aprx.save()

 

JaredPilbeam2_0-1731000143738.png

JaredPilbeam2_1-1731000210378.png

 

 

 

 

0 Kudos
1 Solution

Accepted Solutions
TonyAlmeida
Frequent Contributor

replace

elm.text.replace(text, newList[counter])

with

elm.text = elm.text.replace(text, newList[counter])

 

View solution in original post

4 Replies
TonyAlmeida
Frequent Contributor

replace

elm.text.replace(text, newList[counter])

with

elm.text = elm.text.replace(text, newList[counter])

 

JaredPilbeam2
MVP Regular Contributor

Thought you had something there. I changed that at some point while debugging and forgot to put it back the way it was. Didn't make a difference.

0 Kudos
TonyAlmeida
Frequent Contributor

Maybe try,

arcpy.mp.ArcGISProject(aprx_path)
    for lyt in aprx.listLayouts():
        for elm in lyt.listElements('TEXT_ELEMENT'):
            counter = 0
            for text in oldList:
                if text in elm.text:
                    elm.text = elm.text.replace(text, newList[counter])
                    arcpy.AddMessage(f'{aprxname} | updated to: {elm.text}')
                counter += 1

 

0 Kudos
JaredPilbeam2
MVP Regular Contributor

@TonyAlmeida After implementing your suggestion, I did some further debugging and found the problem. These two parameters require a space after the separator when imputing multiple values, and I wasn't doing that.

oldList = Old_Text.split(', ')
newList = New_Text.split(', ')

 

So, if you have a space in the split() function then you need to put it after the parameter value too. Also, make sure your old text matches your existing layout text element or else it won't be found.

JaredPilbeam2_0-1731009628059.png

Working in Pro 3.3.2

import arcpy
import os
arcpy.env.overwriteOutput = True

#workspace folder
arcpy.env.workspace = ws = arcpy.GetParameterAsText(0)
Old_Text = arcpy.GetParameterAsText(1)
oldList = Old_Text.split(', ') #split into a list, use comma and space as separator
New_Text = arcpy.GetParameterAsText(2)
newList = New_Text.split(', ')


f = arcpy.ListFiles("*.aprx")
for aprxname in f:
    aprx_path = os.path.join(ws, aprxname)
    aprx = arcpy.mp.ArcGISProject(aprx_path)
    for lyt in aprx.listLayouts():
        for elm in lyt.listElements('TEXT_ELEMENT'):
            counter = 0
            for text in oldList:
                if text in elm.text:
                    elm.text = elm.text.replace(text,newList[counter])
                    arcpy.AddMessage(f'{aprxname} | updated to: {elm.text}')
                    counter = counter + 1
                else:
                    counter = counter + 1
    aprx.save()

 

0 Kudos