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

5162
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
1 Solution

Accepted Solutions
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()

View solution in original post

14 Replies
IanMurray
Frequent Contributor

I'm fairly certain that a layer label value is not exposed with the arcpy module and therefore cannot be changed with arcpy.

0 Kudos
WilliamKalande
New Contributor III

Hi Ian, Here a year later. Can I edit labels so that I allocate a field in a layer manually or through arcpy? It is denoted as read/write but cant edit it other than through TOC.

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

I wonder if you can use UniqueValuesSymbology—Help | ArcGIS for Desktop  but "trick" the unique value to only have one.  then use one of these properties? 

I didn't see a set of properties for no-unique-values otherwise.

0 Kudos
Raphael_Augusto_FoscariniFerre
Occasional Contributor

The problem is, i need to change it in several mxds. More than 1000... And it would make changes in the legend in layout also, because would add 'headings', for example...

Thats why i want to try this way!! but thanks for the help...

0 Kudos
IanMurray
Frequent Contributor

I doubt it would change your legend unless your legend item for that layer was already using a style item that used the layer heading.  You could always change your legend item style to use only the layer name, or layer name and label only if you didn't want an heading added to them.

0 Kudos
IanMurray
Frequent Contributor

Sheesh I'm slipping.... I remember looking at that about a month ago and reminding myself to use that if needed sometime.

0 Kudos
Raphael_Augusto_FoscariniFerre
Occasional Contributor

Please, try to remember it!!! i really need to change this automatically!!!

help me!

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

What version of ArcMap are you using?  What is your current code and result?

I have ArcMap 10.6, and I can change the label class to point to a different field and it works fine:

>>> mxd = arcpy.mapping.MapDocument("CURRENT")
>>> lyr = arcpy.mapping.ListLayers(mxd)[0]
>>> lbl = lyr.labelClasses[0]
>>> lbl.expression = '[NEW_FIELD]'
>>> arcpy.RefreshActiveView()
>>> 
0 Kudos
WilliamKalande
New Contributor III

Thanks Joshua

I applied the code, it changes the expression label field as below and that is fine

What I need though is the TOC to maintain the value field assigned under symbology properties but allow me to use another field in the layer as a label or to copy the corresponding values into TOC. What I mean I want for instance the values below in the OpCodeBoundaries. The value field set in symbology is say field A. I want to maintain that but change the display in the TOC window to display values in Field B. Effectively, that will change the display in the legend

0 Kudos