【问题标题】:How to copy-paste data from an OS-running application with Python?如何使用 Python 从运行 OS 的应用程序中复制粘贴数据?
【发布时间】:2021-01-08 12:36:12
【问题描述】:

我需要编写一个应用程序,它主要关注给定的 Windows 窗口标题并在记事本中复制粘贴数据。我已经设法用 pygetwindow 和 pyautogui 实现了它,但它有问题:

import pygetwindow as gw
import pyautogui


# extract all titles and filter to specific one
all_titles = gw.getAllTitles()
titles = [title for title in all_titles if 'title' in title]

window = gw.getWindowsWithTitle(titles[0])[0].activate()

pyautogui.hotkey('ctrl', 'a')
pyautogui.hotkey('ctrl', 'c')

使用Spyder,激活时偶尔会报如下错误:

PyGetWindowException: Error code from Windows: 126 - The specified module could not be found.

此外,我有兴趣在不影响在机器上工作的用户的情况下执行此过程。激活基本上使窗口弹出到前面。此外,最好不要依赖操作系统,但我还没有找到任何东西。

我尝试过 pywinauto,但 SetFocus() 方法不起作用(它有问题,已记录)。

有没有其他方法可以使整个过程不可见且更容易?

【问题讨论】:

    标签: python-3.x process


    【解决方案1】:

    不确定这是否有帮助

    我正在使用 pywinauto 来设置焦点

    import pywinauto
    import pygetwindow as gw
    def focus_to_window(window_title=None):
        window = gw.getWindowsWithTitle(window_title)[0]
        if not window.isActive:
            pywinauto.application.Application().connect(handle=window._hWnd).top_window().set_focus()
    

    【讨论】:

      猜你喜欢
      • 2013-02-23
      • 1970-01-01
      • 2011-03-05
      • 1970-01-01
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-27
      相关资源
      最近更新 更多