SOK
SOK

Reputation: 1792

How to open up a program and press a key from a python script

This is more of a theoretical question but not sure where to start.

I run my scripts inside of PyCharm but am trying to automate a process.

Before running the PyCharm script I do the following manual tasks:

1 - open an .exe program (from desktop)
2 - once the program opens i need to press the "Y" key to produce a spreadsheet to be used

And then I run the analysis script in PyCharm using the generated spreadsheet.

Is there anyway I can automate the first two steps from within a PyCharm Script?

Thanks very much!

Upvotes: 0

Views: 98

Answers (1)

Sulayman
Sulayman

Reputation: 61

You can start by installing the PyAutoGui module. pip install pyautogui from your command line. There's a lot of helpful functions here you can use but for your purpose pyautogui.press('y') should work fine. So if you try:

import pyautogui
from time import sleep
import subprocess

subprocess.Popen("directory to app's exe")
# Delay for app to load up
sleep(10)
pyautogui.press("Y")

Upvotes: 1

Related Questions