search/replace text in map using python

2880
4
Jump to solution
10-25-2013 12:15 PM
JasonTrook
New Contributor II
Hello,
Does anyone have an example or know the function to use that will allow me to search/replace text elements in a map?  An example is if I have 10 mxd's and I want to make a universal change to the text in my title block.  Instead of opening each map and making the change one by one I'd like to have a tool that searches for a specific text string and replaces it with a new string. 
Thanks,
Jason
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
This is untested code, but something like this should work:

import arcpy, os from arcpy import mapping as m  # Folder containing MXD's folder = r'C:\path\to_your\mapdocuments' arcpy.env.workspace = folder   # Find and Replace text element find_elm = 'Text you want to find' replace_elm = 'Text you want to replace find_elm'  # Find all .mxd files in folder # replaces text element in mxd if it exists for mapDoc in arcpy.ListFiles('*.mxd'):     mxd = arcpy.mapping.MapDocument(os.path.join(folder, mapDoc))     for elm in arcpy.mapping.ListLayoutElements(mxd, 'TEXT_ELEMENT'):         if elm.text == find_elm:             elm.text = replace_elm             print 'Replaced {0} in {1}'.format(find_elm, mapDoc)     mxd.save()     del mxd


You can also use a wild card in the [url=http://resources.arcgis.com/en/help/main/10.1/index.html#/ListLayoutElements/00s30000003w000000/]Lis...] method.

View solution in original post

0 Kudos
4 Replies
by Anonymous User
Not applicable
This is untested code, but something like this should work:

import arcpy, os from arcpy import mapping as m  # Folder containing MXD's folder = r'C:\path\to_your\mapdocuments' arcpy.env.workspace = folder   # Find and Replace text element find_elm = 'Text you want to find' replace_elm = 'Text you want to replace find_elm'  # Find all .mxd files in folder # replaces text element in mxd if it exists for mapDoc in arcpy.ListFiles('*.mxd'):     mxd = arcpy.mapping.MapDocument(os.path.join(folder, mapDoc))     for elm in arcpy.mapping.ListLayoutElements(mxd, 'TEXT_ELEMENT'):         if elm.text == find_elm:             elm.text = replace_elm             print 'Replaced {0} in {1}'.format(find_elm, mapDoc)     mxd.save()     del mxd


You can also use a wild card in the [url=http://resources.arcgis.com/en/help/main/10.1/index.html#/ListLayoutElements/00s30000003w000000/]Lis...] method.
0 Kudos
JasonTrook
New Contributor II
Thanks Caleb - this worked but I'd like your advice on modifying based on two scenarios:

1) How do I find/replace a single word instead of the entire text string? 
2) It doesn't work if I have two rows of text.  My text block says: Figure 1 (row 1) Project Overview (row 2). 
My code says: if elm.text == "Figure 1 Project Overview":  Everything works fine when only one row of text it present but multiple lines do not work.
Thanks,
Jason
0 Kudos
by Anonymous User
Not applicable
Thanks Caleb - this worked but I'd like your advice on modifying based on two scenarios:

1) How do I find/replace a single word instead of the entire text string? 
2) It doesn't work if I have two rows of text.  My text block says: Figure 1 (row 1) Project Overview (row 2). 
My code says: if elm.text == "Figure 1 Project Overview":  Everything works fine when only one row of text it present but multiple lines do not work.
Thanks,
Jason


No problem....As for your first question, it is literally as easy as changing that find variable to just the one word you want to replace.  For your second question, I would have to do some testing but it may work if you insert the python new line character in the text element, which is '\n'. 

So this is untested, but perhaps something like this might work:

if elm.text == "Figure 1 Project Overview\nYour next line to replace here":
    elm.text = "First replace line\nSecond Replace Line"


If I get time today, I can do some testing here.  One other thing you may be able to do is loop through the text elements to see if it startswith a word or full line:

if elm.text.startswith("Figure 1 Project Overview\nYour next line to replace here"):
    elm.text = "First replace line\nSecond Replace Line"


I will do some testing when I have time and re-post.  Good luck!
0 Kudos
by Anonymous User
Not applicable
Sorry, I think I misunderstood your question first question...You meant replace just one word in a text string?  I initially thought you meant just change a text element that is only one word.  For this you could do something like this:

find_word = 'test'
replace_word = 'replace'

for elm in arcpy.mapping.ListLayoutElements(mxd, 'TEXT_ELEMENT'):
        if find_word in elm.text:
            elm.text = elm.text.replace(find_word, replace_word)
            print 'Replaced {0} in {1}'.format(elm.text, mapDoc)


I also made a mistake with the second example for the second question (I still haven't tested yet though):

if elm.text.startswith("Figure 1 Project Overview"):
    elm.text = "First replace line\nSecond Replace Line"
0 Kudos