1

I've got some problems with the following code, found on this answer to Auto-updating symbology between *.mxds for same shapefile using arcgisscripting?:

import arcpy.mapping as mmap

mxd = mmap.MapDocument("C:/Users/enclume/Carto/Cartes/UpdateTest/P_5_1.mxd")
DF = mmap.ListDataFrames(mxd, "Couches")[0]
sourcely = mmap.Layer("C:/Users/enclume/Carto/Cartes/Templates/Couches.lyr")
ly = mmap.ListLayers(mxd)[1]
mmap.UpdateLayer(DF, ly, sourcely, False)
The folder path containing my mxd's is: "C:/Users/enclume/Carto/Cartes/UpdateTest"

It looks fine when I run it, no error message, but nothing changes in the mxd. What am I doing wrong? As you see it, what is the expected outcome of this code segment?

If I ever find a solution, I would like to loop it. As I understands it, looping would repeat the updateLayer script to every mxd's in the specified file. But if it contains some subfolders, will the looping function find all mxd's in the folder and subfolder?

On the previously linked post, the person who wrote the comment said it would be possible to loop it using glob.glob. Can't find anything one this.

I also saw Improving performance of looping over MXDs using ArcPy? which is about looping mxd's, but I can't seem to find a way to fuse those 2 scripts.

I'm new to using arcpy.

Can anyone help me with this 2-fold problem?

3
  • 3
    Do you have multiple dataframes in your .mxd and are you sure you have one called "Couches"? You might be better off not using the "Couches" search string. Also try calling mxd.save() at the end. I think this is what writes any changes to disk.
    – Dowlers
    Apr 5 '16 at 23:38
  • I have two dataframes "Couches" and "Couches sous la légende", that why I was using the search string. I'll add mxd.save() at the end and give it a try, thanks!
    – Rashomon
    Apr 5 '16 at 23:42
  • It worked! It's updating the mxd. Thanks @Dowlers! Now, how to loop this thing...!?
    – Rashomon
    Apr 6 '16 at 0:16
3

List your mxd before you list your dataframe and layers

    import arcpy
    import os

    MxdFolderPath = arcpy.GetParameterAsText(0)
    if MxdFolderPath == '#' or not MxdFolderPath:
        MxdFolderPath = "C://Users//enclume//Carto//Cartes//UpdateTest"
    MxdCount = 0

    sourcelypath = arcpy.GetParameterAsText(1)
    if sourcelypath == '#' or not sourcelypath:
        sourcelypath = "C://Users//enclume//Carto//Cartes//Templates//Couches.lyr"
    sourcely = arcpy.mapping.Layer(sourcelypath)


    arcpy.env.workspace = MxdFolderPath
    arcpy.AddMessage("   MXD PROCESSING")
    mxdList = arcpy.ListFiles("*.mxd")
    Mxdlen = len(mxdList)
    if Mxdlen == 0:
        arcpy.AddMessage( "   There are no mxd in: "+str(MxdFolderPath))
    else:
        arcpy.AddMessage( "   There are "+str(Mxdlen) +" mxd in: "+str(MxdFolderPath))

        for mxd in mxdList:
            MxdCount = (MxdCount +1)
            MxdPPath = os.path.join(MxdFolderPath,mxd)
            processingmxd = arcpy.mapping.MapDocument(MxdPPath)
            arcpy.AddMessage( " Mxd: "+ str(MxdCount)+" :"+str(mxd))
            dflist = arcpy.mapping.ListDataFrames(processingmxd,"Couches")
            for df in dflist:
                    arcpy.AddMessage( " df: "+ str(df.name))
                    layers = arcpy.mapping.ListLayers(processingmxd, "Couches", df) #layers called Couches
                    for layer in layers:
                        arcpy.AddMessage("      Layer: "+str(layercount) +" :"+str(layer))
                        arcpy.mapping.UpdateLayer(df, layer, sourcely, False)
                        arcpy.AddMessage("      Layer Updated")
                        processingmxd.save()
            arcpy.AddMessage(" ")
    arcpy.AddMessage(" End")

I had most of this as a script tool already, I just added your wild cards. enter image description here

13
  • THANKS!!! We are near, I can feel it!! Awesome script by the way. When I run it, I get this message : Traceback (most recent call last): File "<string>", line 24, in <module> File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\arcobjects\mixins.py", line 611, in init assert (os.path.isfile(mxd) or (mxd.lower() == "current")), gp.getIDMessage(89004, "Invalid MXD filename") AssertionError: Invalid MXD filename. Can you see what's wrong?
    – Rashomon
    Apr 6 '16 at 1:17
  • Do your mxd names conform to microsoft windows standards, and is mxd open?
    – GISI
    Apr 6 '16 at 1:36
  • BTW. Here's a folder crawl if you wanted to consider crawling folders: gis.stackexchange.com/questions/179816/…
    – GISI
    Apr 6 '16 at 1:40
  • None of them are open. I think the names are conform: P.9.2.7.mxd, P.5.1_A.mxd, Tuilage.mxd, P_5_1.mxd and P.5.1.mxd. I tested most of them with the code in the post and they were fine. Is it because some of them are in a subfolder of "C://Users//enclume//Carto//Cartes//UpdateTest"?
    – Rashomon
    Apr 6 '16 at 1:41
  • I'm a newb, but what's a fodler crawl/crawling folder?
    – Rashomon
    Apr 6 '16 at 1:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.