<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: search/replace text in map using python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/search-replace-text-in-map-using-python/m-p/422978#M33240</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This is untested code, but something like this should work:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy, os from arcpy import mapping as m&amp;nbsp; # Folder containing MXD's folder = r'C:\path\to_your\mapdocuments' arcpy.env.workspace = folder&amp;nbsp;&amp;nbsp; # Find and Replace text element find_elm = 'Text you want to find' replace_elm = 'Text you want to replace find_elm'&amp;nbsp; # Find all .mxd files in folder # replaces text element in mxd if it exists for mapDoc in arcpy.ListFiles('*.mxd'): &amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument(os.path.join(folder, mapDoc)) &amp;nbsp;&amp;nbsp;&amp;nbsp; for elm in arcpy.mapping.ListLayoutElements(mxd, 'TEXT_ELEMENT'): &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if elm.text == find_elm: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elm.text = replace_elm &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Replaced {0} in {1}'.format(find_elm, mapDoc) &amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.save() &amp;nbsp;&amp;nbsp;&amp;nbsp; del mxd&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&lt;SPAN&gt;You can also use a wild card in the [url=&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://resources.arcgis.com/en/help/main/10.1/index.html#/ListLayoutElements/00s30000003w000000/]ListLayoutElements[/url" rel="nofollow" target="_blank"&gt;http://resources.arcgis.com/en/help/main/10.1/index.html#/ListLayoutElements/00s30000003w000000/]ListLayoutElements[/url&lt;/A&gt;&lt;SPAN&gt;] method.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 27 Oct 2013 00:58:40 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2013-10-27T00:58:40Z</dc:date>
    <item>
      <title>search/replace text in map using python</title>
      <link>https://community.esri.com/t5/python-questions/search-replace-text-in-map-using-python/m-p/422977#M33239</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Does anyone have an example or know the function to use that will allow me to search/replace text elements in a map?&amp;nbsp; An example is if I have 10 mxd's and I want to make a universal change to the text in my title block.&amp;nbsp; 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.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Jason&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 25 Oct 2013 19:15:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/search-replace-text-in-map-using-python/m-p/422977#M33239</guid>
      <dc:creator>JasonTrook</dc:creator>
      <dc:date>2013-10-25T19:15:29Z</dc:date>
    </item>
    <item>
      <title>Re: search/replace text in map using python</title>
      <link>https://community.esri.com/t5/python-questions/search-replace-text-in-map-using-python/m-p/422978#M33240</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This is untested code, but something like this should work:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy, os from arcpy import mapping as m&amp;nbsp; # Folder containing MXD's folder = r'C:\path\to_your\mapdocuments' arcpy.env.workspace = folder&amp;nbsp;&amp;nbsp; # Find and Replace text element find_elm = 'Text you want to find' replace_elm = 'Text you want to replace find_elm'&amp;nbsp; # Find all .mxd files in folder # replaces text element in mxd if it exists for mapDoc in arcpy.ListFiles('*.mxd'): &amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument(os.path.join(folder, mapDoc)) &amp;nbsp;&amp;nbsp;&amp;nbsp; for elm in arcpy.mapping.ListLayoutElements(mxd, 'TEXT_ELEMENT'): &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if elm.text == find_elm: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elm.text = replace_elm &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Replaced {0} in {1}'.format(find_elm, mapDoc) &amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.save() &amp;nbsp;&amp;nbsp;&amp;nbsp; del mxd&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&lt;SPAN&gt;You can also use a wild card in the [url=&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://resources.arcgis.com/en/help/main/10.1/index.html#/ListLayoutElements/00s30000003w000000/]ListLayoutElements[/url" rel="nofollow" target="_blank"&gt;http://resources.arcgis.com/en/help/main/10.1/index.html#/ListLayoutElements/00s30000003w000000/]ListLayoutElements[/url&lt;/A&gt;&lt;SPAN&gt;] method.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 27 Oct 2013 00:58:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/search-replace-text-in-map-using-python/m-p/422978#M33240</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2013-10-27T00:58:40Z</dc:date>
    </item>
    <item>
      <title>Re: search/replace text in map using python</title>
      <link>https://community.esri.com/t5/python-questions/search-replace-text-in-map-using-python/m-p/422979#M33241</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Caleb - this worked but I'd like your advice on modifying based on two scenarios:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;1) How do I find/replace a single word instead of the entire text string?&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2) It doesn't work if I have two rows of text.&amp;nbsp; My text block says: Figure 1 (row 1) Project Overview (row 2).&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;My code says: if elm.text == "Figure 1 Project Overview":&amp;nbsp; Everything works fine when only one row of text it present but multiple lines do not work.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Jason&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 31 Oct 2013 21:31:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/search-replace-text-in-map-using-python/m-p/422979#M33241</guid>
      <dc:creator>JasonTrook</dc:creator>
      <dc:date>2013-10-31T21:31:05Z</dc:date>
    </item>
    <item>
      <title>Re: search/replace text in map using python</title>
      <link>https://community.esri.com/t5/python-questions/search-replace-text-in-map-using-python/m-p/422980#M33242</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Thanks Caleb - this worked but I'd like your advice on modifying based on two scenarios:&lt;BR /&gt;&lt;BR /&gt;1) How do I find/replace a single word instead of the entire text string?&amp;nbsp; &lt;BR /&gt;2) It doesn't work if I have two rows of text.&amp;nbsp; My text block says: Figure 1 (row 1) Project Overview (row 2).&amp;nbsp; &lt;BR /&gt;My code says: if elm.text == "Figure 1 Project Overview":&amp;nbsp; Everything works fine when only one row of text it present but multiple lines do not work.&lt;BR /&gt;Thanks,&lt;BR /&gt;Jason&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&amp;nbsp; 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'.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So this is untested, but perhaps something like this might work:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
