1

I am trying to make changes to titles on multiple mxds instead of having to open 1 by 1 but keep getting an error.

import arcpy.mapping as mapping, os

for root, dirs, files in os.walk(r"F:\Final Maps\New folder"): # GetPArameterAsTExt(0)
    for name in files:
        filename = os.path.join(root, name) 
        if ".mxd" in filename:
            mxd = mapping.MapDocument(filename)
            for map in mxd:
                for elm in arcpy.mapping.ListLayoutElements(map, "TEXT_ELEMENT"):
                    if elm.text == "TEST 90666":
                        elm.text = "9067"

This is the error that I am getting:

enter image description here

  • 1
    Please always provide errors as text rather than pictures. – PolyGeo Dec 5 '18 at 12:25
3

You are getting this error because the arcpy.mapping.MapDocument object does not support iteration. You would need to construct a list of map document files (which can be done with os.walk() or a list of arcpy.mapping.MapDocument objects and then iterate it.

So the line for map in mxd: should be removed.

import arcpy.mapping as mapping, os

for root, dirs, files in os.walk(r"F:\Final Maps\New folder"): # GetParameterAsText(0)
    for name in files:
        filename = os.path.join(root, name) 
        if ".mxd" in filename:
            mxd = mapping.MapDocument(filename)
            for elm in arcpy.mapping.ListLayoutElements(map, "TEXT_ELEMENT"):
                if elm.text == "TEST 90666":
                    elm.text = "9067"
  • Thank you for the help, the code now seems to run with no issues but the titles have not changed when I open the mxd files – R R Dec 5 '18 at 17:02
  • 1
    You need to save the MXD after you make the changes to the text element. mxd.save() – GBG Dec 5 '18 at 17:35
  • 1
    That's right, the changes should be saved. I have focused on the error you are receiving and have overlooked the saving operation. – Alex Tereshenkov Dec 5 '18 at 19:35
0

I have now made some changes to the code which it works but it is only updating 1 mxd file instead of all 3 I have in the folder. Any suggestions?

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!"
  • Please post a new question next time to get someone to provide an answer. For now, your problem is that you run mxd.save() outside of the for loop - indent the mxd.save() to the right one level. – Alex Tereshenkov Dec 6 '18 at 13:31
  • Please ask another question – Mapperz Dec 6 '18 at 15:40
  • I will ask it in another question, I apologize and thanks everyone, indent did not work. – R R Dec 6 '18 at 18:47

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.