Export mxd's and Data Driven Pages to PDF

565
5
12-21-2011 11:32 AM
DanielHofer
New Contributor
Hi,

I got this code below from
http://resources.arcgis.com/gallery/file/geoprocessing/details?entryID=3EA2A85B-1422-2418-7F00-29AE1...

It works great.  But I need it to not combine the pdfs.  I'm not a pythoner yet.  Could someone help me modify this code.  Thanks
Daniel Hofer



import arcpy, os

path = os.curdir
PDFDir = os.path.join(os.curdir, "PDFs")

def ExportToPDF (fileName):

    if not os.path.exists(PDFDir):
        os.mkdir(PDFDir);
   
    OutputPDFPath = os.path.join(PDFDir, fileName.replace(".mxd", ".pdf"))
    if os.path.exists(OutputPDFPath):
        os.remove(OutputPDFPath)

    mxd = arcpy.mapping.MapDocument(fileName)
   
    if (hasattr(mxd, 'dataDrivenPages')):
        outPDF = arcpy.mapping.PDFDocumentCreate(OutputPDFPath)

        for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
            mxd.dataDrivenPages.currentPageID = pageNum           
            arcpy.mapping.ExportToPDF(mxd, os.path.join(PDFDir, "Temp.pdf"))
            outPDF.appendPages(os.path.join(PDFDir, "Temp.pdf"))
            os.remove(os.path.join(PDFDir, "Temp.pdf"))               
            print pageNum
        del outPDF
       
    else:
        arcpy.mapping.ExportToPDF(mxd, OutputPDFPath)
   
    print OutputPDFPath
    return True

print path
for subdir, dirs, files in os.walk(path):
    for file in files:
        if (file.endswith(".mxd") == True):
            ExportToPDF (file)
0 Kudos
5 Replies
NinaRihn
Occasional Contributor III
I'm not a pythoner either, but have you checked what would happen if you commented out this line:  outPDF.appendPages(os.path.join(PDFDir, "Temp.pdf")) ?
0 Kudos
DanielHofer
New Contributor
Thanks for the response.  One of my coworkers came up with the following that seems to work.

import arcpy, os

path = os.curdir
PDFDir = os.path.join(path, "PDFs")

def ExportToPDF (fileName):

    if not os.path.exists(PDFDir):
        os.mkdir(PDFDir);
    mxd = arcpy.mapping.MapDocument(os.path.join(path,fileName))
    print mxd.filePath
    if (hasattr(mxd, 'dataDrivenPages')):
        print "*** Starting DDP export " + str(mxd.dataDrivenPages.pageCount) + " ***"
        for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
            OutputPDFPath = os.path.join(PDFDir, fileName.replace(".mxd", "")) + str(pageNum) + ".pdf"
            print OutputPDFPath
            if os.path.exists(OutputPDFPath):
                os.remove(OutputPDFPath)
            mxd.dataDrivenPages.currentPageID = pageNum           
            arcpy.mapping.ExportToPDF(mxd, OutputPDFPath)
            print pageNum
    else:
        OutputPDFPath = os.path.join(PDFDir, fileName.replace(".mxd", ".pdf"))
        print OutputPDFPath
        if os.path.exists(OutputPDFPath):
            os.remove(OutputPDFPath)
    return True

print path
for subdir, dirs, files in os.walk(path):
    for file in files:
        if (file.endswith(".mxd") == True):
            print str(file)
            ExportToPDF (file)
0 Kudos
DanielHofer
New Contributor
However, now it is exporting out individual pdfs and they are labeled with mxdname+page number.  Like ALB1.pdf,  ALB2.pdf, ALB3.pdf

where as I need it to use the page name instead of the number like
ALB_California.pdf, ALB_Colorado.pdf, ALB_Texas.pdf

Any suggestions?
0 Kudos
DanielHofer
New Contributor
No solution yet, but this is interesting.  With a DDP mxd go file export and hit the pages tab.   �??Multiple PDF Files (page name)�?�   The code must be there to exploit without doing that field feature class stuff.    Screen shot attached.
0 Kudos
JeffBarrette
Esri Regular Contributor
Have you tried the DDP.exportToPDF function?  It has a parameter called mulitple_files.  I think you would want to set the value to "PDF_MULTIPLE_FILES_PAGE_NAME".

Jeff
0 Kudos