I am brand new to Python and trying to change out a shapefile in several MXDs to a GDB layer. When I run the following I get the error 'arcpy has no attribute mapping' error. I have seen where this can stem from using the wrong arcpy if you are using Pro but this is an MXD created in 10.8 and I am using Idle 2.7.18 to run the script. Therefore I don't think I need to change to arcpy.mp in this case.
Can you help me understand what the error is meaning?
Below is the Code I have:
#This scenario involves updating two different workspaces in a map document.
import arcpy
from arcpy import env
# Set the workspace for the folder containing MXDs
arcpy.env.workspace = "S:\Workgroups\Test""
# Iterate through all MXDs in the workspace
for mxd_name in arcpy.ListFiles("*.mxd"):
mxd_path = env.workspace + "\\" + mxd_name
mxd = arcpy.mapping.MapDocument(mxd_path)
df = arcpy.mapping.ListDataFrames(mxd)[0]
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
mxd.replaceDataSource ("S:\Workgroups\backup_2019_2.shp",
"SHAPEFILE WORKSPACE", "S:\Workgroups\Data.gdb\Final",
"FILEGDB_WROKSPACE")
# Save changes to the MXD
mxd.save()
print ("Data sources updated for all MXDs.")
1 Answer 1
for mxd_name in arcpy.ListFiles("*.mxd"):
mxd_path = env.workspace + "\\" + mxd_name
mxd = arcpy.mapping.MapDocument(mxd_path)
df = arcpy.mapping.ListDataFrames(mxd)[0]
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
if lyr == "All_Indexes_Merged_backup_2019_2":
Print ("lyr")
Lyr.replaceDataSource("S:\Workgroups\backup_2019_2.shp", "SHAPEFILE_WORKSPACE", "S:\Workgroups\Data.gdb\Indexes_Final", "FILEGDB_WROKSPACE")
-
Thank you. I tried to paste it but it became a run on sentence. Thank you for the link with instructions.Anonymous– Anonymous2024年04月18日 15:37:51 +00:00Commented Apr 18, 2024 at 15:37
replaceDataSource
is aLayer
method not aMapDocument
method. See desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/…