Select to view content in your preferred language

Script wont export a specified range of data driven pages

2059
3
01-17-2013 03:10 AM
GeorgeChaka
Emerging Contributor
Hallo community. This is my first time to post something on this forum and I must say from the outset that I am a complete novice as far as python is concerned. I created a map document and have been trying to combine data driven pages with a title and overview pages that I also created and exported as PDF. I downloaded a script that would help me do the combination. I modified the script in a few places, added it to a toolbox. My script has two parameters, one to specifying the directory for the final pdf document and the other for making an entry for the range of data driven pages I would like to export from my map document. The trouble is, I cant get this script to export a specified range of pages. It always export all pages. Another thing is that I really dont know the which data type I should use for the parameter for the entry of a range of pages I would like to export. I will be very appreciative of some help. I have attached the code and a picture of the script .

George[ATTACH=CONFIG]20783[/ATTACH][ATTACH=CONFIG]20784[/ATTACH]

import arcpy, os, string, sys

#There are two parameters:
#     1) Output directory
#     2) Input page range
#
outDir = arcpy.GetParameterAsText(0)
pageRange = str(arcpy.GetParameterAsText(1))
 
folderNam = os.path.basename(outDir)
# Create a new, empty pdf document in the specified output directory
#
finalpdf_filename = folderNam + r"\ASF_MapBook.pdf"
if os.path.exists(finalpdf_filename):
  os.remove(finalpdf_filename)
finalPdf = arcpy.mapping.PDFDocumentCreate(finalpdf_filename)

# Add the title page to the pdf
#
finalPdf.appendPages(r"C:\ArcGIS Tutor\DataDrivenPages\TitleOverview\TitlePage.pdf")

# Add the index map to the pdf
#
finalPdf.appendPages(r"C:\ArcGIS Tutor\DataDrivenPages\TitleOverview\OverviewPage.pdf")

# Export the Data Driven Pages to a temporary pdf and then add it to the
# final pdf. Alternately, if your Data Driven Pages have already been
# exported, simply append that document to the final pdf.

mxdPath = r"C:\ArcGIS Tutor\DataDrivenPages\DataDrivenPages_MapBook.mxd"
for tempMap in pageRange:
  tempMap = arcpy.mapping.MapDocument(mxdPath)
  temp_filename = r"C:\temp\tempDDP.pdf"
if os.path.exists(temp_filename):
  os.remove(temp_filename)
tempDDP = tempMap.dataDrivenPages
tempDDP.exportToPDF(temp_filename)
finalPdf.appendPages(temp_filename)

# Update the properties of the final pdf
#
finalPdf.updateDocProperties(pdf_open_view="USE_THUMBS",
                             pdf_layout="SINGLE_PAGE")

# Save your result
#
finalPdf.saveAndClose()

# Delete variables
#
del finalPdf, tempMap, tempDDP
0 Kudos
3 Replies
by Anonymous User
Not applicable
Original User: jimcousins

George,
Eliminate the loop for the page range, as DataDrivePages.exportToPDF takes a page range string as an argument. This will simplify your script, and should get you the subset of pages that you are looking for.
Regards,
Jim
0 Kudos
GeorgeChaka
Emerging Contributor
Jim,

I did eliminate the loop and the run the script from my tool. It still didn't give me the subset of pages I wanted. It still gave me all pages. I changed the code by putting in a line to get the value of the page range input parameter and referred to that value in the DataDrivenPages.exportToPDF line. See the code below. I ran the script from my tool, this time it gave me a subset of pages. I am not sure if the changes I made is a most appropriate way to do it.

Kind regards,

George


import arcpy, os, string, sys

#There are two parameters:
#     1) Output directory
#     2) Input page range
#
outDir = arcpy.GetParameterAsText(0)
pageRange = str(arcpy.GetParameterAsText(1))
 
folderNam = os.path.basename(outDir)
range = os.path.basename(pageRange)

# Create a new, empty pdf document in the specified output directory
#
finalpdf_filename = folderNam + r"\ASF_MapBook.pdf"
if os.path.exists(finalpdf_filename):
  os.remove(finalpdf_filename)
finalPdf = arcpy.mapping.PDFDocumentCreate(finalpdf_filename)

# Add the title page to the pdf
#
finalPdf.appendPages(r"C:\ArcGIS Tutor\DataDrivenPages\TitleOverview\TitlePage.pdf")

# Add the index map to the pdf
#
finalPdf.appendPages(r"C:\ArcGIS Tutor\DataDrivenPages\TitleOverview\OverviewPage.pdf")

# Export the Data Driven Pages to a temporary pdf and then add it to the
# final pdf. Alternately, if your Data Driven Pages have already been
# exported, simply append that document to the final pdf.

mxdPath = r"C:\ArcGIS Tutor\DataDrivenPages\DataDrivenPages_MapBook.mxd"
tempMap = arcpy.mapping.MapDocument(mxdPath)
temp_filename = r"C:\temp\tempDDP.pdf"
if os.path.exists(temp_filename):
  os.remove(temp_filename)
tempDDP = tempMap.dataDrivenPages
tempDDP.exportToPDF(temp_filename, "RANGE", range)
finalPdf.appendPages(temp_filename)

# Update the properties of the final pdf
#
finalPdf.updateDocProperties(pdf_open_view="USE_THUMBS",
                             pdf_layout="SINGLE_PAGE")

# Save your result
#
finalPdf.saveAndClose()

# Delete variables
#
del finalPdf, tempMap, tempDDP
0 Kudos
by Anonymous User
Not applicable
Original User: jimcousins

George, your script ran correctly for me using the pageRange parameter directly.
Jim
0 Kudos