I want to set the FindLabel function to the labelclass expression using ArcPy. My code is like How To: Label a related table. I don't want to load the function into the expression editor of the label tab but in the toolbox.
I try to load function programmatically but it's not working. What's wrong in the code?
Another problem is using brackets "[]" for field names. In the expression editor the users can write [field name] but in Python I must use string characters. Also FindLabel mainly uses an advanced expression in expression editor but I could not set the advanced expression and parser.
How can I use FindLabel Function programmatically?
import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
layer = arcpy.mapping.ListLayers(mxd, "fc")[0]
def FindLabel ([keyField], [FirstLabel]):
key1 = [keyField] # Key field in feature class
key2 = "ID" # Key field in related table
L = [FirstLabel] # Label field in feature class
L2 = "Label2" # Label field in related table
myDataTable = r"<path-to-related-table>" # Path to related table
cur = arcpy.da.SearchCursor(myDataTable, [key2, L2])
for row in cur:
if str(key1) == str(row[0]):
L = L + " " + str(row[1])
return L
layer.labelClasses[0].expression = .... ?
print layer.labelClasses[0].expression
layer.labelClasses[0].showClassLabels = True
layer.showLabels = True
arcpy.RefreshActiveView()
del mxd