rename layers displayed on TOC

14948
13
Jump to solution
01-10-2012 05:27 AM
FugroSurvey_Ltd_
New Contributor II
I???m brand new to Python so I???m not sure if what I want to do is possible or not and I apologise for my lack of technical terms.

Basically I need to export a few files from ArcMap to CAD on a daily basis. As part of company procedures my CAD layers need to have a specific name which is different from the original GIS layer name. The problem is that the Export to CAD tool picks up the name displayed on the TOC to create levels' name in CAD.
Having said that what I'm looking for is to make a script to rename all the layers on my TOC. Is this possible?

I appreciate any help or pointers in the right direction.

Thanks

PT
Tags (2)
0 Kudos
13 Replies
Raphael_Augusto_FoscariniFerre
Occasional Contributor

Is there a way to change the 'label' of the layerfile? The script above, changes the layer name. But i want to change the layer name. I'm refering to the name is written right after the symbol, in the TOC.

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

since you posted this question https://community.esri.com/message/673130-re-how-can-i-change-the-layer-label-in-toc-with-py you may want to delete your comment/question above.  If geonet doesn't allow you to, I might be able to do it for you.

0 Kudos
Raphael_Augusto_FoscariniFerre
Occasional Contributor

i'm just trying to get as many options as possible.

0 Kudos
Raphael_Augusto_FoscariniFerre
Occasional Contributor

I figured how to do it. I just need to change it, the way i want one time, than save a .lyr from the correct information (it can also change symbology, and other stuff).

The script below, will apply the .lyr file to your layer in the TOC. You just must specify the name of the layer, like displayed in TOC.

It will do to all MXD files, in the same folder of the script.

import arcpy, os
from arcpy import env  
env.workspace = os.curdir

  
for mxdFile in arcpy.ListFiles("*.mxd"):  
    mxdPath = env.workspace + "\\" + mxdFile  
    mxd = arcpy.mapping.MapDocument(mxdPath)    
    layers = arcpy.mapping.ListLayers(mxd)    
        
    for lyr in layers:    
        if lyr.name == "layer_name_in_TOC": #change here
            print mxdPath
            symbologyLayer = r"D:\LayerFile.lyr" #change here
            arcpy.ApplySymbologyFromLayer_management (lyr, symbologyLayer) 

            arcpy.RefreshTOC()
            mxd.save()