1

I would like to update misc text in an active map window but can't seem to crack the code and I found some presentations that mention it can be done but the full code isn't there.

The following is from an ESRI presentation @ http://www.google.com.au/url?sa=t&source=web&cd=3&ved=0CCMQFjAC&url=http%3A%2F%2Fproceedings.esri.com%2Fdvd%2Fuc%2F2010%2Fuc-index%2Fuc%2Fworkshops%2Ftw_1433.pdf&ei=2R3STZmoAefTiAKT9oTyCg&usg=AFQjCNH-wFZb1mgpnWrV1QKQD5ynZtikfg&sig2=UBLu89hwpAx72djEJggFJg

import arcpy
findText=arcpy.GetParameterAsText(0)
replaceText=arcpy.GetParameterAsText(1)
mxd=arcpy.mapping.MapDocument("Current")
for textElm in arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT"):
    if findText in textElm.text:
        textElm.text=textElm.text.replace(findText, replaceText)

I want to be able to enter text string "This is the old text" and replace with "this is the replacement text"

Simple find-->replace that word or notepad does but in an active ArcMap.

3
  • You should delete your previous answer on this question, gis.stackexchange.com/questions/5674/…
    – Andy W
    Commented May 17, 2011 at 12:51
  • Andy, that post is a different script which has the required functionality but isn't working...so the two may help someone advise me on how to get the required full functionality. Best
    – GeorgeC
    Commented May 18, 2011 at 3:19
  • if that is the case then you should open that answer as a new question. The format of the site is not to open new questions as answers to other peoples questions.
    – Andy W
    Commented May 18, 2011 at 3:59

1 Answer 1

3

Here is a PSU link that searches and replaces for a certain text element within a given mxd (code sample below).

# Create a MapDocument object referencing the MXD you want to update
mxd = arcpy.mapping.MapDocument(r"C:\GIS\TownCenter_2009.mxd")

# Loop through each text element in the map document
for textElement in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):

    # Check if the text element contains the out of date text
    if textElement.text == "GIS Services Division 2009":

    # If out of date text is found, replace it with the new text
        textElement.text = "GIS Services Division 2010"
3
  • Thanks mate. It works great. Do you know if we can modify it to not only accept full match but partial map as well. For example I now have to change "Map Sheet 4 of 19" to "Map Sheet 05 of 19" rather than simply "Sheet 4" to "Sheet 05". Cheers.
    – GeorgeC
    Commented May 18, 2011 at 0:47
  • how do you reset your text element to its original?
    – ziggy
    Commented Mar 29, 2016 at 15:34
  • @ziggy, one option would be to write the old value(s) to text file or some custom configuration file. When you need to pull in an old value, you could call a custom function to pull the value out of the said file above.
    – artwork21
    Commented Mar 29, 2016 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.