Iterating through MXDs to change datasource shapefile to gbd

244
11
3 weeks ago
kaww02
by
New Contributor III

I am trying my hand at python for the first time. Using 2.7.18 Idle to check for errors (b/c I don't understand visual studio yet)  I am wanting to go through all MXDs in a folder, find the layer Backup (shapefile) and change its source to a different folder and a GDB with another name, then update the TOC layer name. I don't get errors, but the script does not do anything.  All my printed statements print but nothing seems to be working.  Any suggestions for this newbie? 

 

import arcpy
from arcpy import env

# Set the workspace for the folder containing MXDs
arcpy.env.workspace = r"S:\Workgroups\Test"
mxdList = arcpy.ListFiles("*.mxd")

# Iterate through all MXDs in the workspace
for mxd in mxdList:
    mxdpath = env.workspace + "\\" + mxd
    print (mxd + "Is being processed")
    
    mxdNext = arcpy.mapping.MapDocument(mxdpath)

for df in arcpy.mapping.ListDataFrames(mxdNext,"Layers"):
    for lyr in arcpy.mapping.ListLayers(mxd, "Backup", df):
        if lyr == "Backup":
           Lyr.replaceDataSource(r"S:\Workgroups", "SHAPEFILE_WORKSPACE", "Backup.shp",
                                  r"S:\Workgroups\Data.gdb\Final", "FILEGDB_WROKSPACE")
        if lyr.name == "Backup":
            lyr.name = "Final"
print("lyr.name")      


arcpy.RefreshTOC()
del mxd
    
print("Data sources updated for all MXDs.")

 

 

 

0 Kudos
11 Replies
AlfredBaldenweck
MVP Regular Contributor

>"FILEGDB_WROKSPACE"

This might be the issue? Although tbh I feel like it'd throw an actual error at you.

kaww02
by
New Contributor III

good catch. I corrected that. Still no error or changes made.

0 Kudos
Tom_Laue
New Contributor III

After making changes to your mxd via Python, you need to save those changes with:

 

mxd.save()

 

kaww02
by
New Contributor III

I added the mxd.save() just now just above the del mxd. I received this error: mxd.save()
AttributeError: 'unicode' object has no attribute 'save'

AlfredBaldenweck
MVP Regular Contributor

Try mxdNext.save() instead.

RhettZufelt
MVP Frequent Contributor

line 18 you are trying to replaceDataSource on variable "Lyr", but your layer variable is set to lower case "lyr".

Is it just this capitalization typo that is causing it?

R_

Tom_Laue
New Contributor III

@AlfredBaldenweck is correct.

If you're ever curious what a variable is you can type:

 

print(type(mxd))

 




Also, the df part need to be tabbed over:

 

from arcpy import env

# Set the workspace for the folder containing MXDs
arcpy.env.workspace = r"S:\Workgroups\Test"
mxdList = arcpy.ListFiles("*.mxd")

# Iterate through all MXDs in the workspace
for mxd in mxdList:
    mxdpath = env.workspace + "\\" + mxd
    print (mxd + "Is being processed")
    
    mxdNext = arcpy.mapping.MapDocument(mxdpath)

    for df in arcpy.mapping.ListDataFrames(mxdNext,"Layers"):
        for lyr in arcpy.mapping.ListLayers(mxd, "Backup", df):
            if lyr == "Backup":
               lyr.replaceDataSource(r"S:\Workgroups", "SHAPEFILE_WORKSPACE", "Backup.shp",
                                      r"S:\Workgroups\Data.gdb\Final", "FILEGDB_WROKSPACE")
            if lyr.name == "Backup":
                lyr.name = "Final"
    print("lyr.name")      

    mxdNext.save()
    arcpy.RefreshTOC()

 

 

Otherwise it's only going to run on the last MXD in the folder, not all the MXDs.

kaww02
by
New Contributor III

Okay fixed the Lyr to lyr- based on all the changes now I get one map whose time stamp in file Explorer appears to have done something but when I open it, it is still the same. Also, if I open any map and try and save it under the same name/folder etc it says I don't have permissions for my folder to save or it is too full neither one of which is the case.  Thanks for all the help. I am sure I have a few errors causing all this. I thought putting print statements in would help me determine where it gets hung up but that does not seem to help.

0 Kudos
Tom_Laue
New Contributor III

Python IDLE is probably putting a lock on those mxds.

If you close the Python results Window, you should be able to edit in ArcMap.