# Import ArcPy import arcpy # Set up variables # Location of pole map .mxd document mxdDoc = r"G:\GEOSPATIAL\Publishing\Pole\Pole_QtrSec.mxd" # Create the MapDocument object mxd = arcpy.mapping.MapDocument(mxdDoc) layers = arcpy.mapping.ListLayers (mxd, "SURVEY_GRID_BNDRY") # Overwrite an existing file arcpy.env.overwriteOutput = True # Output directory for the pole maps outDir = r"G:\GEOSPATIAL\Publishing\Pole" # Set the workspace arcpy.env.workspace = outDir twpList = ['\"TOWNSHIP_C\" = \'D5\' AND \"RANGE_CD\" = \'7\'',\ '\"TOWNSHIP_C\" = \'D6\' AND \"RANGE_CD\" = \'7\''] for twpName in twpList: layers[0].definitionQuery = twpName print layers[0].definitionQuery # The final mapbook name taken from the list finalPDFfn = outDir + "\\" + twpName [16:18] + twpName [38:39] + "Pole.pdf" # Create the final PDF -- which is just an empty shell right now finalPDF = arcpy.mapping.PDFDocumentCreate(finalPDFfn) # A temporary pdf file for processing tmpPDF = outDir + "\\PoleMapPages.pdf" # Let the user know what is happening! print "Exporting " + twpName [16:18] + twpName [38:39] # Export the data driven pages in the mxd to a temporary PDF print "Exporting map pages to the temporary PDF" ddp = mxd.dataDrivenPages.exportToPDF(tmpPDF) # Append the temporary pdf to the final pdf # Cover, map pages, back cover print "Appending Map Pages" finalPDF.appendPages (r"G:\GEOSPATIAL\Publishing\Pole\PoleCovers\Covers_" + twpName [16:18] + twpName [38:39] + ".pdf") finalPDF.appendPages(tmpPDF) finalPDF.appendPages(r"G:\GEOSPATIAL\Publishing\TwpGrid_Color8x11.pdf") # Set properties for Adobe Reader and save PDF. finalPDF.updateDocProperties(pdf_open_view = "USE_THUMBS", pdf_layout = "SINGLE_PAGE") finalPDF.saveAndClose() # Clean up print "Cleaning up" # Delete the temporary PDF using the ArcPy function if arcpy.Exists(tmpPDF): arcpy.Delete_management(tmpPDF) # Delete objects del mxd, tmpPDF, ddp
Solved! Go to Solution.
import os, arcpy arcpy.env.overwriteOutput = True mxdDoc = r"F:\qryDefTest\qryTestDoc.mxd" mxd = arcpy.mapping.MapDocument(mxdDoc) # assuming the 1st data frame df = arcpy.mapping.ListDataFrames(mxd)[0] # queries - SDE workspace twpList = ['AK in (' + str(1240290) + ', ' + str(1240311) + ')',\ 'AK in (' + str(1240281) + ', ' + str(1240338) + ')'] # layer (the 1st and only layer in the map) layer = arcpy.mapping.ListLayers(mxd, '', df)[0] i = 1 for twpName in twpList: layer.definitionQuery = twpName mxd.saveACopy(os.path.splitext(mxdDoc)[0] + str(i) + os.path.splitext(mxdDoc)[1]) i += 1 del mxd
import os, arcpy arcpy.env.overwriteOutput = True outDir = r"G:\GEOSPATIAL\Publishing\Pole" arcpy.env.workspace = outDir mxdDoc = r"G:\GEOSPATIAL\Publishing\Pole\Pole_QtrSec.mxd" mxd = arcpy.mapping.MapDocument(mxdDoc) # assuming the 1st data frame df = arcpy.mapping.ListDataFrames(mxd)[0] # queries - should make these sensitive to the workspace type? twpList = ['\"TOWNSHIP_C\" = \'D5\' AND \"RANGE_CD\" = \'7\'',\ '\"TOWNSHIP_C\" = \'D6\' AND \"RANGE_CD\" = \'7\''] # Your layer layer = arcpy.mapping.ListLayers (mxd, "SURVEY_GRID_BNDRY", df)[0] i = 1 for twpName in twpList: layer.definitionQuery = twpName # oops, should be 'saveACopy' not as I originally posted, 'savACopy' mxd.saveACopy(os.path.splitext(mxdDoc)[0] + str(i) + os.path.splitext(mxdDoc)[1]) i += 1
twpList = [""" "TOWNSHIP_C" = 'D5' AND "RANGE_CD" = '7' """,\ """ "TOWNSHIP_C" = 'D6' AND "RANGE_CD" = '7' """]
twpName [16:18]. It would also simplify everything if you took the township and range text out of the list. e.g.
twpList = [['D5','7'],['D6','7']] for twp in twpList: twpdefQuery = """ "TOWNSHIP_C" = '""" + twp[0] + """' AND "RANGE_CD" = '""" + twp[1] + "'" layers[0].definitionQuery = twpdefQuery ##And from then on instead of using twpName [16:18] you could use twp[0] or twp[1]
import os, arcpy arcpy.env.overwriteOutput = True mxdDoc = r"F:\qryDefTest\qryTestDoc.mxd" mxd = arcpy.mapping.MapDocument(mxdDoc) # assuming the 1st data frame df = arcpy.mapping.ListDataFrames(mxd)[0] # queries - SDE workspace twpList = ['AK in (' + str(1240290) + ', ' + str(1240311) + ')',\ 'AK in (' + str(1240281) + ', ' + str(1240338) + ')'] # layer (the 1st and only layer in the map) layer = arcpy.mapping.ListLayers(mxd, '', df)[0] i = 1 for twpName in twpList: layer.definitionQuery = twpName mxd.saveACopy(os.path.splitext(mxdDoc)[0] + str(i) + os.path.splitext(mxdDoc)[1]) i += 1 del mxd
layer.definitionQuery = twpName mxd.dataDrivenPages.refresh()