How to replace a "Picture_Element" in multiple Mxd's?

2917
5
Jump to solution
06-22-2015 04:07 PM
G__EricCastillo_Morales
New Contributor III

HI, I am trying to loop this script in 35 mxd's, in different folders, but I don't know how. Can someone help me?

Eric

import arcpy

mxd = arcpy.mapping.MapDocument(r"D:\Test\D1.mxd")

locfinal = arcpy.mapping.ListLayoutElements(mxd,"PICTURE_ELEMENT","Localización2")

for object in locfinal:

    object.sourceImage = r"D:\Test\Picture\test_picture.jpg"

    object.name = "loc mun estatal"

    object.elementHeight = 9.4827

    object.elementWidth = 11.99

    object.elementPositionX = 77.0

    object.elementPositionY = 94.0

mxd.saveACopy(r"D:\Test\D1copy.mxd")

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Oops... I forgot to call the function that changes the picture element and saves a copy (see line 17)... Did a small test and it seems to work. Please note that there is no error checking what so ever in the code.

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import arcpy

import os

def main():

    # path to folder where all mxd files reside

    path = r"D:\Test"

    # recursive loop through folder and sub folders

    for root, dirs, files in os.walk(path):

        for filename in files:

            if filename.endswith('.mxd'):

                # use only mxd files

                mxd_in = os.path.join(root, filename)

                print"Processing: {0}".format(mxd_in)

                updateLayout(mxd_in)

def updateLayout(mxd_in):

    path, name_ext = os.path.split(mxd_in)

    name, ext = os.path.splitext(name_ext)

    mxd_out = os.path.join(path, "{0}Copy.{1}".format(name, ext))

    mxd = arcpy.mapping.MapDocument(mxd_in)

    locfinal = arcpy.mapping.ListLayoutElements(mxd,"PICTURE_ELEMENT","Localización2")

    for object in locfinal:

        object.sourceImage = r"D:\Test\Picture\test_picture.jpg"

        object.name = "loc mun estatal"

        object.elementHeight = 9.4827

        object.elementWidth = 11.99

        object.elementPositionX = 77.0

        object.elementPositionY = 94.0

    mxd.saveACopy(mxd_out)

    del mxd

if __name__ == '__main__':

    main()

View solution in original post

5 Replies
XanderBakker
Esri Esteemed Contributor

You could probably use the os.walk to loop through the mxd's like shown in the code below.

Please note that the code has not been tested and is based on a number of assumptions.

Change your path to the mxd's on line 8. The code will loop through that folder and all sub folders and process all mxd's it will find.

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import arcpy

import os

def main():

    # path to folder where all mxd files reside

    path = r"C:\Forum"

    # recursive loop through folder and sub folders

    for root, dirs, files in os.walk(path):

        for filename in files:

            if filename.endswith('.mxd'):

                # use only mxd files

                mxd_in = os.path.join(root, filename)

def updateLayout(mxd_in):

    path, name_ext = os.path.split(mxd_in)

    name, ext = os.path.splitext(name_ext)

    mxd_out = os.path.join(path, "{0}Copy.{1}".format(name, ext))

    mxd = arcpy.mapping.MapDocument(mxd_in)

    locfinal = arcpy.mapping.ListLayoutElements(mxd,"PICTURE_ELEMENT","Localización2")

    for object in locfinal:

        object.sourceImage = r"D:\Test\Picture\test_picture.jpg"

        object.name = "loc mun estatal"

        object.elementHeight = 9.4827

        object.elementWidth = 11.99

        object.elementPositionX = 77.0

        object.elementPositionY = 94.0

    mxd.saveACopy(mxd_out)

    del mxd

if __name__ == '__main__':

    main()

0 Kudos
G__EricCastillo_Morales
New Contributor III

hi, I change the path, but nothing happen

>>> #!/usr/bin/env python

... # -*- coding: utf-8 -*-

... import arcpy

... import os

...

... def main():

...     # path to folder where all mxd files reside

...     path = r"D:\Test"

...

...     # recursive loop through folder and sub folders

...     for root, dirs, files in os.walk(path):

...         for filename in files:

...             if filename.endswith('.mxd'):

...                 # use only mxd files

...                 mxd_in = os.path.join(root, filename)

...

... def updateLayout(mxd_in):

...     path, name_ext = os.path.split(mxd_in)

...     name, ext = os.path.splitext(name_ext)

...     mxd_out = os.path.join(path, "{0}Copy.{1}".format(name, ext))

...

...     mxd = arcpy.mapping.MapDocument(mxd_in)

...     locfinal = arcpy.mapping.ListLayoutElements(mxd,"PICTURE_ELEMENT","Localización2")

...     for object in locfinal:

...         object.sourceImage = r"D:\Test\Picture\test_picture.jpg"

...         object.name = "loc mun estatal"

...         object.elementHeight = 9.4827

...         object.elementWidth = 11.99

...         object.elementPositionX = 77.0

...         object.elementPositionY = 94.0

...     mxd.saveACopy(mxd_out)

...     del mxd

...

... if __name__ == '__main__':

...     main()

...  

0 Kudos
XanderBakker
Esri Esteemed Contributor

Oops... I forgot to call the function that changes the picture element and saves a copy (see line 17)... Did a small test and it seems to work. Please note that there is no error checking what so ever in the code.

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import arcpy

import os

def main():

    # path to folder where all mxd files reside

    path = r"D:\Test"

    # recursive loop through folder and sub folders

    for root, dirs, files in os.walk(path):

        for filename in files:

            if filename.endswith('.mxd'):

                # use only mxd files

                mxd_in = os.path.join(root, filename)

                print"Processing: {0}".format(mxd_in)

                updateLayout(mxd_in)

def updateLayout(mxd_in):

    path, name_ext = os.path.split(mxd_in)

    name, ext = os.path.splitext(name_ext)

    mxd_out = os.path.join(path, "{0}Copy.{1}".format(name, ext))

    mxd = arcpy.mapping.MapDocument(mxd_in)

    locfinal = arcpy.mapping.ListLayoutElements(mxd,"PICTURE_ELEMENT","Localización2")

    for object in locfinal:

        object.sourceImage = r"D:\Test\Picture\test_picture.jpg"

        object.name = "loc mun estatal"

        object.elementHeight = 9.4827

        object.elementWidth = 11.99

        object.elementPositionX = 77.0

        object.elementPositionY = 94.0

    mxd.saveACopy(mxd_out)

    del mxd

if __name__ == '__main__':

    main()

View solution in original post

G__EricCastillo_Morales
New Contributor III

The script work !

thanks for your help.

I notice that the script run fine in python window, however when I run it in pyscripter don´t work, I mean when I check the copy  (D1Copy..mxd) the old picture remains.

Any way thanks.

Eric

0 Kudos
XanderBakker
Esri Esteemed Contributor

Interesting..., I could probably change the code a bit to run within ArcMap (because I assume this must be possible), but if you have no problem running it as standalone code. then let's leave it as it is.

0 Kudos