import arcpy from arcpy import env from arcpy import mapping workspace = arcpy.env.workspace = "C:/GIS/MAPBOOK/Proposed Zoning Book" arcpy.env.overwriteOutput = True mxdList = arcpy.ListFiles("*.mxd") for mxd in mxdList: mxd = workspace + "//" + mxd for lyr in arcpy.mapping.ListLayers(mxd): if lyr.name == "PROPOSED ZONING": lyr.replaceDataSource("C:/Users/t***a/AppData/Roaming/ESRI/Desktop10.1/ArcCatalog/DSD15_SQLEXPRESS.gds/DSD/DSD.DBO.MUNICIPALITY", "SHAPEFILE_WORKSPACE", "DSD.DBO.FUTURE_LAND_USE_ZONING") for lyr in arcpy.mapping.ListLayers(mxd): if lyr.name == "CITY LIMITS": lyr.replaceDataSource("C:/Users/t***a/AppData/Roaming/ESRI/Desktop10.1/ArcCatalog/DSD15_SQLEXPRESS.gds/TonyOneWay/TonyOneWay.DBO.Canyon_Features", "SHAPEFILE_WORKSPACE", "TonyOneWay.DBO.City_Limits") print "Successfully updated data sources" mxd.save()
Traceback (most recent call last): File "C:\GIS\Python Scripts\Change Data Source MXD 2.py", line 13, in <module> for lyr in arcpy.mapping.ListLayers(mxd): File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\utils.py", line 181, in fn_ return fn(*args, **kw) File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\mapping.py", line 1500, in ListLayers result = mixins.MapDocumentMixin(map_document_or_layer).listLayers(wildcard, data_frame) File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\mixins.py", line 823, in listLayers layers = self.layers File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\mixins.py", line 683, in layers for frame in reversed(self.dataFrames): File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\mixins.py", line 695, in dataFrames return map(convertArcObjectToPythonObject, self.pageLayout.dataFrames) File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\mixins.py", line 679, in pageLayout return convertArcObjectToPythonObject(self._mxd._arc_object.pageLayout) AttributeError: 'unicode' object has no attribute '_arc_object'
import os import arcpy path = r'C:\GIS\MAPBOOK\Proposed Zoning Book' for fileName in os.listdir(path): fullPath = os.path.join(path, fileName) if os.path.isfile(fullPath): basename, extension = os.path.splitext(fullPath) if extension == ".mxd": mxd = arcpy.mapping.MapDocument(fullPath) for lyr in arcpy.mapping.ListLayers(mxd): print lyr
C:\GIS\MAPBOOK\Proposed Zoning Book\Proposed_ZoningMapBook_Page_1.mxd is being processed Traceback (most recent call last): File "C:\GIS\Python Scripts\Change Data Source MXD 2.py", line 18, in <module> lyr.replaceDataSource(r"C:\Users\talmeida\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\DSD15_SQLEXPRESS.gds\DSD\DSD.DBO.MUNICIPALITY", "SDE_WORKSPACE", "DSD.DBO.FUTURE_LAND_USE_ZONING") File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\utils.py", line 181, in fn_ return fn(*args, **kw) File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\_mapping.py", line 680, in replaceDataSource return convertArcObjectToPythonObject(self._arc_object.replaceDataSource(*gp_fixargs((workspace_path, workspace_type, dataset_name, validate), True))) ValueError: Layer: Unexpected error
import arcpy from arcpy import env from arcpy import mapping workspace = arcpy.env.workspace = "C:\GIS\MAPBOOK\Proposed Zoning Book" arcpy.env.overwriteOutput = True mxdList = arcpy.ListFiles("*.mxd") for mxd in mxdList: mxd = workspace + "\\" + mxd print mxd + " is being processed" mxd = arcpy.mapping.MapDocument(mxd) mxd.findAndReplaceWorkspacePaths(r"C:\Users\t**a\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\DBO.DSD.VECTOR.sde",r"C:\Users\talmeida\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\DSD15_SQLEXPRESS.gds\DSD") for lyr in arcpy.mapping.ListLayers(mxd): if lyr.name == "PROPOSED ZONING": lyr.replaceDataSource(r"C:\Users\talmeida\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\DSD15_SQLEXPRESS.gds\DSD\DSD.DBO.MUNICIPALITY", "SDE_WORKSPACE", "DSD.DBO.FUTURE_LAND_USE_ZONING") for lyr in arcpy.mapping.ListLayers(mxd): if lyr.name == "CITY LIMITS": lyr.replaceDataSource(r"C:\Users\t**a\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\DSD15_SQLEXPRESS.gds\TonyOneWay\TonyOneWay.DBO.Canyon_Features", "FILEGDB_WORKSPACE", "TonyOneWay.DBO.City_Limits") print "Successfully updated data sources" mxd.save
I started a new window and retyped the script and i got past the AttributeError: 'unicode' object has no attribute '_arc_object' error.
but none of the workspace paths were updated.
it error out on me on line 18.
path = r'C:\GIS\MAPBOOK\Proposed Zoning Book' for fileName in os.listdir(path): fullPath = os.path.join(path, fileName) if os.path.isfile(fullPath): basename, extension = os.path.splitext(fullPath) if extension == ".mxd": mxd = arcpy.mapping.MapDocument(fullPath) print fullPath print mxd for lyr in arcpy.mapping.ListLayers(mxd): if lyr.name == "PROPOSED ZONING": lyr.replaceDataSource(r"C:\Users\talmeida\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\DSD15_SQLEXPRESS.gds\DSD\DSD.DBO.MUNICIPALITY", "SDE_WORKSPACE", "DSD.DBO.FUTURE_LAND_USE_ZONING") elif lyr.name == "CITY LIMITS": lyr.replaceDataSource(r"C:\Users\t**a\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\DSD15_SQLEXPRESS.gds\TonyOneWay\TonyOneWay.DBO.Canyon_Features", "FILEGDB_WORKSPACE", "TonyOneWay.DBO.City_Limits") print "Successfully updated data sources" mxd.save
C:\GIS\MAPBOOK\Proposed Zoning Book\Proposed_ZoningMapBook_Page_1.mxd <geoprocessing Map object object at 0x02965400> Traceback (most recent call last): File "C:\GIS\Python Scripts\Change Data Source MXD 2.py", line 21, in <module> lyr.replaceDataSource(r"C:\Users\talmeida\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\DSD15_SQLEXPRESS.gds\TonyOneWay\TonyOneWay.DBO.Canyon_Features", "FILEGDB_WORKSPACE", "TonyOneWay.DBO.City_Limits") File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\utils.py", line 181, in fn_ return fn(*args, **kw) File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\_mapping.py", line 680, in replaceDataSource return convertArcObjectToPythonObject(self._arc_object.replaceDataSource(*gp_fixargs((workspace_path, workspace_type, dataset_name, validate), True))) ValueError: Layer: Unexpected error
import arcpy, os from arcpy import env from arcpy import mapping arcpy.env.overwriteOutput = True path = r'C:\GIS\MAPBOOK\Proposed Zoning Book' for fileName in os.listdir(path): fullPath = os.path.join(path, fileName) if os.path.isfile(fullPath): basename, extension = os.path.splitext(fullPath) if extension == ".mxd": mxd = arcpy.mapping.MapDocument(fullPath) print fullPath print mxd for lyr in arcpy.mapping.ListLayers(mxd): if lyr.name == "PROPOSED ZONING": lyr.replaceDataSource(r"C:\Users\talmeida\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\DSD15_SQLEXPRESS.gds\DSD\DSD.DBO.MUNICIPALITY", "FILEGDB_WORKSPACE", "DSD.DBO.FUTURE_LAND_USE_ZONING") elif lyr.name == "CITY LIMITS": lyr.replaceDataSource(r"C:\Users\talmeida\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\DSD15_SQLEXPRESS.gds\TonyOneWay\TonyOneWay.DBO.Canyon_Features", "FILEGDB_WORKSPACE", "TonyOneWay.DBO.City_Limits") print "Successfully updated data sources" mxd.save
import arcpy from arcpy import env # Create a variable called Workspace to hold the workspace path # Doing it this way allows us to continually reference the 'workspace' variable # whenever we want to get to that path. Also, use the raw string literal format to # reference the path. The way you had it before is ok, but using forward slashes can # cause issues with inconsistencies in future path definitions, especially when joining # or concatenating paths. workspace = r"C:\GIS\MAPBOOK\Proposed Zoning Book" print "Workspace = " + str(workspace) # Now set the Workspace by referencing the path contained in the 'workspace' variable arcpy.env.workspace = workspace print "Environment Workspace set to: " + str(workspace) # Set the overwriteOutput environment parameter to True arcpy.env.overwriteOutput = True print "Overwrite Outputs: True" # Create a list of MXDs in the workspace and populate the results in the variable # 'mxdList' mxdList = arcpy.ListFiles("*.mxd") # Validate that results were actuall populated in the 'mxdList' variable and that it is # not empty # If the number of items (len) contained in the mxdList is equal to 0: if len(mxdList) == 0: # Raise an error because there are no MXDs in the workspace!! raise Exception("No MXDs were found in the workspace!") # Otherwise: else: # Do nothing and continue pass # Inform how many MXDs were returned from the Workspace and populated in the 'mxdList' print "Found " + str(len(mxdList) + " MXDs in the Workspace") # For each mxd populated in the mxdList: for mxd in mxdList: # Change the variable name to 'targetMXD' because you already have an 'mxd' # variable instantied as the current mxd being executed against in the loop. # This could cause problems with execution and at the least, can be confusing targetMXD = workspace + "//" + mxd # For each layer in the 'targetMXD' for lyr in arcpy.mapping.ListLayers(targetMXD)[0]: # If the given 'lyr' is "PROPOSED ZONING" if lyr.name == "PROPOSED ZONING": print "Found Layer with name: " + str(lyr.name) + ". Replacing Datasource..." # Replace the Layer's datasource with the below lyr.replaceDataSource("C:/Users/t***a/AppData/Roaming/ESRI/Desktop10.1/ArcCatalog/DSD15_SQLEXPRESS.gds/DSD/DSD.DBO.MUNICIPALITY", "SHAPEFILE_WORKSPACE", "DSD.DBO.FUTURE_LAND_USE_ZONING") print "Success" # Otherwise if the given 'lyr.name' is "CITY LIMITS" elif lyr.name == "CITY LIMITS": print "Found Layer with name: " + str(lyr.name) + ". Replacing Datasource..." # replace the given Layer's Datasource with the below lyr.replaceDataSource("C:/Users/t***a/AppData/Roaming/ESRI/Desktop10.1/ArcCatalog/DSD15_SQLEXPRESS.gds/TonyOneWay/TonyOneWay.DBO.Canyon_Features", "SHAPEFILE_WORKSPACE", "TonyOneWay.DBO.City_Limits") print "Success" print "Successfully updated data sources" mxd.save()
I tried the following and got on line 21,
Proposed Zoning layer data source in the 1st mxd did not update.C:\GIS\MAPBOOK\Proposed Zoning Book\Proposed_ZoningMapBook_Page_1.mxd <geoprocessing Map object object at 0x02965400> Traceback (most recent call last): File "C:\GIS\Python Scripts\Change Data Source MXD 2.py", line 21, in <module> lyr.replaceDataSource(r"C:\Users\talmeida\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\DSD15_SQLEXPRESS.gds\TonyOneWay\TonyOneWay.DBO.Canyon_Features", "FILEGDB_WORKSPACE", "TonyOneWay.DBO.City_Limits") File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\utils.py", line 181, in fn_ return fn(*args, **kw) File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\_mapping.py", line 680, in replaceDataSource return convertArcObjectToPythonObject(self._arc_object.replaceDataSource(*gp_fixargs((workspace_path, workspace_type, dataset_name, validate), True))) ValueError: Layer: Unexpected error
Codeimport arcpy, os from arcpy import env from arcpy import mapping arcpy.env.overwriteOutput = True path = r'C:\GIS\MAPBOOK\Proposed Zoning Book' for fileName in os.listdir(path): fullPath = os.path.join(path, fileName) if os.path.isfile(fullPath): basename, extension = os.path.splitext(fullPath) if extension == ".mxd": mxd = arcpy.mapping.MapDocument(fullPath) print fullPath print mxd for lyr in arcpy.mapping.ListLayers(mxd): if lyr.name == "PROPOSED ZONING": lyr.replaceDataSource(r"C:\Users\talmeida\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\DSD15_SQLEXPRESS.gds\DSD\DSD.DBO.MUNICIPALITY", "FILEGDB_WORKSPACE", "DSD.DBO.FUTURE_LAND_USE_ZONING") elif lyr.name == "CITY LIMITS": lyr.replaceDataSource(r"C:\Users\talmeida\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\DSD15_SQLEXPRESS.gds\TonyOneWay\TonyOneWay.DBO.Canyon_Features", "FILEGDB_WORKSPACE", "TonyOneWay.DBO.City_Limits") print "Successfully updated data sources" mxd.save
Workspace = C:\GIS\MAPBOOK\Proposed Zoning Book Environment Workspace set to: C:\GIS\MAPBOOK\Proposed Zoning Book Overwrite Outputs: True Traceback (most recent call last): File "C:/GIS/Python Scripts/Change Data Source MXD_test.py", line 37, in <module> print "Found " + str(len(mxdList) + " MXDs in the Workspace") TypeError: unsupported operand type(s) for +: 'int' and 'str'