Search code examples
vbapywin32pywinautosap-gui

SAP GUI Scripting - Create PDF from print screen in pywin32


I am trying to script a SAP GUI process which looks at a custom report. The report can be "printed" and then saved as a PDF. However, once you specify "LOCL" as the output device and select the check box it goes to a print screen that isn't included in the GUI scripting output.

Does anyone know how to write script in VBA or Python to continue? I have attached a screenshot of the print dialog box that pops up that I can't figure out how to work in VBA or Python. Then the second dialog that comes up is asking for filepath and filename.

It should be possible with pywin32 but I can't figure it out. enter image description here

enter image description here


Solution

  • It should be much easier with pywinauto: pip install pywinauto. The code should look like this:

    from pywinauto import Application
    
    # handle Print dialog
    app = Application(backend="win32").connect(title="Print") # timeout (in sec.) is optional
    app.PrintDialog.OK.click() # or .click_input() for real click
    app.PrintDialog.wait_not("visible") # to make sure it is closed
    
    # handle Save dialog
    app = Application(backend="win32").connect(title="Save Print Output As") # maybe not needed if it is the same process
    app["Save Print Output As"].FileNameEdit.set_text(file_path) # or .type_keys(file_path, with_spaces=True)
    app["Save Print Output As"].SaveButton.click() # or .click_input()
    app["Save Print Output As"].wait_not("visible")