I had a request from the Asset Management team to display all the asset management data I had for a particular area, grouped by service category and with labels displaying their unique IDs. My challenge was to turn on the labels for all the layers using a specific expression. However, the ID fields were not named the same in all the layers (again, the joys of working with data received from outside).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|# @date 17/09/2014|
|# @author Cindy Williams|
|# Set the label field and switch labels on for|
|# all layers in a mxd.|
|# For use in the Python window in ArcMap.|
|mxd = arcpy.mapping.MapDocument("CURRENT")|
|for lyr in arcpy.mapping.ListLayers(mxd):|
|f = arcpy.ListFields(lyr, "*GIS*")|
|lyr.labelClasses.expression = "[" + f.name + "]"|
|lyr.showLabels = True|
|lyr.visible = True|
On line 19, I set the label expression for the default label class using the correct field name. The square brackets are what ArcGIS is expecting to delimit field names in the label expression dialog. Line 20 turns the labels on, while line 23 turns the layer on.