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. Any assistance would be appreciated.
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)
Aucun commentaire:
Enregistrer un commentaire