It does not run for me at all. In fact, I set root = r'D:' and it gives me .prj files located several folders deep. In no way can I make it report the mxd's in the listed directory.If I start it out like this it will get the list of mxd's:
arcpy.env.overwriteOutput = True
text_elms = []
the_text_elm = None
start = "D:\\"
for root, dirs, files in os.walk(start):
for mapDoc in files:
if mapDoc.endswith(".mxd"):
print 'Working on ',mapDoc
path = os.path.abspath(os.path.join(root,mapDoc))
mxd = arcpy.mapping.MapDocument(path)
text_elms = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "Archit")[0]
the_text_elm = None
the_text_elm = text_elm
have you typed print os.listdir(root) and make sure that it only reports mxd files? as mine crashes trying to set the mxd = somefileotherthan mxd (the first file in my directory) the above code "fixes" that.If you are changing for ALL text elements with name = "Archit", you don't need to iterate through the listIn fact, you assign it to text_elms, then try to iterate through that as a list.Can't be done as text_elms is a list of TextElement objects, and is not iterable as is not a list ().Then, in this part, do whatever if's you need, but you have assigned the text element to text_elms variable (not the_text_elm), so that is the one you want to update:
if the_text_elm:
i = the_text_elm.text.find('Archit')
new_text = the_text_elm.text[:i] + 'Historic Resources'
text_elms.text = new_text
mxd.save()
Here is modified code that is working for me, you should be able to extract what you need:
import os
import arcpy
# Overwrite pre-existing files
arcpy.env.overwriteOutput = True
# Loop over MXDs
folder = r'H:\myDir'
arcpy.env.workspace = folder
arcpy.env.overwriteOutput = True
for root, dirs, files in os.walk(start):
for mapDoc in files:
if mapDoc.endswith(".mxd"):
print 'Working on ',mapDoc
path = os.path.abspath(os.path.join(root,mapDoc))
mxd = arcpy.mapping.MapDocument(path)
if arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "Archit"): # this keeps it from erroring out if the text element "Archit" doesn't exist...
text_elms = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "Archit")[0]
new_text = "test" + ' Historic Resources'
text_elms.text = new_text
mxd.save()
Keep in mind this will walk through all sub-folders as well.Hope this gets you on the right track,R_