3

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')
  • 1
    Please include code as text rather than as a picture so that it can be searched. There is a format button to make it look nice. – PolyGeo Feb 5 '16 at 19:38
7

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?

  • Hi thanks for the reply! I think you are a few levels above me in terms of Python as I did not even write the script I copied above. What do I need to do to the script that you have provided? Do I combine it with my original script or just copy it in to the python window in the geoprocessing drop down menu? Sorry for my incompetence! – Harry Catharell-Hargreaves Feb 5 '16 at 14:53
  • 2
    @HarryCatharell-Hargreaves To run the script, copy the text into a text file (notepad or something) and save it with a .py extension. Then you can add it into ArcMap by doing this: desktop.arcgis.com/en/arcmap/10.3/analyze/creating-tools/… – Sara Barnes Feb 5 '16 at 15:01
  • 2
    @HarryCatharell-Hargreaves If this answered your question, please mark it as "answered." You can learn more here: gis.stackexchange.com/tour – Sara Barnes Feb 8 '16 at 13:43
  • 1
    What would be data type for this parameters while setting in arctoolbox. mxd = arcpy.GetParameterAsText(0) PDFpath = arcpy.GetParameterAsText(1) PDFname = arcpy.GetParameterAsText(2) – GIS Data Butcher Apr 19 '16 at 12:15
  • 1
    @GISDataButcher They should both be text strings. – Sara Barnes Apr 19 '16 at 12:18
0

My code proposal :

import arcpy

# Set Input Parameters
mxd = r"C:\TEST\DATADRIVEN\Data.mxd"
PDFpath = r"C:\TEST\DATADRIVEN\Testing.pdf"
dpi = "200"
res = int(dpi)
quality = "BEST"

# Create an MXD object
mxd_doc = arcpy.mapping.MapDocument(mxd)
mxd_doc.dataDrivenPages.exportToPDF(PDFpath, resolution=res, image_quality=quality, colorspace="RGB", picture_symbol="VECTORIZE_BITMAP", layers_attributes="NONE")
del mxd_doc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.