I have a project that has me create a package of about a dozen maps for a client and I do this multiple times per week. I am trying to figure out if there is a way to automate this with python. I am hoping to create a script that takes a shapefile input from the user and inserts that shapefile into multiple mxd templates (FEMA, USGS, Soils, Aerials, etc).

I am very experienced with GIS but somewhat new to python.


2 Answers 2

# PubishMapBook.py
# Purpose: Create map book pdf, output Data driven pages series from .mxd, 
# assemble map book and save
# Import modules
import arcpy
import os
# set the current workspace to your folder
arcpy.env.workspace = r"C:\your_data"
# set up variables for output path and PDF file name
outDir = r"C:\your_data"
finalpdf_filename = outDir + r"FinalMapBook.pdf"
#Remove existing multi-page PDF if it exists
if os.path.exists(outDir + r"\MapPages.pdf"):
    os.remove(outDir + r"\MapPages.pdf")
# Check whether the final map book PDF exists,if it does, delete it.
if os.path.exists(finalpdf_filename):
#Create map book PDF
finalPDF = arcpy.mapping.PDFDocumentCreate(finalpdf_filename)
# Start appending pages.Title page first
finalPDF.appendPages(outDir + r"\TitlePage.pdf")
# Create MapDocument object pointing to specified mxd
mxd = arcpy.mapping.MapDocument(outDir + r"TitlePage.mxd")
# Export data driven page MXD to multi-page PDF
ddp = mxd.dataDrivenPages
Print "Exporting map pages to PDF"
ddp.exportToPDF(outDir + r"\MapPages.pdf")
#Append multi-page to finaPDF
finalPDF.appendPages(outDir +r"\MapPages.pdf")
# Append contact page to PDF
finalPDF.appendPages(outDir + r"TitlePageContactPage.pdf")
# Set up properties for Adobe Reader and save PDF
finalPDF.updateDocProperties(pdf_open_view = "USE_THUMBS",pdf_layout = "SINGLE_PAGE")
# Done. Clean up and let user know the process has finished.
del mxd, finalPDF
print  "Creation of map book complete!"

I don`t know if the above works or not so it is just for your reference.


For automating any task using ArcPy the procedure that I would use is as follows:

  1. Follow the steps that you currently use once
  2. As you complete each step that uses a Geoprocessing tool use the Geoprocessing | Results window to Copy As Python Snippet that line of code into your Python script
  3. If there are steps that you do through the GUI rather than via a tool dialog then look for their Geoprocessing tool equivalent
  4. Test to ensure that your script does repeat your manual process successfully
  5. Add iteration to your script to repeat it multiple times
  6. Add code to accept inputs like your shapefile

Not the answer you're looking for? Browse other questions tagged or ask your own question.