[GIS] Wrap text element labels

arcpylabeling

I am writing a python script for batch map production and for every map the script replaces a text label called "Company". sometimes the name is way to long and it extends onto the map and off the map.

I have not been able to find any wrap labeling info on the internet.

So far i have tried manually editing the label in the MXD but there seems to be no wrap text option

Best Answer

@Midavalo has an excellent solution. I just wanted to add one alternative if you do not want to work with rectangle text boxes. You can use the built-in textwrap module to define how you want to wrap the text based on a character count. This way you do not have to change the type of your text labels.

It is very simple code:

>>> mxd = arcpy.mapping.MapDocument('current')
>>> txt = [elm for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT")][0]
>>> s = 'This is a very long string that we want to wrap.  It looks very bad when there is overlow and it goes off of the page'
>>> import textwrap
>>> txt.text = '\r\n'.join(textwrap.wrap(s, 15))
>>> 

And the photo:

enter image description here

And of course you can always insert manual breaks with carriage returns: '\r\n'.

Related Question