4

I've written a script that makes use of a data driven pages mxd, but I'm having issues with locks. The script iterates through a series of feature classes and uses them as the base layer for the DDP. I've found that once an mxd object is created, saved, and the code arcpy.mapping.ExportToPDF is used, the mxd can't be saved again. The code mxd.save () fails on its second iteration. I've run the script commenting out arcpy.mapping.ExportToPDF and the script ran fine. Here's my code:

oldMapDoc = mapDoc

#iterate feature classes
for i, fc in enumerate (sorted (fcs)):
    print fc
    mxd = arcpy.mapping.MapDocument (mapDoc)
    df = arcpy.mapping.ListDataFrames(mxd)[0]
    ddpLyr = [l for l in arcpy.mapping.ListLayers (mxd) if l.name == "Symbology"] [0]
    ddpLyr.replaceDataSource (platsGdb, "FILEGDB_WORKSPACE", fc)
    mapFolder = os.path.join (distFolder, fc.title ().replace ("_", " "))
    mxd.save ()
    pageCount =  mxd.dataDrivenPages.pageCount
    print pageCount
    for pageNum in range(1, pageCount + 1):
        mxd.dataDrivenPages.currentPageID = pageNum
        index = mxd.dataDrivenPages.pageNameField.name
        plat = mxd.dataDrivenPages.pageRow.getValue (index).replace ("\\", "_")
        outPdfName = fc + "_" + plat + ".pdf"
        pageId = mxd.dataDrivenPages.currentPageID
        print "Exporting page {} of {}: {}".format(pageId,
                                                   pageCount,
                                                   outPdfName)
        outPdf = os.path.join (mapFolder, outPdfName)
        if arcpy.Exists (outPdf):
            print "already made"
            continue
        arcpy.mapping.ExportToPDF (mxd, outPdf)
        #break
    del mxd, df, ddpLyr, index, plat, outPdfName, pageId, mapFolder, pageCount
    mapDoc = oldMapDoc.replace (".mxd", "") + "_" + str (i) + ".mxd"
    print "copying"
    copyfile (oldMapDoc, mapDoc)

I've tried the variation of creating the mxd object outside of the loop. This produces a different error - the script runs but the DDP layer never actually updates.

As you can see, I've created a workaround where I copy the mxd file with each iteration. It works, but it's pretty wonky. Has anyone dealt with this issue before and found a solution besides something hokey like I have?

  • 1
    Why do you need to save the same mxd on each iteration? Does export to PDF require a save first? – Midavalo Feb 5 '17 at 20:45
  • What version of ArcGIS are you using? 10.3 has a bug with DDP which is resolved in 10.4. This may be part of your problem. – jbalk Feb 6 '17 at 1:27
  • 1
    under your line mxd = arcpy.mapping.MapDocument (mapDoc) add the line ddp = mxd.dataDrivenPages then export to pdf using ddp.exportToPDF("name_of_your_pdf", "ALL") – Clubdebambos Feb 6 '17 at 11:34
  • If I don't save the mxd the line of code pageCount = mxd.dataDrivenPages.pageCount seems to fail. The count remains the same as it iterates through each feature class, until it hits a feature class with less than the given count of pages. Then I get an index out of range sort of error when it tries to find a page that doesn't exist. I'm using 10.1. – Emil Brundage Feb 9 '17 at 20:34
1

I frequently write applications that automate export to PDF from an index layer by simply using a SearchCursor to "visit" each feature extent, do any desired manipulations, and then export to PDF without saving the MXD.

I find this much easier and more flexible to do than trying to merge Data Driven Pages functions with ArcPy code.

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.