1

I have folder having multiple single mxd and mxd's having data driven pages. I am trying to get the pdf's of each of these MXD's.if mxd's has a data driven pages then all the pages from a same mxd document needs to save in a single pdf. What part of the below mentioned script will change so that it will consider the data driven pages.Right now this script is only exporting pdf from multiple single mxd document and not considering data driven pages. Need your suggestion for the same!!

>>> #Multiple single MXD's and Multiple datadriven pages in a single folder.

import arcpy, os

#Read input parameter from user.
#path = arcpy.GetParameterAsText(0)
path = r'Z:\Tier3\TrainControls\NonReportSpecific\2015_FINAL_PresentationMaps\MXD\REVISED\test'

#Write MXD names in folder to txt log file.
writeLog=open(path+"\FileListLog.txt","w")
for fileName in os.listdir(path):
    fullPath = os.path.join(path, fileName)
    if os.path.isfile(fullPath):
        basename, extension = os.path.splitext(fullPath)
        if extension == ".mxd":
            writeLog.write(fullPath+"\n")
            mxd = arcpy.mapping.MapDocument(fullPath)
            print fileName + "\n"
del mxd
print "Done"
writeLog.close()

#exportPath =arcpy.GetParameterAsText(1)
exportPath = r'Z:\Tier3\TrainControls\NonReportSpecific\2015_FINAL_PresentationMaps\MXD\REVISED\test'
MXDread=open(path+"\FileListLog.txt","r")
for line in MXDread:
    #Strip newline from line.
    line=line.rstrip('\n')
    if os.path.isfile(line):
        basename, extension = os.path.splitext(line)
        newName=basename.split('\\')[-1]
        if extension.lower() == ".mxd":
            print "Basename:" +newName
            mxd = arcpy.mapping.MapDocument(line)
            newPDF=exportPath+"\\"+newName+".pdf"
            print newPDF
            if "DDP enabled mxd":
                ddp = mxd.dataDrivenPages
                ddp.exportToPDF(tmpPdf,"ALL")
                finalPdf.appendPages(tmpPdf)
            else:
              arcpy.mapping.ExportToPDF(mxd,newPDF)
            print line + "Export Done"
MXDread.close()
item=path+"\FileListLog.txt"
os.remove(item)
del mxd
BergenCountyLine_KeyMap.mxd

BergenCountyLine_SurveyAssets.mxd

Done
Basename:BergenCountyLine_KeyMap
Z:\Tier3\TrainControls\NonReportSpecific\2015_FINAL_PresentationMaps\MXD\REVISED\test\BergenCountyLine_KeyMap.pdf
Runtime error 
Traceback (most recent call last):
  File "<string>", line 38, in <module>
  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\arcobjects\mixins.py", line 612, in dataDrivenPages
    return convertArcObjectToPythonObject(self._arc_object.pageLayout.dataDrivenPages)
AttributeError: Data Driven Pages is not enabled on this map document.
1
  • As it stands there is more than a code snippet included here which could lead to it being put on hold. I think you should try and isolate out just that part which is giving you the error into a code snippet and focus your question on it.
    – PolyGeo
    Commented Nov 3, 2015 at 0:07

1 Answer 1

1

You will need to replace this line:

arcpy.mapping.ExportToPDF(mxd,newPDF)

with an if/else that uses this pseudo code:

if "DDP enabled mxd":
    "Export PDF of all pages"
else:
    arcpy.mapping.ExportToPDF(mxd,newPDF)

To do "Export PDF of all pages" I recommend reviewing Combining Data Driven Pages with Python and arcpy.mapping where these lines of code will play a part:

ddp = mxd.dataDrivenPages
ddp.exportToPDF(tmpPdf, “ALL”)
finalPdf.appendPages(tmpPdf)
8
  • Runtime error Traceback (most recent call last): File "<string>", line 38, in <module> File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\arcobjects\mixins.py", line 612, in dataDrivenPages return convertArcObjectToPythonObject(self._arc_object.pageLayout.dataDrivenPages) AttributeError: Data Driven Pages is not enabled on this map document.
    – accedesoft
    Commented Nov 2, 2015 at 23:39
  • This is the error I m getting after replacing your code
    – accedesoft
    Commented Nov 2, 2015 at 23:40
  • Please make this an edit to your question rather than a comment here, but be sure to show us exactly the code snippet that you are running to produce the error.
    – PolyGeo
    Commented Nov 2, 2015 at 23:42
  • Code updated in the question!!
    – accedesoft
    Commented Nov 2, 2015 at 23:59
  • 1
    @accedesoft I think I see what's causing your error. You put in the pseudo code PolyGeo gave you as if it were actual, working code. Really it's just designed to show what your code needs to do. You need to use something like arcpy.describe (resources.arcgis.com/EN/HELP/MAIN/10.1/index.html#//…) on each mxd in the folder (resources.arcgis.com/en/help/main/10.1/index.html#//…) to see if it is DDP enabled, then if it is, you can run the code.
    – CSB
    Commented Nov 23, 2015 at 23:40

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.