0

I am trying to export data driven pages using ArcPy. I can manually export the data driven pages into a PDF so it seems like it is set up correctly. I followed the script to export DDP on the Esri site, but I'm not having any luck. I'm sure it is something simple I am overlooking, but I have been playing around with this for hours and could use another set of eyes. Sometimes my script will run but nothing is produced. Most of the time I get this error:

File "c:\program files (x86)\arcgis\desktop10.8\arcpy\arcpy\utils.py", line 182, in fn_ return fn(*args, **kw) File "c:\program files (x86)\arcgis\desktop10.8\arcpy\arcpy\utils.py", line 182, in fn_ return fn(*args, **kw) File "c:\program files (x86)\arcgis\desktop10.8\arcpy\arcpy_mapping.py", line 450, in exportToPDF return convertArcObjectToPythonObject(self._arc_object.exportToPDF(*gp_fixargs((out_pdf, page_range_type, page_range_string, multiple_files, resolution, image_quality, colorspace, compress_vectors, image_compression, picture_symbol, convert_markers, embed_fonts, layers_attributes, georef_info, jpeg_compression_quality, show_selection_symbology), True))) AttributeError: PageLayoutObject: Error in exporting pages

I had some code working at one point, but as soon as I switched to a different way of making the grid, it stopped working even though the grid is saved under the same name in the same location.

I thought maybe it was the change in grid, but I went back and reran that code and it doesn't work anymore either. The only thing I can think of is I made a change in the .mxd template itself that is causing an issue. I don't think I altered the initial code at all before I saved it as a working version.

Any thoughts on what I could try?

import arcpy
import os

output = r"C:\Users\name\Desktop\junk\testing\test6"

mxdpath = r"C:\Users\name\Desktop\junk\testing\test6\testing.mxd"
tempmap = arcpy.mapping.MapDocument(mxdpath)
tempddp = tempmap.dataDrivenPages
temp_filename = os.path.join(output,"grid.pdf")

if os.path.exists(temp_filename):
    os.remove(temp_filename)
tempddp.exportToPDF(temp_filename,"ALL")

Using information from this website https://support.esri.com/en/technical-article/000022852 I changed the code to the following:

Output_Location = arcpy.GetParameterAsText(0)
map_copy2 = os.path.join(Output_Location, "grids.mxd")
mxd = arcpy.mapping.MapDocument(map_copy2)
for pageNum in range(0, mxd.dataDrivenPages.pageCount + 1):
  mxd.dataDrivenPages.currentPageID = pageNum
  OutF = os.path.join(r"C:\Users\coburm1\Desktop\junk\testing\test20" ,str(pageNum) + ".pdf" )
  arcpy.mapping.ExportToPDF(mxd, OutF)

The second script will run without errors, but nothing is produced. If I run the second script in the python window in Arcmap, it will produce pdfs, however, each grid is an individual pdf so if I have an index with 250 squares, I get 250 pdfs. I need to figure out why it is not running as a stand alone script, and how to get it to produce one pdf with multiple pages instead of multiple pdfs.

1 Answer 1

0

This is a standalone script that I have used to generate Data Driven Pages in Arcmap from a template MXD.

Full discplosure here, I have taken this out of a larger script I have setup, so havent tested it properly - the script I have taken it from is a script called by FME and runs as a stand-alone script (outside Arcmap).

import arcpy
import os

arcpy.env.overwriteOutput = True 
mxd = arcpy.mapping.MapDocument(r'C:\Path_to_Mxd.mxd')
#df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

outp = r"Path to output folder\\"
temp = r"Path to temp folder\\"



for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
    print "Page Number: " + str(pageNum)
    print "MXD Page ID " + str(mxd.dataDrivenPages.currentPageID)
    arcpy.mapping.MapDocument.activeView = 'PAGE_LAYOUT'
    mxd.dataDrivenPages.currentPageID = pageNum
    print "Name: " + str(mxd.dataDrivenPages.pageRow.Title)
    name = mxd.dataDrivenPages.pageRow.Title +'_' + str(mxd.dataDrivenPages.pageRow._count)
    print ("End Name: " + name)
    tmpPdf = temp + name + "_Temp.pdf"
    print ("Temp PDF: " + tmpPdf)
    print "Exporting page {0} of {1}".format(str(mxd.dataDrivenPages.currentPageID), mxd.dataDrivenPages.pageCount)
    arcpy.mapping.ExportToPDF(mxd, tmpPdf,resolution =150)
    print "Exported page {0} of {1}".format(str(mxd.dataDrivenPages.currentPageID), mxd.dataDrivenPages.pageCount)

#The following section combines the DDP output PDFs into a single PDF...if you want
          
files = []

for file in os.listdir(temp):
    files.append(file)

    for tile in mylist:
       finalPDF = arcpy.mapping.PDFDocumentCreate(outp + tile + '_Fires.pdf')
                
       for file in files:
           print file
           f = file.split('_')
           if f[0] == tile:
               finalPDF.appendPages(temp + file)
    
    del mxd, finalPDF, temp



       
1
  • Thank you. I tried this but have the same issue. It runs, but nothing is produced. I'm wondering if it has to do with the data driven pages not refreshing when the grid is changed. When I open the mxd , the refresh button won't even work. I have to open the data driven pages setup window and hit ok to get things to reset. Any thoughts on how I can get the pages to refresh. I've tried both of these arcpy.RefreshActiveView() mxd.dataDrivenPages.refresh()
    – hosta
    Commented Jul 13, 2022 at 14:32

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.