[GIS] Export all data driven pages to PDF using Python script

arcpydata-driven-pages

I am a novice Python user but I am trying to create a script which will allow me to export all of the data driven pages within an mxd to a single pdf file (without having to open the mxd). I currently have the script below which works for exporting single pages. And the highlighted section was my initial (poor) attempt to export all pages based on what I have seen or read.

# Python script to run in windows.
# 1) Copy script into folder to run script
# 2) Edit quality and dpi variables if desired

import arcpy, os

# get current directory python script is in
cwd = os.getcwd()

# quality to export, BEST, BETTER, NORMAL, FASTER, FASTEST 
quality = "BEST"
# dots per inch resolution
dpi = "300"
res = int(dpi)

# Ask user to confirm settings and directory
print "Export ArcMap MXD(s) to PDF(s), " + quality + " Quality, " + dpi + "dpi to " + cwd + "?"

# pauses for user to press key to continue
os.system('pause')

# set folder path to current directory
folderPath = cwd

# prints message to console for user
print "IN PROGRESS...PLEASE WAIT..."

# counter to count number of PDFs
n = 0

# create fullpath from filename and folder path
for filename in os.listdir(folderPath):  
    fullpath = os.path.join(folderPath, filename)
# extract file extension
    if os.path.isfile(fullpath):  
        basename, extension = os.path.splitext(fullpath)  
        if extension.lower() == ".mxd":
# advance counter to count PDFs
            n = n + 1
            mxd = arcpy.mapping.MapDocument(fullpath)
# export tool with parameters
            arcpy.mapping.ExportToPDF(mxd, basename + '.pdf', resolution=res, image_quality=quality, colorspace="RGB", picture_symbol="VECTORIZE_BITMAP", layers_attributes="NONE")
            print basename + "...exported"

            del mxd

# prints message to console for user
nstr = str(n)
print nstr + " pdf(s) exported to " + folderPath
print "DONE"

# pauses for user to press key to continue
os.system('pause')

Best Answer

I am a novice in Python as well, but I have a script that will do what it sounds like you are trying to do - export all pages of an MXD with Data Driven Pages to one PDF.

I run this through the toolbox in ArcCatalog or ArcMap. The MXD you are running it on does not have to be open.

#Set Input Parameters
mxd = arcpy.GetParameterAsText(0) 
PDFpath = arcpy.GetParameterAsText(1) 
PDFname = arcpy.GetParameterAsText(2)

#Create an MXD object
mxd_doc = arcpy.mapping.MapDocument(mxd) 

#Export to DDP 
ddp = mxd_doc.dataDrivenPages 
ddp.exportToPDF(PDFpath + r"\\" + PDFname + ".pdf", "ALL") 
del mxd, mxd_doc, PDFname, PDFpath

I developed this code based on my question here: How to Prevent Data Driven Pages From Hanging on Subsequent Export?

Related Question