[GIS] How to print selected page from Data Driven Pages using ArcPy

arcgis-desktoparcpydata-driven-pages

I am looking to print DDP (Data Driven Pages) to a printer through a Python (arcpy) script which I will create in Arc Catalog. In other words, I would like for the script to have the following parameters:

  • Select the MXD,
  • Select the feature class,
  • SQL Expression (to select an attribute),
  • Then a Target Field (such as the name of the column in a field).

I have a small script which I wrote, but it prints the entire contents DDP in the library layer instead of letting a user select only a specific page in DDP.

How can I or a user select one page and print the selected page?
Additionally, what parameters should be set in the script?

import arcpy
from arcpy import env
#
# Set the workspace
env.workspace = arcpy.GetParameterAsText(0)
dir = env.workspace
#
#
# Local Variables
input = arcpy.GetParameterAsText(1)
expression = arcpy.GetParameterAsText(2)
#
#
# Make a layer from the input feature class
arcpy.MakeFeatureLayer_management(r'Database Connections\gis_sql3.sde\gis_parcels.MP.Districts\gis_parcels.MP.Library', "lib_lyr")
#
#
# Within selected features, further select based on a SQL query within the script tool
arcpy.SelectLayerByAttribute_management("lib_lyr", "SUBSET_SELECTION", expression)
#
#
#
mxd = arcpy.mapping.MapDocument (r"M:\CCAO_GIS_Projects\Library_Districts\MXD\Lib_Test_new.mxd")
mxd.dataDrivenPages.getPageIDFromName("LIBRARY")
# As of now this prints only the first page
mxd.dataDrivenPages.pageNameField.name = targetPageName
mxd.dataDrivenPages.printPages(r"HP Color LaserJet 2600n (Copy 1)", page_range_type = "CURRENT")
mxd.dataDrivenPages.refresh()
del mxd

Best Answer

Here's a snippet of code that might work for you. the variable 'pageName' is the name of the page to be printed.

pageName = "page1"

mxd = arcpy.mapping.MapDocument (r"M:\CCAO_GIS_Projects\Library_Districts\MXD\Lib_Test_new.mxd")
pageIndex = mxd.DataDrivenPages.getPageIDFromName (pageName)
mxd.DataDrivenPages.printPages ("HP Color LaserJet 2600n (Copy 1)", "RANGE", pageIndex)