Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In an attempt to teach myself some Python, I am writing a script that will loop through a netwrok directory for all MXDs and when found, replace layout text elements with dynamic text element equivalents (document path and copyright year in this case). My code is below and it does work. However, some (not all) MXDs "serious application error" crash upon reopen. A trip through the MXD Doctor app will fix whatever the critical error is, but I have not identified the issue. These MXDs open without problem before running the Python script. The only potential culprit I can see so far is that the crashing MXDs have a raster layer reference to a network drive letter that no longer exists.

This leads to a number of questions on potential strategies to fix:

  1. Should I be using a different Save methodology through Python?
  2. Is there a way to create an exception for this (potential) problem? I'm not sure I'm ready to do that - I'm not looking to create code to fix broken links. I just want to repalce layout text.
  3. As the depreicated drive letter should noto be a common issue, what are the thoughts on leaving the issue and just fixing any broken MXDs as they arise?

    # Author:  HDG
    # Date:    June, 2012
    # Version: ArcGIS 10.0
    # Purpose: This script will perform a search and replace on page layout text
    #          elements to replace old Copyright and Pathname text with new Dynamic text
    #          Much code borrowed and edited from Esri ArcPy examples
    import arcpy, string, os 
    #Read input parameters from script tool
    folderPath = arcpy.GetParameterAsText(0)
    # Existing Map Path is a named text element.  Search for name and replace text value
    txtDSName = 'COS_DateTimeStamp'
    newDSText = 'Map Document: <dyn type="document" property="path"/>  <dyn type="date" format="short"/>  <dyn type="time" format=""/>'
    # difficulties encoding the '©' symbol, so brute force searched for each year - not elegant, but effective
    txtCopyright12 = '2012 <Company Name Here>'
    txtCopyright11 = '2011 <Company Name Here>'
    txtCopyright10 = '2010 <Company Name Here>'
    txtCopyright09 = '2009 <Company Name Here>'
    txtCopyright08 = '2008 <Company Name Here>'
    newCopyright = '<dyn type="date" format="yyyy"/> <Company Name Here>'
        #Loop through each MXD file
        for filename in os.listdir(folderPath):
            fullpath = os.path.join(folderPath, filename)
            if os.path.isfile(fullpath):
                if filename.lower().endswith(".mxd"):
                    #Reference MXD
                    mxd = arcpy.mapping.MapDocument(fullpath)
                    mxdName = filename
                    #Find the text elements and replace with new Dynamic Text
                    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):     
                        #Map Path
                        if == txtDSName:
                            elmText = elm.text.replace(elm.text, newDSText)
                            elm.text = elmText
                        #Westwood copyright
                        if txtCopyright12 in elm.text:
                            elmText = elm.text.replace(txtCopyright11, newCopyright)
                            elm.text = elmText
                        if txtCopyright11 in elm.text:
                            elmText = elm.text.replace(txtCopyright11, newCopyright)
                            elm.text = elmText
                        if txtCopyright10 in elm.text:
                            elmText = elm.text.replace(txtCopyright11, newCopyright)
                            elm.text = elmText
                        if txtCopyright09 in elm.text:
                            elmText = elm.text.replace(txtCopyright11, newCopyright)
                            elm.text = elmText
                        if txtCopyright08 in elm.text:
                            elmText = elm.text.replace(txtCopyright11, newCopyright)
                            elm.text = elmText
                    del mxd
        del folderPath, fullpath
    except Exception, e:
        import traceback
        map(arcpy.AddError, traceback.format_exc().split("\n"))
share|improve this question

closed as off-topic by PolyGeo May 10 at 8:50

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This problem cannot or can no longer be reproduced. Changes to the system or to the asker's circumstances may have rendered the question obsolete, or the question does not include a procedure to enable potential answerers to reproduce the same symptoms. Such questions are off-topic as they are unlikely to help future readers, but editing them to include more details can lead to re-opening." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

what happens when you make no changes? Just open the file, step through the file, then save. – mhoran_psprep Jul 18 '12 at 17:58
My first guess would be map document version incompatibilities. These documents with the bad drive reference may also be your oldest, and hence in a version that does not work with dynamic text. save() should convert to the version you are using, but maybe you need to use saveACopy instead. Unfortunately, I don't think there is a way to retrieve the software version of an mxd in arcpy. – blord-castillo Jul 20 '12 at 14:37