Getting a list of available printers

Yet another list function provided by arcpy is ListPrinterNames(), which generates a list of the available printers. As is the case with the other list functions that we've examined, ListPrinterNames() is often called a preliminary step in a multistep script.

Getting ready

Before printing maps with the PrintMap() function, 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 and using it as an input for the PrintMap() function.

How to do it…

Follow these steps to learn how to use the ListPrinterNames() function to return a list of the available printers for your script:

  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. Call the ListPrinterNames() function and print each printer:
    for printerName in mapping.ListPrinterNames():
      print(printerName)
  6. You can check your work by examining the c:ArcpyBookcodeCh4GetListOfPrinters.py solution file.
  7. Run the script. The output will vary depending upon the list of available printers for your computer. However, it should print something similar to the following code snippet:
    HP Photosmart D110 series
    HP Deskjet 3050 J610 series (Network)
    HP Deskjet 3050 J610 series (Copy 1)
    HP Deskjet 3050 J610 series
    Dell 968 AIO Printer
    

How it works...

The ListPrinterNames() function returns a Python list containing all the printers available to use in your script. You can then use the PrintMap() function, which we'll examine in the next recipe, to send a print job to a particular printer that is available for your computer.

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

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