if elm.text == "Figure 1 Project Overview\nYour next line to replace here":
&amp;nbsp;&amp;nbsp;&amp;nbsp; elm.text = "First replace line\nSecond Replace Line"
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If I get time today, I can do some testing here.&amp;nbsp; One other thing you may be able to do is loop through the text elements to see if it &lt;/SPAN&gt;&lt;STRONG&gt;startswith&lt;/STRONG&gt;&lt;SPAN&gt; a word or full line:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
if elm.text.startswith("Figure 1 Project Overview\nYour next line to replace here"):
&amp;nbsp;&amp;nbsp;&amp;nbsp; elm.text = "First replace line\nSecond Replace Line"
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I will do some testing when I have time and re-post.&amp;nbsp; Good luck!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:06:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/search-replace-text-in-map-using-python/m-p/422980#M33242</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-11T19:06:49Z</dc:date>
    </item>
    <item>
      <title>Re: search/replace text in map using python</title>
      <link>https://community.esri.com/t5/python-questions/search-replace-text-in-map-using-python/m-p/422981#M33243</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Sorry, I think I misunderstood your question first question...You meant replace just one word in a text string?&amp;nbsp; I initially thought you meant just change a text element that is only one word.&amp;nbsp; For this you could do something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
find_word = 'test'
replace_word = 'replace'

for elm in arcpy.mapping.ListLayoutElements(mxd, 'TEXT_ELEMENT'):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if find_word in elm.text:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elm.text = elm.text.replace(find_word, replace_word)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Replaced {0} in {1}'.format(elm.text, mapDoc)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I also made a mistake with the second example for the second question (I still haven't tested yet though):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
if elm.text.startswith("Figure 1 Project Overview"):
&amp;nbsp;&amp;nbsp;&amp;nbsp; elm.text = "First replace line\nSecond Replace Line"&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:06:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/search-replace-text-in-map-using-python/m-p/422981#M33243</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-11T19:06:52Z</dc:date>
    </item>
  </channel>
</rss>

