Posts: 6
Threads: 1
Joined: Mar 2017
Hi All,
Need some help in writing a python script. Currently am using windows, below is the problem description
If i select a text and copy it using CTRL+C and then use some keyboard short cut say ctrl+alt+1 then the selected text has to be automatically pasted to some specific column in an excel sheet, similarly if i use ctrl+alt+2 then the text has to be pasted to some other column in an excel sheet.
How can i achieve this?
How can i write a py script to read the copied text and automatically paste it to specific column in excel sheet upon using a keyboard shortcut?
Thanks and Regards
Ambi
Posts: 2,953
Threads: 48
Joined: Sep 2016
Hello!
See Xerox and Pyperclip modules. There is also clipboard but it uses Pyperclip
Posts: 6
Threads: 1
Joined: Mar 2017
Hi Wavic,
Thanks for the reply... i will check those modules, But how do i get the context of the copied text using control+C from windows to python context ? Do you have any sample script snippets or ideas ?
Posts: 2,953
Threads: 48
Joined: Sep 2016
Mar-03-2017, 12:22 PM
(This post was last modified: Mar-03-2017, 12:22 PM by wavic.)
Well, I've opened Pyperclip github page and copied some text to the clipboard. In Python is simple:
1 2 3 4 |
>>> import pyperclip
>>> from_clipboard = pyperclip.paste()
>>> print (from_clipboard)
Pyperclip is a cross - platform Python module for copy and paste clipboard functions. It works with Python 2 and 3.
|
The same is for the other modules. See their webpages for more info. In Windows first install pywin32 module
Hm! Xerox requires pywin32. For Pyperclip it's not needed
Posts: 6
Threads: 1
Joined: Mar 2017
Okie, Thanks a lot Wavic, i will work on this !
Posts: 6
Threads: 1
Joined: Mar 2017
Hi wavic,
I installed pyperclip and am able to read the clipboard contents to my script and print it. Also i have integrated the other part to write it to file. But some how am not able to integrate these. I mean to say
Now after i get the clipboard contents, the python script should be running in background to read keyboard events such as CTRL+ALT+1. Now when it detects this key combination is pressed it has to further proceed to paste those contents to a file. How do i do that? Am unable to install Tkinter module. Also i tried to work with msvrt module but somehow unable to achieve that. Can you give your suggestions?
Thanks and Regards,
Narendra
Posts: 2,953
Threads: 48
Joined: Sep 2016
Mar-09-2017, 02:14 PM
(This post was last modified: Mar-09-2017, 02:14 PM by wavic.)
Hm! You got me... I do not use Windows at all.
See this On SO. You have to use nested if statement to check for ctrl then alt then 1 key before to proceed with the clipboard and writing its content to a file.
Or you can use a different approach. Write the simple script to get the clipboard contend and add/write it to a file. Just that. And when you need it run it with a key combination by your choice. It will do its job then will exit until the next time you need it.
Posts: 6
Threads: 1
Joined: Mar 2017
I tried using pyhook module, am able to handle the key press for single keys, but key combination am trying to work it out.
Posts: 2,953
Threads: 48
Joined: Sep 2016
Mar-10-2017, 12:09 PM
(This post was last modified: Mar-10-2017, 12:10 PM by wavic.)
Show the code. And the errors if there is any
Posts: 6
Threads: 1
Joined: Mar 2017
Mar-10-2017, 12:30 PM
(This post was last modified: Mar-10-2017, 12:30 PM by Ambidexter2017.)
Below is the code snippet,
But its not fruitful :( !!, even if i press ctrl+c its going to else block
[python]
def OnKeyboardEvent(event):
if event.Key.lower() in ['lwin', 'tab', 'lmenu', 'lcontrol']:
print "Control or win or tab or alt is pressed"
if event.Ascii == 99:
print "control+c is pressed"
exit()
else:
print 'MessageName:',event.MessageName
print 'Ascii:', repr(event.Ascii), repr(chr(event.Ascii))
print 'Key:', repr(event.Key)
print 'KeyID:', repr(event.KeyID)
print 'ScanCode:', repr(event.ScanCode)
print '---'
print "The pressed key does not match Control+c"
hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages ()
</python>
|