I have a working script that searches for a text element in an MXD and replaces it with new text. I'm using it to update the "print date: mo./day/year". But, this method is sort of limited in that it only finds that exact string. In my map document folder, the dates of these maps differ from each other. So, how can I make the script replace an open-ended string?
I've tried:
oldText <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Print Date: "</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
and
oldText <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Print Date: *"</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
As the script is here, "oldText = Print Date: 9.6.2016" will be replaced with "newText = Print Date: 11/13/2018". But, dates on other maps could be anything. If I could replace the old with dynamic text date that would be even better.
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> Workspace <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"path\to\TestFolder"</SPAN>
Output <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"path\to\TestFolder2"</SPAN>
oldText <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Print Date: 9.6.2016"</SPAN>
oldList <SPAN class="operator token">=</SPAN> oldText<SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN><SPAN class="string token">', '</SPAN><SPAN class="punctuation token">)</SPAN>
newText <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Print Date: 11/13/2018"</SPAN>
newList <SPAN class="operator token">=</SPAN> newText<SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN><SPAN class="string token">', '</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># list the mxds of the workspace folder</SPAN>
<SPAN class="keyword token">for</SPAN> mxdname <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListFiles<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"*.mxd"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"checking document: {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>mxdname<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># set the variable</SPAN>
mxd <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>MapDocument<SPAN class="punctuation token">(</SPAN>Workspace <SPAN class="operator token">+</SPAN> <SPAN class="string token">"\\"</SPAN> <SPAN class="operator token">+</SPAN> mxdname<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># replace elements that occur in the map document</SPAN>
<SPAN class="keyword token">for</SPAN> elm <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>ListLayoutElements<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"TEXT_ELEMENT"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
counter <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN>
<SPAN class="keyword token">for</SPAN> text <SPAN class="keyword token">in</SPAN> oldList<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> text <SPAN class="keyword token">in</SPAN> elm<SPAN class="punctuation token">.</SPAN>text<SPAN class="punctuation token">:</SPAN>
elm<SPAN class="punctuation token">.</SPAN>text <SPAN class="operator token">=</SPAN> elm<SPAN class="punctuation token">.</SPAN>text<SPAN class="punctuation token">.</SPAN>replace<SPAN class="punctuation token">(</SPAN>text<SPAN class="punctuation token">,</SPAN> newList<SPAN class="punctuation token">[</SPAN>counter<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">'{} changed'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>elm<SPAN class="punctuation token">.</SPAN>text<SPAN class="punctuation token">)</SPAN>
counter <SPAN class="operator token">=</SPAN> counter <SPAN class="operator token">+</SPAN> <SPAN class="number token">1</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
counter <SPAN class="operator token">=</SPAN> counter <SPAN class="operator token">+</SPAN> <SPAN class="number token">1</SPAN>
<SPAN class="keyword token">for</SPAN> elm <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>ListLayoutElements<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
counter <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN>
<SPAN class="keyword token">for</SPAN> text <SPAN class="keyword token">in</SPAN> oldList<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> text <SPAN class="keyword token">in</SPAN> mxd<SPAN class="punctuation token">.</SPAN>title<SPAN class="punctuation token">:</SPAN>
mxd<SPAN class="punctuation token">.</SPAN>title <SPAN class="operator token">=</SPAN> elm<SPAN class="punctuation token">.</SPAN>text<SPAN class="punctuation token">.</SPAN>replace<SPAN class="punctuation token">(</SPAN>text<SPAN class="punctuation token">,</SPAN> newList<SPAN class="punctuation token">[</SPAN>counter<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
counter <SPAN class="operator token">=</SPAN> counter <SPAN class="operator token">+</SPAN> <SPAN class="number token">1</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
counter <SPAN class="operator token">=</SPAN> counter <SPAN class="operator token">+</SPAN> <SPAN class="number token">1</SPAN>
<SPAN class="comment token"># move the mxd.saveACopy outside of the if loop so a copy is saved even it does not meet the condition of the if loops</SPAN>
mxd<SPAN class="punctuation token">.</SPAN>save<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#(os.path.join(Output + "\\" + mxdname))</SPAN>
<SPAN class="comment token"># do not include this delete statement inside the above loop or it will delete the mxd object inside the loop. Make sure to dedent.</SPAN>
<SPAN class="keyword token">del</SPAN> mxd<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>