如何在 Ubuntu 22.04 和 python 中使用 pyperclip 从输入框复制/粘贴文本?

如何在 Ubuntu 22.04 和 python 中使用 pyperclip 从输入框复制/粘贴文本?

我正在使用 python 3.10.4 和 Ubuntu 22.04 创建一个 whatsapp 自动化项目。有时我们需要复制用户 web.app 发送的消息文本,对其进行处理并发送响应。

print('======== whatsapp_bot ========')
from turtle import right
import cv2 as cv
from time import sleep
# Waiting time
print('Waiting 2s')
sleep(2)

import pyautogui as pt
import paperclip as pc
from tkinter import ttk
class WhatsApp:
    def __init__(self, speed=5, click_speed=.3):
        self.speed = speed
        self.click_speed = click_speed
        self.message = ''
        self.last_message = ''

    def nav_green_dot(self):
        try:
            greenPicCenter = pt.locateCenterOnScreen('./green_pic.png', confidence=0.7)
            # print('LocateCenter green_pic', greenPicCenter)
            pt.moveTo(greenPicCenter[0]-250, greenPicCenter[1], duration=self.speed) 
            pt.doubleClick(interval=self.speed)
        except Exception as e:
            print ('Exception (nav_green_dot): ', e)

    def nav_message(self):
        try:
            clipPicCenter = pt.locateCenterOnScreen('./clip_pic.png', confidence=0.7)
            # print('LocateCenter clip_pic', clipPicCenter)
            pt.moveTo(clipPicCenter[0]+48, clipPicCenter[1]-60, duration=self.speed) 
            pt.click(clicks=3)
            sleep(.9)
            self.message = pc.copy()
            print(self.message)

            # pt.click(button='right', clicks=1)
            # pos = pt.position()
            # pt.moveTo(pos[0]+20, pos[1]-300, duration=self.speed)
            # txt = pt.click()
            # print(txt)
            # self.nav_input_box()
            # pt.write(txt)
            # txt = pt.hotkey("ctrl", "c") # copy the text (simulating key strokes)
            # pt.click(button=right)
            

        except Exception as e:
            print ('Exception (nav_green_dot): ', e)

    def nav_input_box(self):
        try:
            clipPicCenter = pt.locateCenterOnScreen('./clip_pic.png', confidence=0.7)
            print('LocateCenter clip_pic', clipPicCenter)
            pt.moveTo(clipPicCenter[0]+60, clipPicCenter[1], duration=self.speed) 
            pt.doubleClick(interval=self.speed)
        except Exception as e:
            print ('Exception (nav_green_dot): ', e)


    # def copy_clipboard(self):
    #     root = ttk()     # Initialize tkinter
    #     root.withdraw() # hide the tkinter window
    #     pt.hotkey("ctrl", "c") # copy the text (simulating key strokes)
    #     clipboard = root.clipboard_get() # get the text from the clipboard
    #     print (clipboard)

wa_bot = WhatsApp(speed = .5, click_speed = .4)
wa_bot.nav_green_dot()
wa_bot.nav_message()
# wa_bot.nav_input_box()

在该部分中self.message = pc.copy()我有以下错误消息:Exception (nav_green_dot): module 'paperclip' has no attribute 'copy'

有人知道我错过了什么吗?

相关内容