Printing maps with PrintMap()

Sending your map layout to a printer is easy with the PrintMap() function. By default, the print job will be sent to the default printer saved with the map document, but you can also define a specific printer to which the job should be sent.

Getting ready

The arcpy.mapping module provides a PrintMap() function to print page layouts or data frames from ArcMap. Before calling PrintMap(), it is a common practice to call the ListPrinterNames() function, which returns a list of the available printers for the local computer. A particular printer can then be found by iterating the list of printers that can be used as an input for the PrintMap() function.

PrintMap() can print either a specific data frame or the page layout of a map document. By default, this function will use the printer saved with the map document or, the default system printer in the map document. As I mentioned earlier, you can also use ListPrinterNames() to get a list of the available printers, and select one of these printers as an input for PrintMap(). In this recipe, you will learn how to use the PrintMap() function to print the layout.

How to do it…

Follow these steps to learn how to use the PrintMap() function to print the layout view in ArcMap:

  1. Open C:ArcpyBookCh4Crime_Ch4.mxd in ArcMap.
  2. Open the Python window.
  3. Import the arcpy.mapping module:
    import arcpy.mapping as mapping
  4. Reference the currently active document (Crime_Ch4.mxd), and assign this reference to a variable:
    mxd = mapping.MapDocument("CURRENT")
  5. Look for the Test_Performance data frame and print it if it's found:
    for df in mapping.ListDataFrames(mxd):
      if df.name == "Test_Performance":
        mapping.PrintMap(mxd,"",df)
  6. You can check your work by examining the c:ArcpyBookcodeCh4PrintingWithPrintMap.py solution file.
  7. Run the script. The script should send the data frame to the default printer.

How it works...

The PrintMap() function accepts one required parameter and a handful of optional parameters. The required parameter is a reference to the map document. The first optional parameter is the printer name. In this case, we haven't specified a particular printer to use. Since we haven't provided a specific printer; it will use the printer saved with the map document or the default system printer if a printer is not part of the map document. The second optional parameter is the data frame that we'd like to print, which in this instance is Test_Performance. Other optional parameters, not supplied in this case, are an output print file and image quality.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset