0

I am trying to make changes to titles on multiple mxds instead of having to open 1 by 1 but it is only making changes to 1 mxd file, I have even tried to make duplicate copies of the mxd to test but again it only makes 1 change. The code runs with no issues, am I missing something ?

import arcpy, os
from os import listdir

mxdPath = r"C:\Users\Admin\Desktop\Final Maps\New folder"

mxdFiles = [ f for f in listdir(mxdPath) if os.path.isfile(os.path.join(mxdPath,f)) and f[-3:] == 'mxd' ]

try:

    map = arcpy.mapping
    for mxdFile in mxdFiles:
        mxd = map.MapDocument(mxdPath + "\\" + mxdFile)
        for elm in map.ListLayoutElements(mxd,"TEXT_ELEMENT"):
            if elm.text == "CountyProject130":
                elm.text = "Testing709"


    mxd.save()
    del mxd
    Print "Complete!"

except Exception as e:
    print(e.message)     
  • I think that try/except statements are best removed when presenting code here and testing because they can mask otherwise useful messages. – PolyGeo Dec 6 '18 at 20:15
2

Unless the formatting is screwed up, you need one more indent when you save the MXD. The way it's showing now only saves the last MXD in the loop.

for mxdFile in mxdFiles:
    mxd = map.MapDocument(mxdPath + "\\" + mxdFile)
    for elm in map.ListLayoutElements(mxd,"TEXT_ELEMENT"):
    |   if elm.text == "CountyProject130":
    |        elm.text = "Testing709"
    |
--->mxd.save()
--->del mxd
  • GENIUS ! IT WORKED! Thank you very much !!!!!!!! very very much! – R R Dec 6 '18 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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