How can i change the layer label in TOC with py ?

5178
14
Jump to solution
03-15-2017 12:33 PM
Raphael_Augusto_FoscariniFerre
Occasional Contributor

I want to change this information, with python:

I have this code (got here: https://community.esri.com/thread/43148 ) , but it changes the layer name. I want to change layer label.

It is a simple feature, with no unique values.

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 == "old name":
            lyr.name = "newname"

    arcpy.RefreshTOC()
    mxd.save()‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Tags (2)
0 Kudos
14 Replies
JoshuaBixby
MVP Esteemed Contributor

Honestly, I am not certain what you are after.  It seems like you are mixing symbology and labeling.  Labels are displayed on the map, not in the TOC or a map legend.  You can symbolize and label based on the same field/values; but if I understand what you are trying to do, it isn't possible, at least through ArcPy.  That said, I might not understand what you are trying to accomplish.

0 Kudos
WilliamKalande
New Contributor III

You got me right Joshua. I want to allocate a field to a layer's label in symbology. That effectively changes the layer labels displayed in TOC. Not sure if it is possible. If it is it will save me a  lot of time.

Alternatively, I can resolve the issue by finding a way to write into the TOC or copy field attributes into the TOC. Currently, I am copying and pasting a single field attribute at a time

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

For ArcMap and ArcPy, definitely not possible.  Beyond ArcMap, I still need to think about it.

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()
JoshuaBixby
MVP Esteemed Contributor

From your screenshot, it appears you are using ArcMap.  If you are using ArcGIS Pro, there is a SimpleRenderer class you can use to simply and straightforwardly change a symbol layer for simple symbology.

0 Kudos