2

I currently have a python script which will replace the data sources on a MXD with broken links. The code seems to work fine as I have run the code a handful of times using relative paths to indicate the location of the MXD and the location of the GDB. My problem begins when I change to relative paths to "GetParameterAsText". Once I do that and turn it into a script tool, it crashes.

Here is the code that works, its a bit messy, but it works.

import os
import logging
logging.basicConfig(level=logging.DEBUG)

logging.debug("importing arcpy")
import arcpy
logging.debug("arcpy loaded")


arcpy.env.workspace = "Z:\Work\rd\Fortier\World_Index_Test.gdb"

#Add layer to exisiting MXD with a dataframe named Layer.
logging.debug("opening map document")
mxd = arcpy.mapping.MapDocument (r"Z:\Work\rd\Fortier\WorldIndex\World_Index\Templates\World_Index_Template2.mxd")
logging.debug("map document open")
logging.debug("accessing data frame")
df = arcpy.mapping.ListDataFrames (mxd) [0]
logging.debug("Found dataframe: {}".format(df.name))

#Check to see if layers exist within GDB and change data source.

for lyr in arcpy.mapping.ListLayers(mxd, "", df):
if lyr.isFeatureLayer:
    name = lyr.datasetName
    path = r"Z:\Work\rd\Fortier\World_Index_Test.gdb"
    if  arcpy.Exists(os.path.join(path, name)):
        lyr.replaceDataSource(path, "FILEGDB_WORKSPACE", name)
        logging.info("replaced data source on {}".format(lyr.name))
    else:
        print "skipped because no source"

else:
    #arcpy.mapping.RemoveLayer(df, lyr)
    print "skipped group"


#Saves copy of new MXD and deletes original mxd
logging.debug("Saving new mxd")
mxd.saveACopy(r"Z:\Work\rd\Fortier\WorldIndex\World_Index\Outputs\Test1.mxd") 
del mxd #addLayer

Now when I attempt to change it on to a script tool it crashes:

import os
import logging
logging.basicConfig(level=logging.DEBUG)

logging.debug("importing arcpy")
import arcpy
logging.debug("arcpy loaded")


#arcpy.env.workspace = "Z:\Work\rd\Fortier\World_Index_Test.gdb"

#Add layer to exisiting MXD with a dataframe named Layer.
logging.debug("opening map document")
mxd = arcpy.GetParameterAsText(0)
logging.debug("map document open")
logging.debug("accessing data frame")
df = arcpy.mapping.ListDataFrames (mxd) 
logging.debug("Found dataframe: {}".format(df.name))

#Check to see if layers exist within GDB and change data source.

for lyr in arcpy.mapping.ListLayers(mxd, "", df):
if lyr.isFeatureLayer:
    name = lyr.datasetName
    path = arcpy.GetParameterAsText(1)
    if  arcpy.Exists(os.path.join(path, name)):
        lyr.replaceDataSource(path, "FILEGDB_WORKSPACE", name)
        logging.info("replaced data source on {}".format(lyr.name))
    else:
        print "skipped because no source"

else:
    #arcpy.mapping.RemoveLayer(df, lyr)
    print "skipped group"


#Saves copy of new MXD and deletes original mxd
logging.debug("Saving new mxd")
mxd.saveACopy = arcpy.GetParameterAsText(2) 
del mxd #addLayer

Here is the error I am getting:

Start Time: Tue Dec 30 05:55:36 2014 Running script CreateMXD... Failed script CreateMXD...

Traceback (most recent call last): File "D:\World_Index\WorldIndex\World_Index\Scripts\CreateMXD.py", line 29, in df = arcpy.mapping.ListDataFrames (mxd) File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\utils.py", line 181, in fn_ return fn(*args, **kw) File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\mapping.py", line 1479, in ListDataFrames result = mixins.MapDocumentMixin(map_document).listDataFrames(wildcard) File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\arcobjects\mixins.py", line 728, in listDataFrames return list(reversed(list(self.dataFrames))) File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\arcobjects\mixins.py", line 695, in dataFrames return map(convertArcObjectToPythonObject, self.pageLayout.dataFrames) File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\arcobjects\mixins.py", line 679, in pageLayout return convertArcObjectToPythonObject(self._mxd._arc_object.pageLayout) AttributeError: 'unicode' object has no attribute '_arc_object'

Failed to execute (CreateMXD). Failed at Tue Dec 30 05:55:37 2014 (Elapsed Time: 1.41 seconds)

1 Answer 1

6

I can see a couple of things you're missing in your script tool. You need to change the mxd parameter string (the unicode object mentioned in your traceback) to a map document object.

mxd = arcpy.GetParameterAsText(0)
mxd = arcpy.mapping.MapDocument (mxd)

..or more simply:

mxd = arcpy.mapping.MapDocument (arcpy.GetParameterAsText(0))

To avoid a subsequent error you'll also need to change this:

df = arcpy.mapping.ListDataFrames (mxd)

...back to this:

df = arcpy.mapping.ListDataFrames (mxd)[0]

...to get the first data frame in the data frame list.

3
  • Thank you Brad, The script tool ran with no errors after making the suggested corrections. However, the MXD I am saving did not appear in the location I saved it. In fact, it did not appear anywhere. In the script tool I am saving a new MXD. I have it set as an ArcMap Mapping Document and as an output parameter. I have tried saving it as "Test" and "Test.mxd", but it does not seem to exist. Any ideas?
    – ryanhd20
    Jan 3, 2015 at 1:09
  • The output MXD path would actually be an input for the script, so try setting it to an input parameter. Also be sure to use the full path to where you want the MXD stored so you know exactly where it ends up. Jan 5, 2015 at 17:55
  • I tried setting the parameter as an input. The problem is when I tried to save the new MXD, it came back with an error saying that did not exist, so I chose to use an existing MXD, however it did not seem to work. Boggles my mind that with relative paths this script works, but trying to turn it into a script tool it gives me nothing but issues.
    – ryanhd20
    Jan 7, 2015 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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