1

I work with several MXD files. In each map there a polygon layer with unique values:

enter image description here enter image description here enter image description here

I try to change in the table of contents the "a" into "land use" by using this code:

import arcpy,os,sys
from arcpy import env

env.workspace = r"G:\desktop\Project"
for mxdname in arcpy.ListFiles("*.mxd"):
    print mxdname 
    mxd = arcpy.mapping.MapDocument(r"G:\desktop\Project\\" + mxdname)
    df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] 
    lyr = arcpy.mapping.ListLayers(mxd, "*")[0]
    if lyr.symbologyType == "UNIQUE_VALUES":
        vals = lyr.symbology.classLabels
        for v in vals:
            if v == "a":
                print lyr.name
                v = 'landUse'
                print v   
    mxd.save()
del mxd 

I just want to change the name in the table of content, and not change the attribute table value. When running this code i get this result and with no change in the maps:

>>> 
project.mxd
polygon
landUse
>>>    

1 Answer 1

3

To update the labels, you'll need to update them all at once by providing a list to lyr.symbology.classLabels.

if lyr.symbologyType == "UNIQUE_VALUES":
    vals = lyr.symbology.classLabels
    newLabels = []
    for v in vals:
        if v == "a":
            v = 'landUse'
        newLabels.append(v)  # push each label into the list
    lyr.symbology.classLabels = newLabels  # apply the new labels
mxd.save()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.