Wednesday, September 28, 2011

Turning on labels through ArcPy

The Layer object in the ArcPy module allows python developers to turn on labels of a feature class on and off at run time.  This is especially helpful if you are exporting maps and you want to control what Layers are labeled.
Example: Turning on Labels
import arcpy
from arcpy import mapping
fc = r"c:\temp\fc.shp"
tempLayer = "tempLayer"
arcpy.MakeFeatureLayer_management(fc, tempLayer)
#   Make the Layer Object
#
layer = mapping.Layer(tempLayer)
layer.showLabels = True
#   Show the changes in ArcMap
#
arcpy.RefreshActiveView()
del layer 


Enjoy

2 comments:

jorge1187 said...

It works, but how can I change the label field which shows?, I mean when i do that, it just turn on the label for default but doesn't show the label that I want(which is the last field created)

Numbers Man said...

for lblClass in layer.labelClasses:
if lblClass.showClassLabels:
lblClass.expression = "[YOUR FIELD HERE]"

arcpy.RefreshActiveView()