I am trying to find a way to update the index layer for data driven pages through arcpy. I understand that to access DDP from arcpy.mapping you need to be referencing a MXD that has DDP enabled. My problem is that I am trying to create DDP for a feature class I just created from the XML to Geodatabase import tool and use that as my index layer instead of the scratch layer I created to enable DDP. The MXD contains a layout I would ultimately like to use when exporting my DDP. Here is my code so far.

# Import arcpy module
import arcpy, os
from arcpy import env

env.overwriteOutput = True
env.workspace = r"C:\Users\spadgett\Desktop\ArcScrap\Tyler_TEST.gdb"

# Script parameter
xml = arcpy.GetParameterAsText(0)

# Hardcoded GDB for storing xml
Tyler_TEST_gdb = "C:\\Users\\spadgett\\Desktop\\ArcScrap\\Tyler_TEST.gdb"

# execute xml to geodatabase tool
arcpy.AddMessage("Executing XML to Geodatabase")
arcpy.ImportXMLWorkspaceDocument_management(Tyler_TEST_gdb, xml, "DATA", "")

#create feature layer for points
FCpoints = "Control_Point_Name"
arcpy.AddField_management(FCpoints, "SCALE", "SHORT")
arcpy.CalculateField_management(FCpoints, "SCALE", "1200", "PYTHON")
arcpy.AddMessage("Points feature class added with SCALE field")


# Set environment and MXD
mxd = arcpy.mapping.MapDocument(r"C:\Users\spadgett\Desktop\ArcScrap\Tyler Sample\Tyler_Test.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]

layer = arcpy.mapping.Layer("Points2")
arcpy.mapping.AddLayer(df, layer, "TOP")

RefPoints = r"C:\Users\spadgett\Desktop\ArcScrap\LYR\Points.lyr"
updateLayer = arcpy.mapping.ListLayers(mxd)[0]
arcpy.ApplySymbologyFromLayer_management (updateLayer, RefPoints)

updateLayerOld = arcpy.mapping.ListLayers(mxd)[1]

arcpy.mapping.UpdateLayer(df, updateLayerOld, updateLayer, False)

#Create JPG of DDP for Points name
##mxd = arcpy.mapping.MapDocument("CURRENT")
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
    mxd.dataDrivenPages.currentPageID = pageNum
    pageName = mxd.dataDrivenPages.pageRow.Name
    arcpy.mapping.ExportToJPEG(mxd, r"C:\Users\spadgett\Desktop\ArcScrap\Tyler Sample\Images\AGM_" + str(pageName) + ".jpg")

Running this code ends up with an error at the UpdateLayer line. Switching the boolean to True just changes the symbology, just as the help document says.

Any ideas?

Error is

Traceback (most recent call last):
  File "C:\Users\spadgett\Desktop\ArcScrap\Tyler Sample\python\AGM_Report.py", line 59, in <module>
    arcpy.mapping.UpdateLayer(df, updateLayerOld, updateLayer, False)
  File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\utils.py", line 181, in fn_
    return fn(*args, **kw)
  File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\mapping.py", line 1876, in UpdateLayer
    tc.ReplaceLayer(tl, rl)
ValueError: DataFrameObject: Unexpected error

closed as off-topic by PolyGeo Jul 16 '17 at 1:11

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Welcome to GIS SE! As a new user be sure to take the Tour. Rather than saying "ends up with an error at the UpdateLayer line" it would be helpful if you could include the precise text of that error. There is an edit button beneath your question that enables you top provide additional/revised details at any time. – PolyGeo May 27 '15 at 21:52
  • Curious, maybe I'm missing something, but if the schema isn't anything unexpected to confound the indexing as DDP is expecting, then why can't you simply replace the dataSource instead of all the layer obj manipulations? – T. Wayne Whitley May 27 '15 at 23:25
  • @T.WayneWhitley thats a really good idea. I'll give that a try tomorrow. That never crossed my mind. – Spencer P May 28 '15 at 2:13