Instructions:
- Open a new python file.
- Python can connect to programs and websites using developer APIs; however, what happens if an API isn't available? The pyautogui package enables python to control the mouse and keyboard. In order to save a file as a PDF, the easiest solution is to automate the
Save As
process from within the native app. - Anytime a program takes charge of the computer, it's critical that the user has a way to terminate the program. The
pyautogui
package has a.PAUSE
variable that can be set with the number of seconds the program will wait after performing its action. Additionally, there is the.FAILSAFE
variable that controls the option to force quit the program by moving the mouse to the upper-left corner of the screen.import pyautogui import subprocess import sys import time pyautogui.PAUSE = 1.5 pyautogui.FAILSAFE = True
- Execute the command to open the file and perform a sleep action to ensure the application has opened before continuing. Depending on the hardware of the machine, the
.sleep()
timer can be extended or contracted.sys_platform = sys.platform if sys_platform == "win32" or sys_platform == "cygwin": subprocess.Popen(["start", ".\\ResumeTemplate.docx"], shell=True) elif sys_platform == "darwin": subprocess.Popen(["open", ".\\ResumeTemplate.docx"]) elif sys_platform == "linux": subprocess.Popen(["see", ".\\ResumeTemplate.docx"]) else: print(f"Unknown operating system: {sys_platform}") time.sleep(10)
- After launching the Word file to be converted to a PDF, focus needs to be applied to the application. The application first sends an
f12
command to try and open theSave As
dialog box. Ifpyautogui
locates the file type field by using an image of the option and the.locateOnScreen
function, then the program continues. Otherwise, it locates the Microsoft Word icon on the taskbar and clicks on it. Once the focus is confirmed, the program selects the file type field and typesp d f
and then presses enter once to accept the file type and a second time to save. Since the.locateOnScreen()
function returns more information than required for the.click()
function, the.center()
function is used to only return theleft
andtop
values.try: pyautogui.press("f12") time.sleep(2) type_location = pyautogui.locateOnScreen(".\\icons\\TypeField.png") if type_location is None: try: word_icon = pyautogui.locateOnScreen(".\\icons\\WordLogo.png") word = pyautogui.center(word_icon) pyautogui.click(word) except TypeError: print("Word not found") raise else: pyautogui.press("f12") time.sleep(2) except TypeError: print("Save not found") raise else: type_loc = pyautogui.center(type_location) pyautogui.click(type_loc) pyautogui.typewrite(["p", "d", "f"]) pyautogui.press(["enter", "enter"])
- Update the log file with what you have learned today.