ArcPy: Using lblclass expression to change font style and color

4462
4
Jump to solution
10-12-2012 03:05 AM
MartinScheiber
Occasional Contributor
I´m trying to label features of a dataset using ArcPy and the lblclass function. While commands like "VBNewline" and "Round" work pretty well, I´m not able to change colors or font style. An expression that works in ArcMap doesn´t work in the script. Could anyone help me out please? I´m using ArcGIS 10.0

Code that works:
#Adding Labels     layer = arcpy.mapping.ListLayers(mxd, "")[0]      if layer.supports("LABELCLASSES"):         for lblclass in layer.labelClasses:             lblclass.className = "Flaeche"             lblclass.expression = "[FID] & VBNewLine & Round([AREA_mm2],2)"         lblclass.showClassLabels = True     layer.showLabels = True     arcpy.RefreshActiveView() 

Code that works in ArcMap but not in ArcPy:
#Adding Labels layer = arcpy.mapping.ListLayers(mxd, "")[0]  if layer.supports("LABELCLASSES"):     for lblclass in layer.labelClasses:         lblclass.className = "Flaeche"         lblclass.expression = ""<CLR red='255' green='0' blue='0'>" & [FID] & VBNewLine & Round([AREA_mm2],2) & "</CLR>""     lblclass.showClassLabels = True layer.showLabels = True arcpy.RefreshActiveView()
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MartinScheiber
Occasional Contributor
Dear Wayne,

thanks for your answers!
I got the following tip in a different GIS forum and with this it works perfectly now !


This code doesn't work because Python interpreter doesn't understand two double quotes in a row.
Instead you can try this:

lblclass.expression = '"%s" & [FID] & VBNewLine & Round([AREA_mm2],2) & "%s"' % ("<CLR red='255' green='0' blue='0'>", "</CLR>")


View solution in original post

0 Kudos
4 Replies
T__WayneWhitley
Frequent Contributor
Text formatting tags are not interpreted by Python, VBScript, and JScript.....end up passed as plain text to ArcMap and 'dynamically' interpreted.  I paraphrased from here:

http://resources.arcgis.com/en/help/main/10.1/index.html#//00s80000000p000000

So then, how to handle?   Test this, but I think it'll work if you take out the internal double quotes...with the extra quotes in the exp, I think ArcMap isn't 'aware' of the tags.  And with one set of double quotes to set the text string to pass lblclass to in turn pass to the label engine, it'll work I think...see this too:

http://resources.arcgis.com/en/help/main/10.1/index.html#//00s800000027000000
0 Kudos
T__WayneWhitley
Frequent Contributor
EDIT:  Actually, I think I got it backwards and there's another error too... Remove the outer quotes, and use '+' not '&' to concatenate strings, and convert all 'parts' to string.
0 Kudos
MartinScheiber
Occasional Contributor
Dear Wayne,

thanks for your answers!
I got the following tip in a different GIS forum and with this it works perfectly now !


This code doesn't work because Python interpreter doesn't understand two double quotes in a row.
Instead you can try this:

lblclass.expression = '"%s" & [FID] & VBNewLine & Round([AREA_mm2],2) & "%s"' % ("<CLR red='255' green='0' blue='0'>", "</CLR>")


0 Kudos
T__WayneWhitley
Frequent Contributor
Very nice!  Glad it works now, neat trick.
0 Kudos