# Import arcpy module import arcpy # Read the parameter values: # 1: Input MXD # 2: Output folder # 3. Output filename mxdPath = arcpy.GetParameterAsText(0) outDir = arcpy.GetParameterAsText(1) outFile = arcpy.GetParameterAsText(2) arcpy.AddMessage("Processing: "+mxdPath) mxd = arcpy.mapping.MapDocument(mxdPath) # Get data frames from mxd dfs = arcpy.mapping.ListDataFrames(mxd) # Assume first data frame is the map (HARDCODED) and get the x page offset x = dfs[0].elementPositionX #Create final output PDF file finalPdf = arcpy.mapping.PDFDocumentCreate(outDir + "\\" + outFile + ".pdf") # Cycle through DDP in mxd pageCount = mxd.dataDrivenPages.pageCount for pageNum in range(1, pageCount + 1): mxd.dataDrivenPages.currentPageID = pageNum # Get scale from DDP index layer (HARDCODED - assumes "Scale" attribute exists) scale = mxd.dataDrivenPages.pageRow.Scale # Move Grids & Graticules in position for each scale (HARDCODED - data frames must have certain names) for df in dfs: if (df.name == "Graticule_"+str(scale)) or (df.name == "Grid_"+str(scale)): df.elementPositionX = x arcpy.AddMessage("Exporting page {0} of {1}".format(str(pageNum), str(pageCount))) tmpPdf = outDir + "\\" + "temp.pdf" arcpy.mapping.ExportToPDF(mxd, tmpPdf) finalPdf.appendPages(tmpPdf) # Move Grids & Graticules back to original position (HARDCODED -40cm assumed) for df in dfs: if (df.name == "Graticule_"+str(scale)) or (df.name == "Grid_"+str(scale)): df.elementPositionX = -40 del tmpPdf arcpy.AddMessage("Saving: " + outFile + ".pdf") finalPdf.saveAndClose()