Select to view content in your preferred language

Formatting label texts with python

743
3
Jump to solution
08-29-2013 12:08 AM
HitomiMaezawa
Deactivated User
Hello everyone.
I am new to python programming and just managed to write a code to label features with particular field.

Here is the code that I am using for labeling. After specifying map and layer..

   if layer.supports("LABELCLASSES"):
     for lblclass in layer.labelClasses:
         lblclass.showClassLabels = True
         lblclass.expression = " [type]

*I'm creating labels based on [type] field.

Next step is, set the format for the labels.

I am assuming I want to do something on lblclass.expression line..

like surrounding [fieldname] with text-formatting tags.

So I tried this code, lblclass.expression = "<BOL>[type]</BOL>"

and refreshed, but the labels disappeared, and they reappear when I just type lblclass.expression = "[type]"

It does not show any error message, but it just does not work. Same thing in <ITA><\ITA> code.

Does anybody have any idea how to solve this problem?

Any help will be greatly appreciated.

Thanks in advance!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
TrevorHavelka
Deactivated User
I believe you have to quote the tags and the interpreter doesn't understand two double quotes.  Try this code.


lblclass.expression = '"%s" & [type] & "%s"' % ("<BOL>", "</BOL>")

View solution in original post

0 Kudos
3 Replies
TrevorHavelka
Deactivated User
I believe you have to quote the tags and the interpreter doesn't understand two double quotes.  Try this code.


lblclass.expression = '"%s" & [type] & "%s"' % ("<BOL>", "</BOL>")
0 Kudos
HitomiMaezawa
Deactivated User
Thank you so much!
It worked perfectly.
I really appreciate it!
0 Kudos
StevenMoore
Emerging Contributor
I believe you have to quote the tags and the interpreter doesn't understand two double quotes.  Try this code.


lblclass.expression = '"%s" & [type] & "%s"' % ("<BOL>", "</BOL>")


I tried modifying this to use on dynamic labels and I get this instead of the data in the attribute field:
[ATTACH=CONFIG]28558[/ATTACH]

I must be missing some python formatting syntax.
0 Kudos