Replacing text elements that contain a return

Discussion created by MBruening on Dec 31, 2013
Latest reply on Dec 31, 2013 by Wayne_Whitley

I'm fairly new to Python scripting, but I have managed to do something that will be helpful for folks within our office. I have modified a small python script from the on-line class that will change out text within some text elements found within the Layout view on an active MXD document. I am currently running 10.0, but will be upgrading to 10.2 as soon.

However, I am having an issue with one of the text elements, because it contains two lines of text due to a [return]. Example of the text element within the [Text Properties] dialogue box would be as follows -

Main title
Secondary title

Which I believe should be scripted in this manner "Main title \n Secondary title"!?

I have tried a number of different ways of referencing this within the script and no luck on having the text element update to the new value. Here are a few ways that I have tried entering this into the python script -

...     if ele.text == "Main title\n\Secondary title":
...         ele.text = "Main title\n\Secondary title UPDATE"

...     if ele.text == """Main title\n\Secondary title""":
...         ele.text = """Main title\n\Secondary title UPDATE"""

...     if ele.text == """Main title\n\
Secondary title""":
...         ele.text = """Main title\n\
Secondary title UPDATE"""

...     if ele.text == "Main title\n\
Secondary title":
...         ele.text = "Main title\n\
Secondary title UPDATE"

Strange thing is that I can take portions of the above code within the quotes and add [print] in front and have IDLE function correctly. Meaning I can enter the following -

print "Main title\n\Secondary title" and I would receive the result below.

Main title
Secondary title

This is my script; simple and sweet -

#import modules
... import arcpy
... #set environment
... mxd = arcpy.mapping.MapDocument("CURRENT")
... #list map layout text elements
... eleList = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT")
... #loop through list and find all text elements with "Polk County" title
... for ele in eleList:
...  #This will update the title of the figure within the title block
...     if ele.text == """Main title \n Secondary title""":
...         ele.text = """Main title \n Secondary title updated"""
... #refresh the active view
... arcpy.RefreshActiveView()
... print "Script completed"

Any tips or suggestions to this newbie would be appreciated.

Thanks in advance!