import arcpy, sys, os arcpy.OverWriteOutput = 1 mxd = arcpy.mapping.MapDocument("CURRENT") print "Enter save as pdf location:" pdfDir = arcpy.GetParameterAsText(0) outputFolder = pdfDir + r"\PDFs" df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] maskLayer = arcpy.mapping.ListLayers(mxd, "state_bnd110", df)[0] maskField = "STATE_NAME" for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1): mxd.dataDrivenPages.currentPageID = pageNum pageName = mxd.dataDrivenPages.pageRow.STATE_NAME for lyr in arcpy.mapping.ListLayers(mxd): if lyr.name == maskLayer: lyr.definitionQuery = '"STATE_NAME" <> pageName' arcpy.mapping.ExportToPDF(mxd, os.path.dirname(outputFolder)+ os.sep + pageName + ".pdf") lyr.definitionQuery = "" arcpy.RefreshActiveView() del mxd arcpy.GetMessages()
'"STATE_NAME" <> %s' % pageName
arcpy.env.overwriteOutput = True
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1): mxd.dataDrivenPages.currentPageID = pageNum pageName = mxd.dataDrivenPages.pageRow.STATE_NAME for lyr in arcpy.mapping.ListLayers(mxd): if lyr.name == maskLayer: lyr.definitionQuery = '"STATE_NAME" <> %s' % pageName arcpy.RefreshActiveView() arcpy.mapping.ExportToPDF(mxd, os.path.dirname(outputFolder)+ os.sep + pageName + ".pdf") lyr.definitionQuery = ""
#DDP: Set Definition Query on a secondary index #Variables that need to be set by user: #mxdpath = r'C:\Users\Test.mxd' # string #outputfolder = 'PDFFolder' # string #namefield = 'THE FIELD CONTAINING THE NAME OF THE RESULTING PDF FILE' #string #queryfield = 'THE FIELD CONTAINING THE VALUE FOR THE DESIRED QUERY' #string #layers = ['LIST','OF','LAYERS'] # list ('Layer1_UpdateQuery', 'Layer2_UpdateQuery', 'etc') #Import Modules import arcpy, os # Set Variables mxdpath = r'D:\arcgis\Samples\DDP_Index2.mxd' # string outputfolder = 'D:\PDF' # string namefield = 'FIPS' # string queryfield = 'STATE_NAME' # string layers = ['CountySel_FIPS'] # list mxd = arcpy.mapping.MapDocument(mxdpath) # Start Loop through Data Driven Pages for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1): mxd.dataDrivenPages.currentPageID = pageNum # set the current page pageName = mxd.dataDrivenPages.pageRow.getValue(namefield) # get the pageName pageName += '.pdf' # add '.pdf' extension for file name pdf = os.path.join(outputfolder, pageName) # add pageName to output folder to get full output path # Start Loop through all layers of mxd file for lyr in arcpy.mapping.ListLayers(mxd): # for every layer in mxd's Layer List if lyr.name in layers: # if layer name in user input list above #value = mxd.DataDrivenPages.pageRow.getValue(queryfield) # get value of query field value = mxd.dataDrivenPages.pageRow.getValue(queryfield) print value if lyr.definitionQuery == True: # if a definitionQuery Exsts... query = lyr.definitionQuery + "AND" + queryfield + "=" + "\"" + value + "\"" # Add new to old else: query = queryfield + "=" + "\"" + value + "\"" # else create a new query lyr.definitionQuery = query # set the layers defintionQuery else: print 'Next' # else print next, this isn't necassary, but its a good place holder print pageName arcpy.mapping.ExportToPDF(mxd, pdf) # export the current Page to pdf del mxd, lyr, pageName, pageNum, query, value
Thanks for sharing this. It is just what I was looking for.
Thank you very much for the Clean Code and comments as someone new to Python and Arcmap this really helps me understand whats going on. Thanks again I appreciate it.
I agree that is incredibly helpful for folks to post their final code. Thanks for that. I do have a question about line-38. Dont you want "lyr.definitionQuery = query # set the layers defintionQuery " to be outside of the 'else'? You want to set the def query to the variable 'query' either way, and NOT just in the case of the else, right? I cant say for sure, because I am elementary with my python.
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.