I've read just about every post on the internet about updating and/or adding TextElements using arcpy. I am not able to get the text in my TextElement object to update.
The portion of my script is:
pop_cent <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Harrisburg'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Middletown'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Hershey'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Halifax'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Elizabethville'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>exists<SPAN class="punctuation token">(</SPAN>newpath<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">:</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>inpath<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#Create map document object using basemap</SPAN>
mxd<SPAN class="punctuation token">.</SPAN>saveACopy<SPAN class="punctuation token">(</SPAN>newpath<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Copy the basemap from Adams for adding appropriate data</SPAN>
copy_mxd <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>MapDocument<SPAN class="punctuation token">(</SPAN>newpath<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Create map document object using copied basemap</SPAN>
df <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>ListDataFrames<SPAN class="punctuation token">(</SPAN>copy_mxd<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Layers"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token">#lists the dataframe object to use in AddLayer</SPAN>
x <SPAN class="operator token">=</SPAN> df<SPAN class="punctuation token">.</SPAN>elementPositionX
<SPAN class="keyword token">print</SPAN> x
y <SPAN class="operator token">=</SPAN> df<SPAN class="punctuation token">.</SPAN>elementPositionY
<SPAN class="keyword token">print</SPAN> y
orig_text <SPAN class="operator token">=</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="keyword token">for</SPAN> d<SPAN class="punctuation token">,</SPAN> pop <SPAN class="keyword token">in</SPAN> enumerate<SPAN class="punctuation token">(</SPAN>pop_cent<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
orig_text<SPAN class="punctuation token">[</SPAN>d<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>text <SPAN class="operator token">=</SPAN> pop_cent<SPAN class="punctuation token">[</SPAN>d<SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>orig_text<SPAN class="punctuation token">[</SPAN>d<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>text<SPAN class="punctuation token">)</SPAN>
orig_text<SPAN class="punctuation token">[</SPAN>d<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>elementPositionX <SPAN class="operator token">=</SPAN> x
orig_text<SPAN class="punctuation token">[</SPAN>d<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>elementPositionY <SPAN class="operator token">=</SPAN> y
arcpy<SPAN class="punctuation token">.</SPAN>RefreshActiveView<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<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>
The script successfully moves the TextElement to where I want in the "copy_mxd" and the print statement in line 18 provides the correct string from pop_cent. But, the actual TextElement's text does not update the text. Why?
Based on what I've read online, is it because not all the TextElements are embedded in a focused dataframe? Or perhaps I'm trying to go between two different map documents (mxd and copy_mxd) with TextElements? Or do I need to somehow access the LayoutView using the mapping module?
Thank you,
Josh
PS Using ArcMap 10.4 on Windows 7, Python 32-bit.