Is there a way to dynamically pull in a new index layer for a data driven page in ArcMap?
Scenario: I have an MXD which will be my template. 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 by the user/my Python script.
-
Yes, I have successfully achieved this in the past, its achievable if your dummy index layer and the one that gets fed in use the same field names that are used in the DDP setup. You can look up the docs for updating a layers source (your ddp layer) - desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-mapping/… and then you need to refresh the ddp layer using arcpy also available in the docs desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-mapping/…Clubdebambos– Clubdebambos2021年05月25日 07:49:27 +00:00Commented May 25, 2021 at 7:49
1 Answer 1
Here's snippets from my previous endeavour. My template mxd contained a templated DDP feature class/layer. The new DDP contained the same schema. Look up the ESRI docs for any of the methods used below.
import arcpy
import arcpy.mapping as m
mxd = m.MapDocument("mxd_path")
## make sure your saved mxd already had ddp enabled
ddp = mxd.dataDrivenPages
## get the ddp layer
ddp_lyr = m.ListLayers(mxd, "ddp_lyr_name", "df_name")[0]
## replace the datasource with another feature class
ddp_lyr.replaceDataSource("path_to_workspace", "FILEGDB_WORKSPACE", "new_ddp_fc_name")
ddp.refresh()
## to save the mxd with a the updated ddp layer
## or use mxd.saveACopy() to keep your template and save another mxd.
mxd.save()