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.