Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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 stratagies 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?

Thanks

# 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>'

try:
    #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 elm.name == 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

                mxd.save()
                del mxd

    del folderPath, fullpath

except Exception, e:
    import traceback
    map(arcpy.AddError, traceback.format_exc().split("\n"))
    arcpy.AddError(str(e))
share|improve this 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.