Select to view content in your preferred language

Find and replace text for all map documents within a folder (mxd)

5985
5
Jump to solution
02-21-2012 11:28 AM
by Anonymous User
Not applicable
Original User: mthorpe

Hello.

I'm looking to use a Python script to update the text on multiple mxds without opening each one up. Basically a find and replace function, looped through all MXDs in a directory. I have no real Python experience, so perhaps this has a simple answer. This is the code I have right now.

import arcpy, glob, os

path = r"C:\\"
mxdList = glob.glob(path + "\*.mxd")

for mxd in mxdList:
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
  if elm.text == "works":
   elm.text = "replaceword"
mxd.save()
del mxd


The error I am receiving now is "Runtime error <type 'exceptions.AttributeError'>: 'str' object has no attribute '_arc_object'"
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
Ok, instead of using trying to edit the aforementioned code to get it to work ... I used this instead:

import arcpy import os import glob  folder = r"\\sample\sample"  oldtext = "Township" newtext = "Borough"  mxds = glob.glob(folder + '\\' + '*.mxd') arcpy.gp.overwriteOutput = True for mxdFile in mxds:     mxd = arcpy.mapping.MapDocument(mxdFile)     for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):         if elm.text == oldtext:             elm.text = newtext     mxd.save() del mxd     


Did exactly what I wanted it to do. 🙂

View solution in original post

0 Kudos
5 Replies
by Anonymous User
Not applicable
Original User: jscheirer

for mxdFile in mxdList:
    mxd = arcpy.mapping.MapDocument(mxdFile)
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
        if elm.text == "works":
            elm.text = "replaceword"
        mxd.save()
0 Kudos
by Anonymous User
Not applicable
Bumping this thread because I can get this script to work ... but not perfectly.   😄

Instead of looping through every MXD in the folder and changing the specified text (changing "Township" to "Borough"), the script only changes the text in the last MXD in the list and saves it.  The folder has about 20 MXD's within it but only the last one is edited.

Any ideas why only the last MXD would be changed?
0 Kudos
by Anonymous User
Not applicable
Ok, instead of using trying to edit the aforementioned code to get it to work ... I used this instead:

import arcpy import os import glob  folder = r"\\sample\sample"  oldtext = "Township" newtext = "Borough"  mxds = glob.glob(folder + '\\' + '*.mxd') arcpy.gp.overwriteOutput = True for mxdFile in mxds:     mxd = arcpy.mapping.MapDocument(mxdFile)     for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):         if elm.text == oldtext:             elm.text = newtext     mxd.save() del mxd     


Did exactly what I wanted it to do. 🙂
0 Kudos
by Anonymous User
Not applicable
Original User: weltiar

If you wanted to do this dynamically how would the code be changed?  I want to create a tool where you can pick what folder and type in old text and the new text.
0 Kudos
IanMurray
Frequent Contributor
You could add a script to a toolbox and make a script tool out of it.  You would have to make 3 parameters in the tool, one for the input folder(make it a folder type), one for the input text(make it a string type) and one for the output text (make it a string type)

You would have to replace

folder = r"\\sample\sample"
oldtext = "Township"
newtext = "Borough"

with

folder = arcpy.GetParameterAsText(0)
oldtext = arcpy.GetParameterAsText(1)
newtext = arcpy.GetParameterAsText(2)

The 0, 1, 2 pull the first, second, and third parameters you make respectively in the script tool.


Check out this link on how to make a script tool.  It also has links for setting up the parameters.
0 Kudos