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()
Solved! Go to Solution.
replace
elm.text.replace(text, newList[counter])
with
elm.text = elm.text.replace(text, newList[counter])
replace
elm.text.replace(text, newList[counter])
with
elm.text = elm.text.replace(text, newList[counter])
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.
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
@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.
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()