Select to view content in your preferred language

Find and replace text for all map documents within a folder

1794
1
02-21-2012 11:29 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 Reply
JakeSkinner
Esri Esteemed Contributor
You will have to declare the mxd with the 'arcpy.mapping.MapDocument' function.  Try the following:

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

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