1

Can anyone help with the following script? It's goal is to list every mxd in a folder, list picture elements in them and the change the path for a specific element.

Here's an update version of the script. It goes through and save the document, without error messabge, but doesn't change the picture element path.

import arcpy
import os

MxdFolderPath = arcpy.GetParameterAsText(0)
if MxdFolderPath == '#' or not MxdFolderPath:
    MxdFolderPath = "//10.13.77.8/MRC_doc/20_Territoires/20-4000_Schema_Amen_Dev/20-4100_Planification/20-4120_Projet_SAD/20-4121_Projet_SAD/Y_Cartographie/A_Planches/Mise_a_jour/Test"
MxdCount = 0

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:
        mxdPath = os.path.join(MxdFolderPath, mxd)
        MapDoc = arcpy.mapping.MapDocument(mxdPath)
        MapDocElm = arcpy.mapping.ListLayoutElements(MapDoc, "PICTURE_ELEMENT")
        for elm in MapDocElm:
            if elm.name == "LogoMRCL_SloganGrisBlanc.emf":
                elm.sourceImage = r"\\10.13.77.8\MRC_doc\20_Territoires\20-4000_Schema_Amen_Dev\20-4100_Planification\20-4120_Projet_SAD\20-4121_Projet_SAD\Y_Cartographie\A_Planches\Fichiers_de_mise_en_page\Visuel\LogoMRCL_SloganGrisBlanc.emf"
            MapDoc.save()
arcpy.AddMessage(" End")

I tried the following script without looping it and it is working.

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
for elm in arcpy.mapping.ListLayoutElements(mxd,"PICTURE_ELEMENT"):
    if elm.name == "LogoMRCL_SloganGrisBlanc":
        elm.sourceImage = r"\\10.13.77.8\MRC_doc\20_Territoires\20-4000_Schema_Amen_Dev\20-4100_Planification\20-4120_Projet_SAD\20-4121_Projet_SAD\Y_Cartographie\A_Planches\Fichiers_de_mise_en_page\Visuel\LogoMRCL_SloganGrisBlanc.emf"

mxd.save
del mxd

I guess the problem is within the looping part of the script, not with the operation on picture element. Any clue?

EDIT: When the script is ran into PyCharm, this is what comes out:

C:\Python27\ArcGIS10.3\python.exe C:/Users/enclume/PycharmProjects/untitled/updatePictureLinkLoop.py
   MXD PROCESSING
   There are 2 mxd in: //10.13.77.8/MRC_doc/20_Territoires/20-4000_Schema_Amen_Dev/20-4100_Planification/20-4120_Projet_SAD/20-4121_Projet_SAD/Y_Cartographie/A_Planches/Mise_a_jour/Test
[<PictureElement object at 0x23dbcf0[0x23e54f8]>, <PictureElement object at 0xe8a67f0[0xe8ad930]>, <PictureElement object at 0xe8a6810[0xe8ad8e0]>, <PictureElement object at 0xe8a68d0[0xe8adb10]>, <PictureElement object at 0xe8a68f0[0xe8ad908]>, <PictureElement object at 0xe8a6cf0[0xe8adac0]>, <PictureElement object at 0xe8a6d10[0xe8b1d68]>, <PictureElement object at 0xe8a6d30[0xe8b1cf0]>, <PictureElement object at 0xe8a6d50[0xe8b1cc8]>, <PictureElement object at 0xe8a6d70[0xe8b1d90]>, <PictureElement object at 0xe8a6d90[0xe8b1db8]>, <PictureElement object at 0xe8a6db0[0xe8b1de0]>, <PictureElement object at 0xe8a6dd0[0xe8b6228]>, <PictureElement object at 0xe8a6df0[0xe8b6200]>, <PictureElement object at 0xe8a6e10[0xe8b6278]>, <PictureElement object at 0xe8a6e30[0xe8b6250]>, <PictureElement object at 0xe8a6e50[0xe8b6318]>, <PictureElement object at 0xe8a6ef0[0xe8b63b8]>]
[<PictureElement object at 0xe8a6f70[0xe8b69a8]>, <PictureElement object at 0xe8a6f30[0xe8b6980]>, <PictureElement object at 0xe8a6f90[0xe8b6958]>, <PictureElement object at 0xe8a6fb0[0xe8b6930]>, <PictureElement object at 0xe8a6f50[0xe8b6908]>, <PictureElement object at 0xe8a6fd0[0xe8b68e0]>, <PictureElement object at 0xe8a6ff0[0xe8b67f0]>, <PictureElement object at 0x23d7f70[0xe8b67c8]>, <PictureElement object at 0x23d7fb0[0xe8b67a0]>, <PictureElement object at 0xe8c0030[0xe8b6778]>, <PictureElement object at 0xe8c0050[0xe8b6750]>, <PictureElement object at 0xe8c0070[0xe8b6728]>, <PictureElement object at 0xe8c0090[0xe8b6700]>, <PictureElement object at 0xe8c00b0[0xe8b66d8]>, <PictureElement object at 0xe8c00d0[0xe8b6368]>, <PictureElement object at 0xe8c00f0[0xe8b6390]>, <PictureElement object at 0xe8c0110[0xe8b63e0]>, <PictureElement object at 0xe8c0130[0xe8b6408]>, <PictureElement object at 0xe8c01b0[0xe8b6480]>]
Traceback (most recent call last):
  File "C:/Users/enclume/PycharmProjects/untitled/updatePictureLinkLoop.py", line 27, in <module>
    MapDoc.save()
  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\utils.py", line 182, in fn_
    return fn(*args, **kw)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\_mapping.py", line 850, in save
    return convertArcObjectToPythonObject(self._arc_object.save(*gp_fixargs((), True)))
IOError: MapDocObject: Unable to save.  Check to make sure you have write access to the specified file and that there is enough space on the storage device to hold your document.

Process finished with exit code 1
  • What happens when you try to do this on a single MXD? I suspect that your error may not be to do with your loop so I think you should try to eliminate it from the code snippet that you present here. – PolyGeo Apr 20 '16 at 1:32
  • I guess the problem is in the "looping" part... – Rashomon Apr 20 '16 at 1:49
  • processingmxd.save() - where have you got a processingmxd?? I think this should be mxd.save() – Midavalo Apr 20 '16 at 1:58
  • 1
    There are a couple of answers on this page that deals with AttributeError: 'unicode' object has no attribute '_arc_object' – TsvGis Apr 20 '16 at 4:07
  • 1
    Print out if the length of the MapDocElm list. It may be that you do not have anything in that list. Also, print out the name of the picture element in the loop so you can see if your if statement is working. Can you guarantee that it always has been named the same? You might also change it to elm.name.upper() == "LogoMRCL_SloganGrisBlanc.emf".upper() to avoid any upper/lower case issues. Or you can instead check the sourceImage path against the old path. – dslamb Apr 20 '16 at 14:08

Your Answer

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

Browse other questions tagged or ask your own question.