Has anyone every had any issues with ArcGIS not loading the labels that are set via ArcPy? I've only inserted the code that labels two created layers that are made earlier in the script.
# uses the mxd that is running this code
mxd = arcpy.mapping.MapDocument("CURRENT")
# df is the dataframe, Layers is used to run through all the layers within the mxd. Leave Layers as is
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
# lyr sets the layer, needs to be spelt exactly as the layer sits in ArcMap
lyr = arcpy.mapping.ListLayers(mxd, "PARCELS_OWNER", df)[0]
arcpy.env.overwriteOutput = True
for lyr1 in arcpy.mapping.ListLayers(mxd, "Comps"):
print lyr1.name
if lyr1.supports("SHOWLABELS"):
print lyr1.name + " supports label classes"
for lblClass in lyr1.labelClasses:
print lblClass.className
lblClass.expression = lblClass.expression = '"{}" + "Parcel #:" + [AccountNo] + \n + [StrNum] + " " + [Street] + "{}"'.format("<FNT size = '18'>","</FNT>")
if lblClass.showClassLabels:
print " Class Name: " + lblClass.className
print " Expression: " + lblClass.expression
lyr1.showLabels = True
for lyr2 in arcpy.mapping.ListLayers(mxd, "Subject"):
print lyr2.name
if lyr2.supports("SHOWLABELS"):
print lyr2.name + " supports label classes"
for lblClass in lyr2.labelClasses:
print lblClass.className
lblClass.expression = lblClass.expression = '"{}" + "Subject #:" + [AccountNo] + \n + [StrNum] + " " + [Street] + "{}"'.format("<FNT size = '18'>","</FNT>")
if lblClass.showClassLabels:
print " Class Name: " + lblClass.className
print " Expression: " + lblClass.expression
lyr2.showLabels = True
del mxd
arcpy.RefreshActiveView()
I don't get any errors in my code, but the labels are never displayed. The box within properties is checked to display labels, but nothing.
asked Sep 10, 2019 at 15:33
1 Answer 1
I used VB to make this work.
for lyr in arcpy.mapping.ListLayers(mxd, "Comps"):
print lyr.name
if lyr.supports("SHOWLABELS"):
print lyr.name + " supports label classes"
for lblClass in lyr.labelClasses:
print lblClass.className
lblClass.expression = lblClass.expression = '"{}" & "Comp #: " & [AccountNo] & vbNewLine & [StrNum] & " " & [Street] & " " & [StrSuf] & " " & [StrUnit] & "{}"'.format("<FNT size = '10'>","</FNT>")
if lblClass.showClassLabels:
print " Class Name: " + lblClass.className
print " Expression: " + lblClass.expression
lyr.showLabels = True
for lyr in arcpy.mapping.ListLayers(mxd, "Subject"):
print lyr.name
if lyr.supports("SHOWLABELS"):
print lyr.name + " supports label classes"
for lblClass in lyr.labelClasses:
print lblClass.className
lblClass.expression = lblClass.expression = '"{}" & "Subject: " & [AccountNo] & vbNewLine & [StrNum] & " " & [Street] & " " & [StrSuf] & " " & [StrUnit] & "{}"'.format("<FNT size = '10'>","</FNT>")
if lblClass.showClassLabels:
print " Class Name: " + lblClass.className
print " Expression: " + lblClass.expression
lyr.showLabels = True
del mxd
arcpy.RefreshActiveView()
answered Sep 12, 2019 at 19:40
lang-py
lblClass.expression = 'TEST'
)?