Export multiple mxds to pdf

2130
4
03-11-2012 06:35 PM
LynnKanieski
New Contributor
Hi,
I've been slowly learning python.  Currently I'm trying to export multiple mxds into a pdf.   I've been using print statement to see where I've been going wrong and I think I've narrowed it to down to the export to pdf statement.  My print statement only prints out the first mxd in the list.  Your help would greatly be appreciated.   Thanks!!!

# for each mxd in the list, access mxd, export to temporary pdf, and append to final pdf
try:
    for mxds in mxdList:
        arcpy.mapping.MapDocument(mxds)
        print "Accessed " + mxds
        tempFile = r"C\Temp\temp.pdf"
        arcpy.mapping.ExportToPDF(mxds, tempFile)
        print "Exported to temp PDF"
        pdfDocument.appendPages(tempFile)
        print "PDF created"

   
    pdfDocument.saveAndClose()
    del pdfDocument
    del mxds

except:
    print "Did not create pdf"
0 Kudos
4 Replies
LynnKanieski
New Contributor
Oops when I copied and pasted the code in the forum the code didn't indent correctly.  The indentation is not my issue, because the code runs successfully except for the fact it won't create the pdf. 



Thanks again,
Lynn
0 Kudos
JeffBarrette
Esri Regular Contributor
There where 3 issues:

1) the ExportToPDF function takes a MapDocument object as the first parameter, not a path.  Therefore I added the mapDoc variable that references the returned MapDocument object.
2) Your tempFile path was bad.  It was missing a colon.
3) I created a pdfDoc object outside of your loop and then appended each temporary PDF to it.CODE]

The following code
try:
  pdfDoc = arcpy.mapping.PDFDocumentCreate(r"C:\Temp\final.pdf")
  for mxds in mxdList:
    mapDoc = arcpy.mapping.MapDocument(mxds)
    print "Accessed " + mxds
    tempFile = r"C:\Temp\temp.pdf"
    arcpy.mapping.ExportToPDF(mapDoc, tempFile)
    print "Exported to temp PDF"
    del mapDoc
    pdfDoc.appendPages(tempFile)
  pdfDoc.saveAndClose()
except:
  print "Did not create pdf"
CODE]

Jeff
0 Kudos
LynnKanieski
New Contributor
Thanks Jeff.  I really appreciate it!!! I'll give it a try.   🙂
Lynn
0 Kudos
JeffBarrette
Esri Regular Contributor
Sorry, I just noticed my code did not come across indented.  I accidentally deleted one of the
 tags.  Here it is again:

try:
    pdfDoc = arcpy.mapping.PDFDocumentCreate(r"C:\Temp\final.pdf")
    for mxds in mxdList:
        mapDoc = arcpy.mapping.MapDocument(mxds)
        print "Accessed " + mxds 
        tempFile = r"C:\Temp\temp.pdf"
        arcpy.mapping.ExportToPDF(mapDoc, tempFile)
        print "Exported to temp PDF"
    del mapDoc
    pdfDoc.appendPages(tempFile)
    pdfDoc.saveAndClose() 
except:
    print "Did not create pdf"
0 Kudos