简体   繁体   English

如何打开一个程序并从 python 脚本中按下一个键

[英]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.我在 PyCharm 中运行我的脚本,但我正在尝试自动化一个过程。

Before running the PyCharm script I do the following manual tasks:在运行 PyCharm 脚本之前,我执行以下手动任务:

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.然后我使用生成的电子表格在 PyCharm 中运行分析脚本。

Is there anyway I can automate the first two steps from within a PyCharm Script?无论如何,我可以在 PyCharm 脚本中自动执行前两个步骤吗?

Thanks very much!非常感谢!

You can start by installing the PyAutoGui module.您可以从安装 PyAutoGui 模块开始。 pip install pyautogui from your command line.从命令行 pip install pyautogui 。 There's a lot of helpful functions here you can use but for your purpose pyautogui.press('y') should work fine.这里有很多有用的功能您可以使用,但出于您的目的 pyautogui.press('y') 应该可以正常工作。 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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM