I want to lable feature class with related table.One of the solution for this goal is writing script in the Expression of label tab ( How To: Label a related table). But i don't want to scripting in Expression.Then it's not as simple as the Esri solution.
My code is working properly but i could not iterate "lblclass.expression" for the row1 and the related table ( row1) is not iterate and just show one value for all labels.How to iterate related table value for labeling ?
import arcpy
table = "table"
listtlabel = []
mxd = arcpy.mapping.MapDocument("CURRENT")
layer = arcpy.mapping.ListLayers(mxd, "Point")[0]
fielddelimi1 = arcpy.AddFieldDelimiters(table, "Code")
sql_exp1 = "{0} = {1}".format(fielddelimi1,309)
if layer.supports("LABELCLASSES"):
for lblclass in layer.labelClasses:
with arcpy.da.SearchCursor(table,["Code1","Row"],sql_exp1) as cur1:
for row1 in cur1:
listtlabel.append(row1[0])
lblclass.expression = '[Code]' + '&'+ str(row1[1])
str1=','.join(str(e) for e in listtlabel)
print str1
with arcpy.da.SearchCursor(layer,"Code2") as cur:
for row in cur:
if row[0] in listtlabel:
lblclass.SQLQuery = '"Code2"' + "IN" + "({0})".format(str1)
lblclass.showClassLabels = True
layer.showLabels = True
arcpy.RefreshActiveView()
del mxd