0

I need to turn labels on for previously used layer. Here is the code:

import arcpy
import sys
from arcpy import env

env.overwriteOutput = 1
gp = arcgisscripting.create(9.3)

fc_Mord = arcpy.GetParameterAsText(0)

mxd = arcpy.mapping.MapDocument("CURRENT")
layer = arcpy.mapping.ListLayers(mxd, "Mord")[0]
if layer.supports("LABELCLASSES"):
for lblClass in layer.labelClasses:
    lblClass.expression = "[ArcID]"
    layer.showLabels = True
arcpy.RefreshActiveView()
del mxd

I want to use variable fc_Mord instead of layer name Mord, because user can name the layer (in previous steps) as he/she wants.

I have tried this, but it doesn't label the layer

tempLayer = "tempLayer"
arcpy.MakeFeatureLayer_management(fc_Mord, "tempLayer")

mxd = arcpy.mapping.MapDocument("CURRENT")
layer = arcpy.mapping.ListLayers(mxd, tempLayer)[0]
if layer.supports("LABELCLASSES"):
for lblClass in layer.labelClasses:
    lblClass.expression = "[ArcID]"
    layer.showLabels = True
arcpy.RefreshActiveView()
del mxd

What am I doing wrong here?

Edited code

mxd = arcpy.mapping.MapDocument("CURRENT") #Map document reference
def label(fc_Mord, field):
   for lyr in arcpy.mapping.ListLayers(mxd):
       if lyr.name == fc_Mord:
           if lyr.supports("LABELCLASSES"):
               for lblClass in lyr.labelClasses:
                   lblClass.expression = "[ArcID]".format(field)
                   lyr.showLabels = True
arcpy.RefreshActiveView()
del mxd
0

Try this:

import arcpy
import sys
from arcpy import env

env.overwriteOutput = 1
gp = arcgisscripting.create(9.3)
attr_name = 'Attribute Name'

fc_Mord = arcpy.GetParameterAsText(0)
mxd = arcpy.mapping.MapDocument("CURRENT") #Map document reference
def label(fc_Mord, field):
   for lyr in arcpy.mapping.ListLayers(mxd):
       if lyr.name == fc_Mord:
           if lyr.supports("LABELCLASSES"):
               for lblClass in lyr.labelClasses:
                   lblClass.expression = "[{}]".format(field)
                   lyr.showLabels = True
label(fc_Mord, attr_name)
arcpy.RefreshActiveView()
del mxd

Just specify attr_name variable.

1

Try using this function: i use it all the time it works great. Also you need to indent after your if layer.supports("LABELCLASSES"):

def label(layer):
    for lyr in arcpy.mapping.ListLayers(mxd):
        if lyr.name == layer:
            if lyr.supports("LABELCLASSES"):
                for lblClass in lyr.labelClasses:
                    lblClass.expression = "[ArcID]"
                    lyr.showLabels = True

you need to call the function after you initialize it

label(fc_Mord)

if you want "tempLayer" to be labeled then

label("tempLayer")
  • So, where I specify a layer i need to label? in a map document I have i.e. 5 layers, I need to pick one. that's why i set the variable fc_Mord. And in my case it is not necessary to set font...how and where can I use this function in my case? – O.Baklanova Jun 7 '16 at 20:48
  • Check my edit-ur Layer is the first item to be called in the function – ziggy Jun 8 '16 at 3:45
  • Still no labels appearing. I will add the edited code in the question – O.Baklanova Jun 8 '16 at 15:04
  • which layer do you want to show labels? fc_Mord? or "tempLayer"? – ziggy Jun 8 '16 at 15:35
  • I want to be labelled shp which user selects fc_Mord = arcpy.GetParameterAsText(0) – O.Baklanova Jun 8 '16 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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