简体   繁体   中英

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

Hello all…I am using 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.

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 ”. 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'. So the call to sheet.write happens, but it objects to the data that you're passing in.

If you look back to what's in the value of 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 . To call the function, look at this answer for details Troubles with clipboard in 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. You might need to use a different API for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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