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")
Solved! Go to Solution.
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()
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()
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()
...
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()
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
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.