Select to view content in your preferred language

arcpy.mapping replace multi-line text element

918
1
12-22-2011 09:19 AM
RichardThurau
Deactivated User
Hi,
I'm trying to replace a word in a text element for many maps. My code effectively lists all maps, and all text elements, but the replacing part is not happening. I am wondering if it is because the element I am trying to replace is actually in three lines such as:

National Resources
Plan
Base

import arcpy

ws = arcpy.env.workspace = "X:\\Maps\\Draft4_BU\\"

MapList = arcpy.ListFiles("*.mxd")
for m in MapList:
    print m
    mxd = arcpy.mapping.MapDocument(ws + m)
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
        print elm.text
        if elm.text == "National Resources '/r/n' Plan '/r/n'Base":
            elm.text = "Natural Resources '/r/n' Plan '/r/n' Base"
    mxd.save()
    del mxd


Anyone have any ideas about how I can get this to work?

Thanks

Rich
Tags (2)
0 Kudos
1 Reply
JeffWard
Honored Contributor
I think the way you are testing for the newline character might be causing you problems.

I tried the following in the Python command window:

 
x = "National Resources\nPlan\nBase"
 
 
if x == "National Resources\nPlan\nBase": x = "Natural 
Resources\nPlan\nBase"
 
 
print x


and this is what printed:

Natural Resources
Plan
Base

Python uses the backslash for special characters like the newline character \n.

You might want to put a print line inside of your if statement to see if the condition is true.  You can also try expressions in the command window to see how they evaluate like I did.
Jeff Ward
Summit County, Utah
0 Kudos