I am trying to loop through a bunch of mxds in a folder and make some changes, in this case in particular I am trying to replace a text string. This should change all instances of "9/26/2014" with the dynamic date tag. The code runs fine but when I open one of the mxds, the date is not updated. Is there some saving or refreshing that has to be done that i'm not doing currently?

import os
import sys
import string

folderPath = '[folder path goes here]'
oldText = "9/26/2014"
newText = "<dyn type='date' format='short'/>"

for filename_1 in os.listdir(folderPath):  
    sort_list.append(filename_1) #append to empty list
    list_length = len(sort_list)  #list length for progress
sort_list.sort() #sort

for filename in sort_list:      
  fullpath = os.path.join(folderPath, filename) 

  if os.path.isfile(fullpath):   #check if its a file
    basename, extension = os.path.splitext(fullpath)  #split into the name and extension
    if extension.lower() == ".mxd":  #if its an ArcGIS document
      mxd = arcpy.mapping.MapDocument(fullpath)
      print mxd
      df = arcpy.mapping.ListDataFrames(mxd,"*")[0]
      print df

      #Find all page layout text elements
      for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
        if oldText in elm.text:
          elmText = elm.text.replace(oldText, newText)
          elm.text = elmText


The problem might be a very simple typo in this case, no big deal at all. The code posted in your question is providing a reference to the callable object with the name mxd.save, it doesn't appear to be actually executing the save method onboard the mxd object. The original code here will pass through silently without error, which is indeed frustrating/confusing in this case.

Replacing mxd.save with mxd.save() should fix it.

| improve this answer | |
  • That was it! Thanks! I know I originally have the parentheses but lost them somewhere along the way. – RossV Apr 11 '16 at 15:16

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.