1

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
5
  • 1
    Have you also checked the expression from within the label properties? Is it correct? If so, can you try using a simpler expression to test wether it's your code that is not doing things properly or the expression (e.g. lblClass.expression = 'TEST')? Commented Sep 10, 2019 at 15:47
  • I have tested it by doing something similar at it would show up. Could it be the label expression? Commented Sep 10, 2019 at 16:28
  • 2
    That's what I am saying. Try to test it directly in arcmap. If it does not work there, you foynd the issue. Commented Sep 10, 2019 at 18:27
  • 2
    The issue was with the newline python code, I switched it to VBnewline and it worked! Commented Sep 10, 2019 at 19:21
  • 1
    Glad you found out! You should post the solution as answer if you have time, would help anybody with a similar problem Commented Sep 10, 2019 at 20:26

1 Answer 1

0

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.