4

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
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Dec 19, 2014 at 17:57
1
  • 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. Commented Dec 19, 2014 at 18:21

2 Answers 2

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)
answered Dec 19, 2014 at 18:27
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
answered Dec 19, 2014 at 18:29
2
  • This looks good too. I'm curious if both methods work, or have any differences. Commented Dec 19, 2014 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. Commented Dec 19, 2014 at 18:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.