jack.r.jenkins

Creating PDF mapbook document that will contain map for each county in counties feature class using ArcPy?

Discussion created by jack.r.jenkins on Nov 4, 2018

Through a plethora of trials and research, I finally was able to get a script to work.

My original question encompassed me needing to take an .mxd and create a PDF mapbook document that will contain a map for each county in the counties FC. Then, I needed to change the title of each map to the name of the county being shown. Lastly, I need to scale the map a bit so it shows an area just a bit larger than the actual extent.

The following script works great. I hope this will help others in the future;

   
import arcpy, osfrom 
arcpy
.mapping import *

countiesPath
= "C:\\Data\\COUNTIES.shp"
pdfPath = "C:\\Data\\COCounties.pdf"
mxdPath = "C:\\Data\\Counties.mxd"
mxd = MapDocument(mxdPath)

dataFrame
= (mxdPath, "COUNTIES")[0]
listLayers = ListLayers(mxd)

finalPDF
= PDFDocumentCreate(pdfPath)

df
= ListDataFrames(mxd, "Layers")[0]

for layer in listLayers:
    if layer.name == "COUNTIES" :
        with arcpy.da.SearchCursor(layer, "COUNTY") as cursor:
            for row in cursor:
                countyName = row[0]
                whereClause = "COUNTY='"+ countyName + "'"
                arcpy.SelectLayerByAttribute_management(layer, "NEW_SELECTION", whereClause)
                df.zoomToSelectedFeatures()
                arcpy.SelectLayerByAttribute_management(layer,"CLEAR_SELECTION")
                df.scale *= 1.5
                titleItem = ListLayoutElements(mxd, "TEXT_ELEMENT", "title")[0]
                titleItem.text = "Map of: " + countyName + " COUNTY"
                countyPDF=r"C:\\Data"+countyName+".pdf"
                ExportToPDF(mxd,countyPDF,"PAGE_LAYOUT",resolution=600,df_export_width=1100,df_export_height =800)
                finalPDF.appendPages(countyPDF)
                os.remove(countyPDF)

finalPDF
.saveAndClose()
del finalPDF
del mxd

Outcomes