Select to view content in your preferred language

Data Driven Pages - PICTURE_ELEMENT - SourceImage changes

2008
1
05-03-2011 02:52 PM
AndraBobbitt
Deactivated User
Using a previous script posted on this forum, I've gotten the data-driven layouts to print out pdfs with an image (PICTURE_ELEMENT) that changes based on an attribute of the index layer storing the image name (using sourceImage).  However, the ArcMap displayed pages do not update the image source and continues to use the first image. 

Is the PICTURE_ELEMENT supposed to change on the layouts for each data driven page as well as the output to pdfs?

Here is the sort-of working code:

import arcpy
mxd = arcpy.mapping.MapDocument(r"G:\NEPacific\Data-types\maps\andra-datadriven-test-map.mxd")
imagePath = "G:\\NEPacific\Data-types\Vent-locations\Axial\images\\"
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
mxd.dataDrivenPages.currentPageID = pageNum
imageName = mxd.dataDrivenPages.pageRow.getValue("image4map")
for elm in arcpy.mapping.ListLayoutElements(mxd,"PICTURE_ELEMENT"):
  if elm.name == "photo1":
   nextImage = (str(imagePath) + str(imageName))
   elm.sourceImage = nextImage
print "Exporting Page {0} of {1}".format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount))
arcpy.mapping.ExportToPDF(mxd, r"G:\NEPacific\Data-types\maps\andra-datadriven-test-map.mxd" + str(pageNum) + ".pdf")
del mxd
0 Kudos
1 Reply
JeffBarrette
Esri Regular Contributor
Here is your corrected code that was on the previous post.  It is much easier to read when
 tags are wrapped around it via these forum posts. See below.

Your code suggests to me that you are running it as a stand-alone script because you are importing arcpy and NOT using the keyword CURRENT.  I take it that your exported PDFs do have the correct images.  If you want to run your code in the Python window and see ArcMap update from page to page while your script is running then you would need to use arcpy.RefreshActiveView().  I also suggest that you insert a time delay.

Jeff

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Temp\Photo.mxd")
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
  mxd.dataDrivenPages.currentPageID = pageNum
  imageName = mxd.dataDrivenPages.pageRow.getValue("image4map")
  for elm in arcpy.mapping.ListLayoutElements(mxd,"PICTURE_ELEMENT"):
    if elm.name == "photo1":
      elm.sourceImage = "C:\\Temp\\" + imageName
      arcpy.mapping.ExportToPDF(mxd, r"C:\Temp\\" + str(pageNum) + ".pdf")
del mxd
0 Kudos