Skip To Content

Create a map book with Python

ArcGIS Pro provides a powerful set of functions and methods in the arcpy.mp Python submodule. It was designed primarily to manipulate the contents of existing projects (.aprx) and layer files (.lyr or .lyrx). It also provides functions to automate exporting and printing. Arcpy.mp can be used to automate map production and is required to build complete map books because it includes functions to create and manage PDF documents.

In this tutorial, you are going to start with a project that contains a map with bookmarks for each of the Great Lakes and a layout with elements, including a north arrow, scale bar, and dynamic text. You will start by creating a title page for the map book. After that is finished, you will use arcpy.mp to adjust the position of the layout elements on the map and export it to individual PDFs based on the bookmarks that have been defined. Finally, you will build the map book by creating a PDF and appending the other files to it.

To complete the tutorial, you will go through the following steps:

  • Find and open the project.
  • Create a title page.
  • Reposition the layout elements prior to export.
  • Export all pages to PDFs.
  • Build the map book.
  • View the final PDF.

Find and open the project

This tutorial uses a basemap, but the map and layout are stored in a package (.ppkx) file. This is a compressed file that contains GIS data and can be shared like any other file, through email, FTP site, or via ArcGIS Online or a portal. Additionally, you can use packages to archive data at a specific point in time. The Great Lakes map contains the bookmarks you will use to generate the individual maps of the lakes, while the layout has all the layout elements and other information related to the way you want the maps to appear in the map book.

The first step will be to find the project through a portal connection and open it in ArcGIS Pro.

  1. Start ArcGIS Pro.
  2. Click the Open another project link.
  3. Click Portal and click Browse.
  4. On the Open Project dialog box, click All Portal.
  5. Type Great Lakes PPKX in the Search text box and press Enter.
  6. Select the Great Lakes.ppkx file and click Select.

The Great Lakes map opens in ArcGIS Pro. You can also see the Great Lakes Maps layout, which already contains a map frame and some layout elements. You are now ready to create a title page and create a map book.

Create a title page for the map book

A more complete map book includes a title page and more, but for this exercise, we will create a simple title page. You can create a title page using ArcGIS Pro and export it as a PDF so it can be added to a map book using arcpy.mp. In this exercise, you will create an image using one of the bookmarks and save it as an image that can be added to the title page. After that, you will add a new layout that will be used to create the title page.

Create an image for the title page

Since the map book for the Great Lakes covers a wide geographic area, it would be nice to have an image of all the lakes on the title page. If you do not already have an image to put on the title page, you can create one in ArcGIS Pro. You can use the Imagery basemap to view the Great Lakes bookmark and capture an image of the map view using the Windows Snippet tool or another third-party image editing program.

  1. Click the Great Lakes map view.
  2. On the Map tab, in the Navigate group, click the Bookmarks drop-down arrow and choose the Great Lakes bookmark.
  3. On the Map tab, in the Layer group, click Basemap and choose Imagery.

    The Great Lakes bookmark is now displayed using imagery instead of the Topographic basemap.

  4. Press Alt + Print Screen or use a third-party image editing program to capture the map view with the lakes displayed.
  5. Save the image as GreatLakes.jpg.

Create the layout for the title page

Now that the image has been created, you can add a new layout to the map and create a title page for the map book. It will consist of text for the title and the image you created.

  1. On the Insert tab, in the Project group, click New Layout and choose Letter in the ANSI - Portrait group.
  2. Click the layout name on the Contents pane and type Title Page.
  3. On the Insert tab, in the Text group, click Text.
  4. Click the map layout to add a text box.
  5. On the Element pane, type The Great Lakes of North America in the Text text box and click Apply.

    To wrap the text, you need to press Enter after The Great Lakes.

  6. On the Format tab, in the Text Symbol group, type 48 in the Text Symbol Font Size box.
  7. Move the text box so it is in the top portion of the layout.
  8. On the Insert tab, in the Graphics group, click Picture.
  9. Browse to the GreatLakes.jpg file created previously and click Open.
  10. On the Insert tab, in the Text group, click Dynamic Text and choose Current Time in the System group.
  11. Move the text to the bottom of the page and change the size to 16 or greater.

    The final title page will resemble the following.

    Cover page for the map book
  12. Save the project.

Export the title page to a PDF

After the title page is set up, it can be exported as a PDF so it can later be appended to the final map book. The Layout class has a method that allows you to export a layout to a PDF file. You will use it to create a PDF called TitlePage in this part of the tutorial.

  1. On the Analysis tab, click Python Show Python window to open the Python pane.
  2. If necessary, dock the Python pane in the ArcGIS Pro window.
  3. Copy and paste the following lines of code into the Python pane to export the title page as a PDF.
    import arcpy, os
    aprx = arcpy.mp.ArcGISProject("CURRENT")
    
    # Update this path to match the location where the project is saved
    path = r'c:\path_to_project'
    
    lyt = aprx.listLayouts("Title Page")[0]
    lyt.exportToPDF(os.path.join(path, "TitlePage.pdf"))

You now have a title page that is ready to be appended to the map book.

Refine the layout before publishing

With arcpy.mp, you can move layout elements so they have better placement on the page layout. For example, suppose you'd like to move the scale bar so it appears toward the bottom of the page instead of below the map frame. Using the MapsurroundElement class, you can adjust the position and size of a layout element relative to the map frame.

