2

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

1
  • 1
    there is a box text option that would define the width of the box and wrap inside that.
    – Brad Nesom
    Commented Mar 17, 2016 at 18:03

2 Answers 2

3

@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'.

4
  • Okay cool I will give this a try. txt.text = '\r\n'.join(textwrap.wrap(s, 15)) does the 15 refer to the font size?
    – ziggy
    Commented Mar 17, 2016 at 18:53
  • I think 15 will be the character limit (it'll wrap so it doesn't pass 15 characters per line)
    – Midavalo
    Commented Mar 17, 2016 at 18:55
  • 1
    No, 15 is the max amount of characters per line. You can set this to whatever you need. This number will vary based on your font size. I'd check out the documentation. There are also options for breaking long words with hyphens and other things.
    – crmackey
    Commented Mar 17, 2016 at 18:55
  • @Midavalo beat me to it :)
    – crmackey
    Commented Mar 17, 2016 at 18:56
1

As Brad said in his comment above, you can add a text box that allows you to define the width and height of the box, and any text will automatically wrap inside that.

You can find "Rectangle Text" from the Text dropdown on your Drawing toolbar

Find Rectangle Text on the Drawing Toolbar

Add your text box, set your frame to no border (unless of course you want a border around your text), and set the textbox to the dimensions you require. This box can be used by arcpy the same way as any other text element.

Example of different types of text boxes

The Title and Text options from the Insert menu will just resize the text if you try to change the size of the textbox, but the Rectangle Text box allows you to set dimensions.

EDIT: This code snippet is what I used to test:

mxd = arcpy.mapping.MapDocument("CURRENT")
elm = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "Company")[0]

if elm.name == "Company":
    elm.text = "This text is far too long for one line and could go off the edge of the page unless the text wraps within the text box!"  
3
  • So i defined the text dimensions with the recetangle text but now when the text is too long, it just does not show up. it is not wrapping automatically
    – ziggy
    Commented Mar 17, 2016 at 18:42
  • that's odd behaviour - I have added the code I used to test.
    – Midavalo
    Commented Mar 17, 2016 at 18:54
  • it worked! i needed to set the margins to 0 so the text would appear. thanks
    – ziggy
    Commented Mar 17, 2016 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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