0

I've been trying to export map data (mxd) and combine into a PDF. I'm successful in creating the two PDFs, but can't combine them.

How do I correct this error?

finalPDFDoc isn't defined

I'm very new to Python.

import arcpy
from arcpy import env
from arcpy.mapping import *

env.overwriteOutput = True
env.workspace = "C:\\ClassData\\Python_4080\\wk_8\\Lesson8_Data"

output_folder = "C:\\ClassData\\Python_4080\\wk_8\\Lesson8_Data\\Results"

#OPEN and work with map documents
mapDoc = MapDocument("C:\\ClassData\\Python_4080\\wk_8\\Lesson8_Data\\Lesson8.mxd")

#TODO define data frame

dataFrames = ListDataFrames(mapDoc)
dataFrame = dataFrames[0]       #gets first data frame to export

#output paths of exported maps. 1st is in data view and 2nd in layout view.
pdfPathdf = "C:/ClassData/Python_4080/wk_8/Lesson8_Data/Results/lesson8df"
pdfPathpl = "C:/ClassData/Python_4080/wk_8/Lesson8_Data/Results/lesson8pl"

#Export map in dataview, with data frame as third parameter
ExportToPDF(mapDoc, pdfPathdf, dataFrame, df_export_width = 1200, df_export_height = 800)

#Export map in layoutview, with constant as third parameter
ExportToPDF(mapDoc, pdfPathpl, "PAGE_LAYOUT", df_export_width = 1200, df_export_height = 800)
print "PDF has been generated at " + pdfPathdf


####### This doesn't run ###############

################################################################################
####################################################################################
#Add components to new PDFs. Merging the two views into one PDF

finalPDFDoc.appendPages(pdfPathdf)  
finalPDFDoc.appendPages(pdfPathpl)
finalPDFDoc.saveACopy("C:\\ClassData\\Python_4080\\wk_8\\Lesson8_Data\\Results\\Final_PDF")

finalPDFDoc.close()
del finalPDFDoc
1
  • 1
    I think you need to create finalPDFDoc before you can append to it.
    – Mike308
    Commented Jul 28, 2018 at 22:56

1 Answer 1

2

You need to create your finalPDFDoc variable before you can append to it or delete it.

finalPDFDoc = arcpy.mapping.PDFDocumentCreate(r"C:\ClassData\Python_4080\wk_8\Lesson8_Data\Results\Final_PDF")

finalPDFDoc.appendPages(pdfPathdf)  
finalPDFDoc.appendPages(pdfPathpl)

finalPDFDoc.saveAndClose()

For more info and documentation see ArcGIS Desktop Help - PDFDocument

5
  • I tried this and keep getting 'invalid path'. I can't see it.pdfPathdf = "C:/ClassData/Python_4080/wk_8/lesson8df.pdf" pdfPathpl = "C:/ClassData/Python_4080/wk_8/lesson8pl.pdf" finalPDFDoc = arcpy.mapping.PDFDocumentCreate(r"C:\\ClassData\\Python_4080\\wk_8\\Final_PDF.pdf") finalPDFDoc.appendPages(pdfPathdf) finalPDFDoc.appendPages(pdfPathpl) finalPDFDoc.saveAndClose()
    – rocksRule
    Commented Jul 29, 2018 at 1:22
  • @Ann Try removing the r in front of the path from the finalPDFDoc = line, or changing the double slash to single in that line
    – Midavalo
    Commented Jul 29, 2018 at 3:41
  • Still getting invalid path in the for the same line finalPDFDoc. I've tried both suggestions. It has to be in the pdfPathdf line, I think. dataFrames = ListDataFrames(mapDoc) dataFrame = dataFrames[0] pdfPathdf = "C:/ClassData/Python_4080/wk_8/Lesson8_Data/lesson8df.pdf" pdfPathpl = "C:/ClassData/Python_4080/wk_8/Lesson8_Data/lesson8pl.pdf" finalPDFDoc = arcpy.mapping.PDFDocumentCreate("C:/ClassData/Python_4080/wk_8/Final_PDF.pdf") finalPDFDoc.appendPages(pdfPathdf) finalPDFDoc.appendPages(pdfPathpl) finalPDFDoc.saveAndClose()
    – rocksRule
    Commented Jul 29, 2018 at 19:01
  • @Ann please edit your question to include this new info
    – Midavalo
    Commented Jul 29, 2018 at 19:20
  • @Ann what is the full "invalid path" error message?
    – Midavalo
    Commented Jul 29, 2018 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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