Solved! Go to Solution.
import arcpy arcpy.env.workspace = r'< your workspace string (mine was a gdb) >' testdoc = r'< your mxd path >' mxd = arcpy.mapping.MapDocument(testdoc) df = arcpy.mapping.ListDataFrames(mxd, '< your dataframe where your DDP index resides >')[0] # set layer object reference to the index page lyr = arcpy.mapping.ListLayers(mxd, '< your index layer name >', df)[0] # create the appropriate field delimiters for the query string based on the workspace type indexPageField = arcpy.AddFieldDelimiters(arcpy.env.workspace, '< your index layer page fieldname >') # create the query, in this case, filtering for pages less than 6 (pgs 1-5) qry = indexPageField + ' < 6' # set the layer objects definition query lyr.definitionQuery = qry # finally, refresh the DDP -- this is the critical step which will crash if the query is unacceptable... mxd.dataDrivenPages.refresh() # here you can test the DDP by getting the pageCount, printing, exporting, etc... pgCnt = mxd.dataDrivenPages.pageCount print pgCnt # based on the query, results in 5 pages # success! # try changing the definition query to see if the DDP changes correspondingly qry = indexPageField + ' < 11' lyr.definitionQuery = qry mxd.dataDrivenPages.refresh() pgCnt = mxd.dataDrivenPages.pageCount print pgCnt # based on the changed query, results in 10 pages
I know I'm several years behind here, but I'm a little confused on the query above. The query is assuming the grids are in the same gdb, etc., and that way you just pull in the appropriate file within the gdb?
My problem is similar- I am trying to update the index layer dynamically in Python. I want to switch out my "dummy" index layer with the layer that gets fed to the mxd dynamically. Would I use a similar approach as above?
import arcpy, os, sys relpath = os.path.dirname(sys.argv[0]) arcpy.env.overwriteOutput = True ####Stript tool parameters feederID = arcpy.GetParameterAsText(0) ##Part 1 - Create subset to build features mxd = arcpy.mapping.MapDocument(relpath + r"\\FeederMap1.mxd") ##Reference appropriate layers and set definition queries POCLyr = arcpy.mapping.ListLayers(mxd, "Primary Overhead Conductor")[0] POCLyr.definitionQuery = "\"FEEDERID\" = '" + feederID + "'" # Execute GridIndexFeatures arcpy.GridIndexFeatures_cartography(relpath + r"/Scratch.gdb/DDPgrid", [POCLyr], "INTERSECTFEATURE", "", "", "10000 feet", "6500 feet") del mxd, POCLyr, PUCLyr, stationLyr ##Part 2 - Prepare Feeder Map and Export to PDF mxd2 = arcpy.mapping.MapDocument(relpath + r"\\FeederMap2.mxd") #Reference appropriate layers and set definition queries for lyr in arcpy.mapping.ListLayers(mxd2): if lyr.name == "Primary Overhead Conductor": lyr.definitionQuery = "\"FEEDERID\" = '" + feederID + "'" POCLyr = lyr ddp = mxd2.dataDrivenPages ddp.refresh() ddp.exportToPDF(relpath + r"\Feeder_" + feederID + ".pdf") del mxd2