mxd = arcpy.mapping.MapDocument(r"Z:\02_GIS\Project\mxd\P01.mxd") print "mxd" df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] lyrfile = arcpy.mapping.Layer(r"Z:\Maps\GIS_utilities\python_script\lyr\_kabel.lyr") print lyrfile.workspacePath # old path lyrfile.replaceDataSource(r"Z:\02_GIS\Project\wektor", "SHAPEFILE_WORKSPACE", "P01_RIVER.shp")
mxd = arcpy.mapping.MapDocument(r"Z:\02_GIS\Project\mxd\P01.mxd") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] for lyr in arcpy.mapping.ListLayers(mxd, "*", df): if lyr.name == "_kabel": lyr.replaceDataSource(r"Z:\02_GIS\Project\wektor", "SHAPEFILE_WORKSPACE", "P01_River") mxd.save()
#In example below, a layer file if pointing to a shapefile in c:\temp #It is redirected to a data source in c:\active #and then saved to a new layer file (in c:temp) #Old shapefile was usa.shp, new one is use2.shp import arcpy lyrFile = arcpy.mapping.Layer(r"C:\Temp\usa.lyr") for lyr in arcpy.mapping.ListLayers(lyrFile): #THIS IS THE MISSING PIECE if lyr.name == "usa": lyr.replaceDataSource(r"C:\Active", "SHAPEFILE_WORKSPACE", "usa2") lyrFile.saveACopy(r"C:\Temp\usa2.lyr")
according to Jake note this script work fine for me:
import arcpy, os, sys
from arcpy import env
env.workspace = r"F:\Projects\Nadav\gany_tikva\gis"
for mxdname in arcpy.ListFiles("*.mxd"):
print mxdname
mxd = arcpy.mapping.MapDocument(r"F:\Projects\Nadav\gany_tikva\gis\\" + mxdname)
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
for lyr in arcpy.mapping.ListLayers(mxd, "*", df):
if lyr.name == "50000.sid":
# very importent that "RASTER_WORKSPACE" will change according to
# the data we want to replace
lyr.replaceDataSource(r"F:\GIS\topo_50000", "RASTER_WORKSPACE", "50000.sid", True)
print 'replaceDataSource'
mxd.save()
del mxd