2

I have this function works totally fine. But i want to be able to change the font size

 def label(wah, field):
    for lyr in arcpy.mapping.ListLayers(mxd):
        if lyr.name == wah:
            if lyr.supports("LABELCLASSES"):
                for lblClass in lyr.labelClasses:
                    lblClass.expression = "["+field+"]"
                    lyr.showLabels = True

I have tried something like this but i cannot quite seem to get it to work. The label size is not changing from the code below

 lblClass.expression = "<FNT size = '8'>" + "[" + {} + "]" + "</FNT>".format(field)

or

lblClass.expression = '"{}" + [{}] +  "{}"'.format("<FNT size = '8'>",field,"</FNT>")

Just for reference when i am calling the function it looks like this:

label("CountyClip", "County")

Can someone help me fix my code?

  • 2
    What about: "<FNT size = '8'>[{0}]</FNT>".format(field) ? – Chad Cooper May 26 '16 at 18:01
3

You've got a little tied up constructing your string

def label(wah, field):
    for lyr in arcpy.mapping.ListLayers(mxd):
        if lyr.name == wah:
            if lyr.supports("LABELCLASSES"):
                for lblClass in lyr.labelClasses:
                    lblClass.expression = "<FNT size='8'>[{}]</FNT>".format(field)
                    lyr.showLabels = True

You don't need to concatenate the string together using + when you're using the str.format(). Construct your entire string as-is, and use {} as the place-holder for input from the str.format() such as your field value.

And if you still have trouble with this you could enclose the string in triple quotes """ as this can help get around any potential escape character issues. e.g.

lblClass.expression = """<FNT size='8'>[{}]</FNT>""".format(field)
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.