3

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
  • I'm not sure I completely understand what you're after, but you can print selected pages based on selected features in your index layer, using page_range_type = 'SELECTION' in printPages method. – phloem Dec 19 '14 at 18:21
2

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)
2

Following on Phloem's comment about using arguments in the printPages() method, and echoing Emil's answer as well:

My gut reaction says we need to select the desired active page by setting the currentPageID property in the mxd.dataDrivenPages object, and then run the mxd.dataDrivenPages.printPages() method to send the active page to the printer. I'm thinking the tail-end of your script would look something like this:

targetPageName = arcpy.GetParameterAsText(3)

mxd = arcpy.mapping.MapDocument(r"M:\CCAO_GIS_Projects\Library_Districts\MXD\Lib_Test_new.mxd")
mxd.dataDrivenPages.getPageIDFromName(targetPageName)
mxd.dataDrivenPages.currentPageID = targetPageID
mxd.dataDrivenPages.printPages(r"HP Color LaserJet 2600n (Copy 1)", page_range_type = "CURRENT")
mxd.dataDrivenPages.refresh()
del mxd
  • This looks good too. I'm curious if both methods work, or have any differences. – Emil Brundage Dec 19 '14 at 18:35
  • I'm thinking the end-result will be the same in terms of the printed page, hopefully both will work - Emil's method saves the step of changing the currentPageID and having the map document refresh - in the end feeding the printPages() method a range is more succinct than refreshing the document I would think. – Jim Dec 19 '14 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.