Select to view content in your preferred language

Trying to concatenate 3 parameters to create a DDP page name to export PDF

656
2
04-24-2014 06:51 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 concatenate 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 24.

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")
df = mapping.ListDataFrames(mxd, "Layers") [0]
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, df, ddp, pageID
Tags (2)
0 Kudos
2 Replies
DouglasSands
Occasional Contributor II
The first thing I see is that your input parameters have some unclosed parenthesis. Also, I would recommend trying to print out the values of newpage and pageID as you go. It might yield some good insights on what is happening. Also since all you do with your Data Frame object (df) is create it and then delete it you can probably skip creating it all together.

- Doug
0 Kudos
MollyWatson
New Contributor III
Thanks Doug. I removed the extra parenthesis.  Those were left over when I was experimenting making the parameters a string and I forgot to delete them. I added the print statements and ran the script in python.  My print statement = 1.  Then I get an error on the Export to PDF function:

1

Traceback (most recent call last):
  File "V:\gislu\_BasemapMXD\STRMapSearch.py", line 33, in <module>
    ddp.exportToPDF(finalPdf, 'CURRENT', resolution = 200)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\utils.py", line 181, in fn_
    return fn(*args, **kw)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\utils.py", line 181, in fn_
    return fn(*args, **kw)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\_mapping.py", line 446, in exportToPDF
    return convertArcObjectToPythonObject(self._arc_object.exportToPDF(*gp_fixargs((out_pdf, page_range_type, page_range_string, multiple_files, resolution, image_quality, colorspace, compress_vectors, image_compression, picture_symbol, convert_markers, embed_fonts, layers_attributes, georef_info, jpeg_compression_quality, show_selection_symbology), True)))
AttributeError: PageLayoutObject: Error in exporting pages
>>>
0 Kudos