0

Here's my code for my third parameter in my Python Toolbox. When I run the toolbox and look at the list of printers, it only shows one of them. My print command of 'printerName' lists all of them in a seperate script. How can i get printerName to loop through all of the printers and put them in the list? Is there a way to make a loop on one line? Thanks

    # Third Parameter
    printer = arcpy.Parameter(
        displayName="Select a Printer",
        name="printer",
        datatype="String",
        parameterType="Required",
        direction="Input")

    for printerName in arcpy.mapping.ListPrinterNames():
        print printerName

    # Set a value list of Add or Remove
    printer.filter.type = "ValueList"
    printer.filter.list = [printerName]

Update 2

Here's the code for the third parameter that list the local printers:

    # Third Parameter
    printer = arcpy.Parameter(
        displayName="Select a Printer",
        name="printer",
        datatype="String",
        parameterType="Required",
        direction="Input")
    # Set a value list for the available local printers
    printer.filter.type = "ValueList"
    printer.filter.list = arcpy.mapping.ListPrinterNames()

This works and lists all of my local printers. The problem is it can't print the looped MXDs. It looks like python can't find them in the loop according to my arcpy.AddMessage(). Here's the code:

    # Get inputs
    InputMXD = parameters[0].valueAsText
    PDF_Folder = parameters[1].valueAsText
    printer = parameters[2].valueAsText

    # Set workspace as defined by user
    #env.workspace = PDF_Folder
    arcpy.env.workspace = r"c:/temp"

    #List Names for Inputs
    arcpy.AddMessage("PDF_Output: "+PDF_Folder)
    arcpy.AddMessage("Printer Name: "+printer)
    arcpy.AddMessage("MXDs before Loop: "+InputMXD)


    MXDList = arcpy.ListFiles(InputMXD)
    arcpy.AddMessage("After MXDList")
    arcpy.AddMessage(MXDList)
    for MXDPath in MXDList:
        arcpy.AddMessage("MXD Path: " + MXDPath)
        MXD = arcpy.mapping.MapDocument(MXDPath)
        arcpy.AddMessage("MXD: " + MXD)
        arcpy.mapping.PrintMap(MXD, printer)

ArcMessage It's showing the mxds before the loop, but it isn't printing them once the loop hits. Any ideas? It's showing "[]" when it should be showing what's in the "MXDList".

2

arcpy.mapping.ListPrinterNames() returns a list, so just do something like

printer.filter.list = arcpy.mapping.ListPrinterNames()

If I understand the question and code snippet, you got only one printer name because printer.filter.list = [printerName] takes the printer name from the last iteration for your for loop.

  • That worked and I feel like an idiot. Thanks for the help! – MjonesGEO Feb 4 '14 at 16:58
  • No worries. Glad that helped. – Roland Feb 4 '14 at 17:00

Your Answer

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

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