In this part of the exercise, you are going to move the scale bar so it appears more toward the bottom of the page. You will also move the north arrow so it is aligned with the right side of the map frame and adjust the placement of the spatial reference text and the map title. After all the layout elements are moved, you will save the project to ensure the changes are saved.

  1. Click the Great Lakes Maps layout view.
  2. If necessary, on the Analysis tab, click Python Show Python window to open the Python pane.
  3. Adjust the position of the layout elements by copying and pasting the following lines of code into the Python pane.
    lyt = aprx.listLayouts("Great Lakes Maps")[0]
    
    # Reposition the scale bar
    scaleBar = lyt.listElements("MAPSURROUND_ELEMENT", "Scale Bar")[0]
    mf = scaleBar.mapFrame
    scaleBar.elementPositionX = mf.elementPositionX + 1.25
    scaleBar.elementPositionY = mf.elementPositionY - 3
    
    # Reposition the north arrow
    northArrow = lyt.listElements("MAPSURROUND_ELEMENT", "North Arrow")[0]
    mf = northArrow.mapFrame
    northArrow.elementPositionX = mf.elementPositionX + 6
    northArrow.elementPositionY = mf.elementPositionY - 0.9
    
    # Reposition the spatial reference text
    srText = lyt.listElements("TEXT_ELEMENT",'Spatial Reference Text')[0]
    srText.elementPositionX = northArrow.elementPositionX - 6
    srText.elementPositionY = northArrow.elementPositionY - 1.5
    
    # Align the title with the center of the map frame
    title = lyt.listElements("TEXT_ELEMENT","Title")[0]
    mf = lyt.listElements('MAPFRAME_ELEMENT',"Great Lakes")[0]
    title.elementPositionX = mf.elementPositionX + (mf.elementWidth / 2)
    
    aprx.save()
  4. Leave the Python pane open if you are going to complete the next part of the tutorial.

Export the map to individual PDF files

Now that the layout has been set up, you are ready to export the individual maps to PDF files. The Layout class in the arcpy.mp module has a method that allows you to export a layout to a PDF file. This method has several parameters that allow you to customize the resolution, image quality, and other properties of the output file. For this tutorial, you will define the output location for the files and embed the fonts.

You are also going to update the Title text element you added earlier so that it reflects the bookmark name. This allows the map title to be dynamic based on what is currently shown in the map frame.

  1. If necessary, on the Analysis tab, click Python Show Python window to open the Python pane.
  2. Copy and paste the following lines of code in the Python pane and press Enter.
    title = lyt.listElements("TEXT_ELEMENT", "Title")[0] 
    bkmks = mf.map.listBookmarks()
    for bkmk in bkmks:
      mf.zoomToBookmark(bkmk)
      title.text = bkmk.name
      lyt.exportToPDF(os.path.join(path, bkmk.name + ".pdf"))
    
    aprx.save()
  3. Leave the Python pane open if you are going to complete the next part of the tutorial.

Build the map book

Now that you have the title page and individual maps, you are ready to build the map book. The PDFDocumentCreate function can be used to create a new, empty PDF file, while PDFDocument class can be used to merge and set properties for the document. In this exercise, you will create an empty PDF file and append the title pages and maps to it. After that, you will set properties on the final PDF such as the author, layout, and view.

  1. If necessary, on the Analysis tab, click Python Show Python window to open the Python pane.
  2. Copy and paste the following lines of code into the Python pane.
    # Create the PDF file
    pdfPath = os.path.join(path, 'GreatLakesMapBook.pdf')
    pdfDoc = arcpy.mp.PDFDocumentCreate(pdfPath)
    
    # Append pages to the PDF
    pdfDoc.appendPages(os.path.join(path, "TitlePage.pdf"))
    pdfDoc.appendPages(os.path.join(path, "Lake Erie.pdf"))
    pdfDoc.appendPages(os.path.join(path, "Lake Huron.pdf"))
    pdfDoc.appendPages(os.path.join(path, "Lake Michigan.pdf"))
    pdfDoc.appendPages(os.path.join(path, "Lake Ontario.pdf"))
    pdfDoc.appendPages(os.path.join(path, "Lake Superior.pdf"))
    
    # Set document properties – Change pdf_author to your name or the name
    # of your organization
    pdfDoc.updateDocProperties(pdf_title = "Great Lakes of the United States",
                               pdf_author = "Your Organization",
                               pdf_subject = "Great Lakes of the United States",
                               pdf_keywords = "Great Lakes; Erie; Michigan; Huron; Ontario; Superior",
                               pdf_open_view = "USE_THUMBS",
                               pdf_layout = "SINGLE_PAGE")
    
    pdfDoc.saveAndClose()
  3. Save the project and close ArcGIS Pro.

View the final map book

Now that you are finished creating the map book, you can open the PDF in the location where it was saved. While viewing the map book, you can ensure that the layout element placement is correct and that the titles have updated the way they should.

Note:

This part of the tutorial requires Adobe Reader.

  1. Double-click the GreatLakesMapBook.pdf file that was created in the previous exercise.

    Notice that it displays a single page when opened and thumbnails, which are both properties that were set for the document.

    Adobe Reader with the map book displayed
  2. Click the File menu and click Properties.

    The Document Properties dialog box appears and displays the title, author, subject, and keywords defined in the script.

    Document Properties dialog box with information populated by a function
  3. Click OK when you are finished viewing the properties.
  4. Scroll through the pages in the map book and ensure that the title and upper left and lower right coordinate information are updated for each page.
  5. Close Adobe Reader when you are finished viewing the map book.

This tutorial has guided you through the process of creating a title page and building a map book with the arcpy.mp Python module. You were also able to use Python to arrange layout elements and dynamically update the title of the map document.

Related topics