Head banging against a wall...
I have figured how to do this for layer name. The Help Docs say it is possible to change classLabel but don't show any examples and I can't find anyone who has successfully done this through Python. I am sure the answer lies in arcpy.mapping and UniqueValuesSymbology but cannot figure it. I have 10-15 different layers, all using unique_values for symbology, consisting of about 15-20 different classes in each. Each class value and therefore label uses an "_" instead of a space as is common practice for GIS data. For legend and TOC purposes, I am trying to figure a script that can quickly manipulate and change the classLabels in Layer Properties.
Any ideas would be wonderful...thanks!
Solved! Go to Solution.
For inserting scripts, i.e., syntax highlighting, you need to use the advanced editor that is accessed from a link in the upper-right corner of the reply window/frame.
Try the following:
for lyr in arcpy.mapping.ListLayers(mxd): if lyr.symbologyType in ["GRADUATED_COLORS", "GRADUATED_SYMBOLS"]: labels = lyr.symbology.classBreakLabels for i, label in enumerate(labels): labels = label.replace("_", " ") lyr.symbology.classBreakLabels = labels elif lyr.symbologyType in ["UNIQUE_VALUES"]: labels = lyr.symbology.classLabels for i, label in enumerate(labels): labels = label.replace("_", " ") lyr.symbology.classLabels = labels
The following code from the interactive Python window in ArcMap should help get you going:
mxd = arcpy.mapping.MapDocument("CURRENT") for lyr in arcpy.mapping.ListLayers(mxd): if lyr.symbologyType in ["GRADUATED_COLORS", "GRADUATED_SYMBOLS"]: print lyr.symbology.classBreakLabels elif lyr.symbologyType in ["UNIQUE_VALUES"]: print lyr.symbology.classLabels
The classBreakLabels and classLabels properties are read-writeable and store lists. After you get the list, update the contents, and set the properties again; you will need to run arcpy.RefreshActiveView() to see the changes.
Joshua,
So it did grab and create the list from the available classLabels. How do I then go about replacing the underscores with spaces though. You say "update the contents, and set the properties again", what does that mean? I am an admitted Python amateur and quite frankly still lost. This is where I'm at (couldn't figure out how to add script like you did to the discussion):
Again...thanks for any advice!
For inserting scripts, i.e., syntax highlighting, you need to use the advanced editor that is accessed from a link in the upper-right corner of the reply window/frame.
Try the following:
for lyr in arcpy.mapping.ListLayers(mxd): if lyr.symbologyType in ["GRADUATED_COLORS", "GRADUATED_SYMBOLS"]: labels = lyr.symbology.classBreakLabels for i, label in enumerate(labels): labels = label.replace("_", " ") lyr.symbology.classBreakLabels = labels elif lyr.symbologyType in ["UNIQUE_VALUES"]: labels = lyr.symbology.classLabels for i, label in enumerate(labels): labels = label.replace("_", " ") lyr.symbology.classLabels = labels
Joshua,
You are the MAN! Had to substitute some single quotes for the doubles you had in the replace areas, added it to my script for renaming the layers with a "arcpy.RefreshTOC()" and presto! Thanks so much for the help!
Your welcome. Keeping plugging away at learning Python, I know I have really enjoyed learning and applying it, there is so much you can do with it. Cheers....