Wednesday, 16 November 2016

labeling - Turning on labels of layer using ArcPy?


I am unable to turn on the labels within my Python script:


import arcpy
mxd = arcpy.mapping.MapDocument(r"Mypathhere") #Map document reference
layer = arcpy.mapping.ListLayers(mxd, "Layername")[0] #Indexing list for 1st layer
if layer.supports("LABELCLASSES"):
for lblclass in layer.labelClasses:
lblclass.showClassLabels = True

arcpy.RefreshActiveView()
mxd.save()
del mxd

If I need to enable labeling in the label manager first, can that be done with ArcPy?


The script does not break. I do not get any errors. The points just do not get labeled.



Answer



I think your problem is that your code is enabling the checkbox under the Layer Properties that says "Label features in this class". The part you are missing is the code to enable to the checkbox for "Label features in this layer"


Try inserting this code:


layer.showLabels = True


After your if statement that activates the label classes, like the following:


import arcpy
mxd = arcpy.mapping.MapDocument(r"Mypathhere") #Map document reference
layer = arcpy.mapping.ListLayers(mxd, "Layername")[0] #Indexing list for 1st layer
if layer.supports("LABELCLASSES"):
for lblclass in layer.labelClasses:
lblclass.showClassLabels = True
layer.showLabels = True`
arcpy.RefreshActiveView()

mxd.save()
del mxd

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...