In this article you will learn how to modify the name of a specific layer stored in several MXD files using Python.
It often happens that during the execution of a project in which different graphic outputs and/or maps have been made that for some reason it becomes necessary to modify the name of a layer used to represent the spatial data (Shapefile, feature Class, among others) and that in turn appears in the dynamic object legend of the graphic outputs and/or maps, either by mistake when naming the layer, taste of the end customer, your immediate boss or because there have been changes in the concept that represents the layer, misspellings, among the reasons that I can glimpse at this time. The traditional solution would be the following:
How to modify the name of an ArcGIS / ArcMap layer?
1) Open the MXD file.
2) Double click on the layer you want to rename, and type the correct name.
3) Save the MXD file.
4) Repeat the procedure for each MXD file.
In case the correction must be done to one or two files it is not a big deal, but if the correction must be done on dozens of images and/or maps this can consume a lot of your time, next, I will show you how it can be much more efficient using Python code (Don't worry you don't need to know how to program to do it).
How to batch rename an ArcGIS / ArcMap layer using Python?
Step 1: Open a blank ArcGIS file.
Step 2: Open the Python window in the Standard toolbar.
Step 3: Backup the original MXD files.
Step 4: Copy the following code into a notepad and in line #02 of the code replace the path C:\EJEMPLO\EJEMPLO with the path where the MXD files to be modified are stored.
Step 5: In line #06 of the code replace DATAFRAME with the name of the dataframe containing the layer to be modified (It is a good practice that if there is more than one dataframe within a MXD file, to name each one uniquely).
Step 6: In line #07 of the code replace the text Nombre_inicial by the previous name of the layer to modify.
Step 7: In line #08 of the code replace the text Nombre_final by the new name of the layer to modify.
Step 8: Copy the modified code into the Python window and press the Enter key twice.
After a few seconds you will see how the modification date of the ArcMap files starts to update, and when you open it you will notice the update in the layer name in each MXD file.
I hope you enjoyed this article, soon I will upload a video explaining the procedure that you can consult in this blog. Save my blog among your favorite links, I will be uploading many more tricks of this style, remember that you can send me your concerns in the contact page or leave your comment, I will be attentive to respond.


