Search code examples
pythonpython-3.xwinapiprintingrundll32

Python get printer dialog configuration set by the user


So I am trying to make a python Windows application that has a button where it displays the user the printer dialog so he can set the configuration for the printer. When I say printer dialog I mean this one:

windows printer dialog

In my program I need to have two printer configurations available at a given time, if it is a certain case I should use the first configuration or else use the second configuration, so my idea is to have two buttons to open the dialog and the user make in each the configuration that he needs and save that configuration to a variable in python and when is time to print select the appropriate configuration. I am not 100% sure that this is possible, but I have seen some examples of this working with .net and Windows Forms, but not sure if I can do it with python.

Currently what I am doing is this:

def open_printer_config():
   dialog = "C:\\Users\\bruno\\Documents\\Personal Projects\\python-printserver\\temp.txt"
   filepath = "C:\\windows\\system32\\mshtml.dll,PrintHTML " + "\"" + dialog + "\"" 
   win32api.ShellExecute(0 , None , "rundll32.exe" , filepath , None , 1)

I could not find a way to open the printer dialog besides using a file to be call like if I would actually print it, that's why I created a temp text file just to show the dialog. Whit this I can't really save the configuration to a variable and it also get's reset every time I click it. Before I was using this:

subprocess.run(f"RUNDLL32 PRINTUI.DLL,PrintUIEntry /e /n \"{PRINTER_SELECTED}\"", shell=True)

The issue here is that it saves the default printer configuration, so I can't really have two sets of configuration available at the same time.

Is what I want even possible with python? Is there a way to save the user configuration to a variable?

EDIT

I have a new idea now to save the configuration, but it requires to me know when the print dialog is closed. Is there a way to listen to when a windows system window (printer dialog) is closed to run some code after it was closed? My idea is after the user closes the print dialog get the printer settings and save in a global variable.


Solution

  • I solve the issue, I was not aware that when you run subprocess it will halt the code until it is resolved, so just changing the order of some things solved my issue. So now I am saving the configuration as soon as the window for the printer dialog is closed and I have another button to deal with the second configuration.

    def open_printer_config():
            global PRINTER_SELECTED
            global configutarion_printer                     
            infoProcess = subprocess.run(f"RUNDLL32 PRINTUI.DLL,PrintUIEntry /e /n \"{PRINTER_SELECTED}\"", shell=True)
            PRINTER_DEFAULTS = {"DesiredAccess":win32print.PRINTER_ALL_ACCESS} 
            # get a handle to the printer
            pHandle = win32print.OpenPrinter(PRINTER_SELECTED, PRINTER_DEFAULTS)
            properties = win32print.GetPrinter(pHandle, 2)
            configutarion_printer = properties["pDevMode"]