0

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.")
peter
9375 silver badges10 bronze badges
asked Apr 17, 2024 at 21:51
5
  • 3
    You code logic appears to be quite flawed. Indentation is everything in python. so you step over a list of mxd's in your workspace but because of you [lack of] indentation you end up only using the last mxd. You then get the first dataframe step over the layers in that dataframe but completely ignore them all as your replaceDataSource is hard wired. You also have no print statements in your code to indicate WHERE the error is occurring... Commented Apr 17, 2024 at 22:20
  • 2
    Please always present code as formatted text rather than only in a picture. Commented Apr 17, 2024 at 22:55
  • 1
    Also provide the full error message including line number which results from running the code that you present. Commented Apr 17, 2024 at 22:58
  • replaceDataSource is a Layer method not a MapDocument method. See desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/… Commented Apr 18, 2024 at 4:52
  • Thank you all. I went back and tweaked the replacedatasource to lyr and a few other items and it worked! It did not change the layer name to the new layer name but I can dig in to that next. Thank you for suggestions on how to provide a better Q in the future. Below is the script I was able to run successfully: Commented Apr 18, 2024 at 13:54

1 Answer 1

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")
answered Apr 18, 2024 at 13:56
1
  • Thank you. I tried to paste it but it became a run on sentence. Thank you for the link with instructions. Commented Apr 18, 2024 at 15:37

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.