I am trying to create a tool that will let me rename multiple layouts one after the other, ideally by replacing just a portion of the name. For example, "City_north_map_projects" and "City_north_map_sites" would become "City_south_map_projects" and "City_south_map_sites". So far, I have tried a number of things. This is the code for the latest attempt:
import arcpy
# Set Variables
# aprx = arcpy.mp.ArcGISProject("CURRENT") # For tool use
aprx = arcpy.mp.ArcGISProject(r"PATH TO THE APRX/APRX.aprx") # Development use. Has 3 layouts: Test_layout, Test_layout1, Test_layout2
for lyt in aprx.listLayouts(): # for each layout in the project
print(lyt.name) # print the name
oldName = lyt.name # write layout name to variable
newName = oldName.replace("Test", "Best") # replaces old text with new
print(newName) # everything works through this line
# lyt.name = newName # Doesn't actually rename the layout? Commented out to try arcpy.Rename
arcpy.Rename_management(oldName,newName) # Fails here with error 000732
I am sure there is something simple, but I am *very* new at Python.
Solved! Go to Solution.
I ended up using a modified version of my original code, but in the ArcGIS Pro Python window:
aprx = arcpy.mp.GISProject("CURRENT") # The current project
for lyt in aprx.listLayouts(): # For each layout in the project
oldName = lyt.name # Gets the name
print(oldName) # For progress checking
nameSplit = oldName.split("_", 1) # Splits the name into 2 parts at the _
keepPart = nameSplit[1] # Saves the 2nd part to be joined to the new part
newName = f"RC1222_1223_{keepPart}" # Constructs new name
print(newName) # For progress checking/troubleshooting
lyt.name = newName # Sets the layout name to the new name
The layout names are things like "RC1222_map_projects_new", and the part I wanted to change was the first part, thus the .split(). This worked like a champ in the Python window.
I'm still exploring how to work this in a Script tool.
If you google that error number (esri error 000732) you'll find https://support.esri.com/en/technical-article/000010149 which just describes what's going on. Looking at the arcpy.Rename_management() method help, it appears to work for actual data sets rather than a layout object.
I'm not sure what would work for renaming a layout, but I've got a little bit of time before I'm off to zoom-meeting land so I'll keep digging. Perhaps someone else knows?
I suspected that was the case. I had seen an instance where someone was using Rename() to change folder names, and though it might work with layouts. 🙂
Thanks!
I ended up using a modified version of my original code, but in the ArcGIS Pro Python window:
aprx = arcpy.mp.GISProject("CURRENT") # The current project
for lyt in aprx.listLayouts(): # For each layout in the project
oldName = lyt.name # Gets the name
print(oldName) # For progress checking
nameSplit = oldName.split("_", 1) # Splits the name into 2 parts at the _
keepPart = nameSplit[1] # Saves the 2nd part to be joined to the new part
newName = f"RC1222_1223_{keepPart}" # Constructs new name
print(newName) # For progress checking/troubleshooting
lyt.name = newName # Sets the layout name to the new name
The layout names are things like "RC1222_map_projects_new", and the part I wanted to change was the first part, thus the .split(). This worked like a champ in the Python window.
I'm still exploring how to work this in a Script tool.