2

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, os
from 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
  • Instead of having ExportToPDF(mxd,finalPDF,ect...) you should have ExportToPDF(mxd,countyPDF,etc...) – Raskar Kapak Nov 8 '18 at 3:14
2

According your script, you are just exporting 2 PDFs: one is the layout and one is just the map. You won't get what you want this way.
I don't think you need to use a python script for this purpose.
I assume you have set up the data driven pages.
Have a look at this page. You will see that you can set up the extent. In the data driven properties, go to Extent, then Best Fit and adjust the size (for example 200%) and click OK (play with the percentage to find out which one is the best). For the title , have a look at this help.
You can create a dynamic title as follow: Insert title and write:

<BOL>Map of:</BOL> <dyn type="page" property="attribute" field="County_Name" domainlookup="true"/>

You just need to match the field with the one of your feature class.
It will display something like this: Map of: CountyName...
Finally, you can export your maps like this:
- in the toolbar -> File
- Export Map
- Select PDF
- In the Options, go to Pages and choose All
- Export page As (you can chooose to merge all the pages in a single one or you can create a single document per page).
- The end
Hope it helps.
cheers.

  • I need help getting this done via Arcpy/Python. I understand the process via arcmap. Thank you, – user129835 Nov 5 '18 at 15:45
0

If you copy and paste this one it will work.

import arcpy
import os
from arcpy.mapping import *


pdf_folder="C:\\Data"
pdfPath = "C:\\Data\\COCounties.pdf"
mxdPath = "C:\\Data\\Counties.mxd"

mxd = MapDocument(mxdPath)

dataFrame = (mxdPath, "Layers")[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
                for elm in ListLayoutElements(mxd, "TEXT_ELEMENT"):
                    if "Map of:" in elm.text:
                        elm.text = "Map of: "+ countyName
                countyPDF=os.path.join(pdf_folder,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

I think you were confused with the shapefile. You don't need it in the script because you access to it through the mxd. So basically you need to define the final pdf location(pdfPath ), a pdf template to export temporary pdf (pdf_folder). So first you call your mxd. Then you need to define your dataframe (that will be useful when you will work with the layout: text element). Then you need to retrieve the COUNTIES layer with your mxd, so retrieving a list of all the mxd, you can parse this list and see if there is a match with the COUNTIES. Once you have a match you can process it. Retrieve the name of each county through a Search Cursor. With the name, you can proceed to a selection and zoom to the attribute. Then you have to clear the selection as you don't want to see the selection outlined in the pdf you are going to export. You can zoom out as well (df.scale *=1.5) the you loop through the element of the layout to find out the title you want to edit. ListLayoutElements(mxd, "TEXT_ELEMENT") means you want to search through all the text element of the layout. Once the text has been update, you create an export path for a temporary pdf (countyPDF) and add it to the final pdf. As it is a temporary file, you can remove it. Finally you can save the pdf and ditch some variable.

  • Raskar. I can't thank you enough for all your time and help with this. I've adjusted my script above with a few changes in regards to getting the title of each page to work. Everything is running great now. Thank you again and all the best. – user129835 Nov 8 '18 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy