Trying to combine 3 variables to export data driven page by page name

926
6
Jump to solution
04-24-2014 08:58 AM
MollyWatson
New Contributor III
I am trying to write a Python script so the user can export a PDF map from an MXD using data driven pages without the user having to open the MXD and scroll through all the data driven pages to find the page they need. The Index Grid is the Section, Township, Range polygon. The page name looks like this in the attribute table: T1N R70W S12.

In order to make it easier for the user, I broke up the STR into 3 separate parameters that have value lists so they can just pick their value from a drop down. The parameters are strings. I'm running into problems trying to combine the 3 parameters into 1 string so the data driven pages can get the page ID from the page name. The script runs with no errors, however, it also exports the firstpage in the mxd. I can't get it to use the user defined parameters to identify the page name. I believe the issue is in line 23.

Here is the script:

# Setup
# Import arcpy module
import sys, string, os, arcpy
from arcpy import env
from arcpy import mapping
from os import sep as bs

# Script arguments
FOLDER = arcpy.GetParameterAsText(0)
TNSHP = arcpy.GetParameterAsText(1)
RNG = arcpy.GetParameterAsText(2)
SCTN = arcpy.GetParameterAsText(3)

#Set workspace
arcpy.env.overwriteOutput = True
outPath = FOLDER + bs
arcpy.env.workspace = outPath

# Select and Zoom to Section Township Range
mxd = mapping.MapDocument(r"V:\gislu\_BasemapMXD\Assessor24x36.mxd")
ddp = mxd.dataDrivenPages
arcpy.AddMessage("Finding Township Range Section")
newpage = TNSHP + " " + RNG + " " + SCTN
pageID = ddp.getPageIDFromName(newpage)
ddp.currentPageID = pageID

#Export PDF
finalPdf = outPath + "T" + TNSHP + "R" + RNG + "S" + SCTN + "ParcelMap.pdf"
arcpy.AddMessage("Exporting PDF")
ddp.exportToPDF(finalPdf, 'CURRENT', resolution = 200)
arcpy.AddMessage("DONE!")

del mxd, finalPdf, ddp, pageID
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MollyWatson
New Contributor III
Thanks for the suggestion. I figured out the issue. There were trailing zeros in the index layer field so while it appeared the strings matched they actually did not.  I used another index layer that does not have trailing zeros and now it works.

View solution in original post

0 Kudos
6 Replies
MathewCoyle
Frequent Contributor
First, please read this and follow the posting guidelines.
http://forums.arcgis.com/threads/48475-Please-read-How-to-post-Python-code

Also, do you get the expected results from this?
arcpy.AddMessage(newpage)


And what version of ArcGIS are you using?
0 Kudos
MollyWatson
New Contributor III
I had a suggestion from a co-worker to make the parameters string using the following:

STR = TNSHP + " " + RNG + " " + SCTN
STRQuoted = "\'"+ STR + "\'"

then the page ID would use the STR so
pageID = ddp.getPageIDFromName(STRQuoted)
ddp.currentPageID = pageID

This still does not work. It exports the firsts page instead of the page defined by the parameters.
0 Kudos
MollyWatson
New Contributor III
When I add a message for the newpage the string matches the string in the index layer table for the page name.  I am using ArcGIS version 10.2
0 Kudos
MollyWatson
New Contributor III
I added another message to get the pageID.  Even though the page name string matches the page name in the index layer table, the pageID is always 0.
0 Kudos
MathewCoyle
Frequent Contributor
I would try refreshing your ddp and saving as a new mxd. Also try setting your currentPageID to 1 or 2 or some other value to make sure you can change it.
0 Kudos
MollyWatson
New Contributor III
Thanks for the suggestion. I figured out the issue. There were trailing zeros in the index layer field so while it appeared the strings matched they actually did not.  I used another index layer that does not have trailing zeros and now it works.
0 Kudos