简体   繁体   English

使用Win32api的Python,模拟鼠标移动并将其复制/粘贴到磁盘文件中

[英]Python using win32api, simulate mouse movements and copy/paste into diskfile

Hello all…I am using win32api. 大家好...我正在使用win32api。 The use is to move mouse on computer screen, highlight an area, copy the content, and send the contents to an MS Excel spreadsheet. 用途是在计算机屏幕上移动鼠标,突出显示区域,复制内容,然后将内容发送到MS Excel电子表格。

Here is the code: 这是代码:

import xlwt
import win32api
import win32con 
import win32clipboard
import time

x,y = win32api.GetCursorPos()
win32api.SetCursorPos((36, 311))

# choose the contents, highlight area from 36,311 to 66,400
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0) 
time.sleep(0.05)
win32api.SetCursorPos((66, 400))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)

# copy the contents
time.sleep(0.05)
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard
aaa = win32clipboard.SetClipboardData
win32clipboard.CloseClipboard()

# write the contents into speadsheet
book = xlwt.Workbook(encoding='utf-8', style_compression = 0)
sheet = book.add_sheet('SheetOK', cell_overwrite_ok = True)
sheet.write (1, 1, aaa)
book.save("C:\\paste_write.xls")

it's not working and returning an error “Exception: Unexpected data type ”. 它无法正常工作,并返回错误“ Exception:Unexpected data type”。 It looks like the copy&paste part doesn't work out. 看起来复制和粘贴部分无法正常工作。

Can you help me? 你能帮助我吗? Thanks. 谢谢。

error: 错误:

Traceback (most recent call last):
  File "C:\Python27\simulate mouse click.py", line 27, in <module>
    sheet.write (0, 0, aaa)
  File "C:\Python27\lib\xlwt\Worksheet.py", line 1030, in write
    self.row(r).write(c, label, style)
  File "C:\Python27\lib\xlwt\Row.py", line 259, in write
    raise Exception("Unexpected data type %r" % type(label))
Exception: Unexpected data type <type 'builtin_function_or_method'>

The error message shows you the line of code where it fails: 错误消息向您显示失败的代码行:

Traceback (most recent call last):
  File "C:\Python27\simulate mouse click.py", line 27, in <module>
    sheet.write (0, 0, aaa)
  ^^^ THIS YOUR FUNCTION CALL THAT FAILED

  File "C:\Python27\lib\xlwt\Worksheet.py", line 1030, in write
    self.row(r).write(c, label, style)
  File "C:\Python27\lib\xlwt\Row.py", line 259, in write
    raise Exception("Unexpected data type %r" % type(label))
  ^^^ THIS IS WHERE IT ACTUALLY FAILED IN THE LIBRARY

Exception: Unexpected data type <type 'builtin_function_or_method'>

The error message is telling you that something received an "Unexpected data type" which was of type 'builtin_function_or_method'. 该错误消息告诉您,某些内容收到的类型为“ builtin_function_or_method”的“意外数据类型”。 So the call to sheet.write happens, but it objects to the data that you're passing in. 因此,发生了对sheet.write的调用,但是它反对您传入的数据。

If you look back to what's in the value of aaa : 如果回头看一下aaa的值:

aaa = win32clipboard.SetClipboardData

Ahh - what you meant to do here is evaluate the function, but you've left off the parenthesis ( & ) . 啊-您在这里要做的是评估函数,但是省略了括号() So what has happened is that aaa has been assigned the function itself . 所以发生了什么事,就是给aaa 本身分配了功能。 To call the function, look at this answer for details Troubles with clipboard in Python 要调用该函数,请查看此答案以获取详细信息Python中带有剪贴板的问题

You've also left of the parenthesis from the function on the line above that. 您还从该行上方的函数中删除了括号。

win32clipboard.EmptyClipboard

Instead try the following: 相反,请尝试以下操作:

win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText("test data")
...
aaa = win32clipboard.GetClipboardText()

There seems to be a second problem here though, that you have to pass data to SetClipbardData - it doesn't trigger a copy action on the current selected text. 但是,这里似乎还有第二个问题,您必须将数据传递给SetClipbardData它不会触发对当前所选文本的复制操作。 You might need to use a different API for that. 您可能需要为此使用其他API。

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

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