I'm writing a python code that allows a user to mark text from a website and then paste it into a word document. I'm using pyautogui and win32clipboard.
So here is the flow-
1. the user finds an interesting line on a website.
2. the user marks the desired text.
3. the user runs the python script ( I don't want python to run all the time, only when asked).
4. python uses pyautogui to copy the text (ctrl + c) and then win32clipboard.
5. python writes the copied text to a doc file.
As for now, the only problem I have is in the transition from 3 to 4.
The issues are-
a) when I try to run the python from cmd, ctrl c hotkey makes the script stop (keyboard interrupt). How can I overcome that?
b) how can I make the script run on the current website? how do I return the focus to that window? as for now, I'm running the script within Pycharm and it works, but I want it to run in the "outside world"!
Thanks in advance,
Karin :-)
P.S- this is the code I'm trying to run.
--getting the url
pyautogui.hotkey("Ctrl","f")
time.sleep(.01)
pyautogui.hotkey("Ctrl","c")
time.sleep(.01)
win32clipboard.OpenClipboard()
url = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
--- getting the marked text
pyautogui.hotkey("Ctrl","c")
time.sleep(.01)
win32clipboard.OpenClipboard()
text = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
Well, after a lot of research, I found a very simple solution for the problem- using time.sleep(). During that time the user can switch to the desired window and the code works fine :-)
You can also use
pyautogui.hotkey("alt","tab") time.sleep(.1)
Related
I am trying to automate a CMD program. I already opened on screen keyboard and "+" sign is located at (1874,919) location.
In the code shown here, I just simply move my cursor towards that location and click on the + sign. But it is not happening as per my expectation. Because according to my expectation when I press + sign it should open a window, but it is not opening and program does not throw any error, it just moves on to the next step.
This is my code:
pa.moveTo(1874,919, duration = 1) # move cursor towarsds + sign.
time.sleep(1)
pa.click(1874,919) # clicking on + plus.
A window should open in that CMD program. By the way I tried the other library "pydirectinput" also.
So basically another library worked for me. Keyboard library.
keyboard.press('+')
this line of code worked for me.
Thanks everyone.
I was trying to make a spam bot using python but it doesn't work when I try to make it arabic but it works perfectly when I make it english I really need help quickly
This is the code
time.sleep(5)
f = open('spam.txt','r')
fa = f.encode("utf-8")
for word in fa:
pyautogui.typewrite(word)
pyautogui.press("enter")
spam.txt
مرحبا
Thanks in advance
Pyautogui, I'm think, works of your keyboard and there for can't recognise some characters perhaps, I'm not sure but you can send it through a copy paste command like so.
import pyautogui
import pyperclip
import time
time.sleep(5)
# Store our string to the clipboard
pyperclip.copy("مرحبا")
# Hotkey the paste command
pyautogui.hotkey("ctrl", "v")
Tested with into a text document and it is sending the characters just fine although it may depend on the app as to weather it will display left to right or right to left. From what I understand there are characters in Unicode that tell it what side to display if you need to.
For a spam bot put it into a text document read it then put each line in your clipboard.
unfortunately, the Pyautogui doesn't support Arabic or Persian languages, for solve this problem you can type a word in TextPad then copy this manually and use hotkey to do this
from pyautogui import click, press, hotkey
# text
def type():
click(1000,701)
hotkey("ctrl","v")
press("enter")
sleep(1)
you can use pywinauto library instead. It works perfectly.
Try this:
from pywinauto.keyboard import send_keys
time.sleep(1)
send_keys(' سلام {ENTER 2}some more textt{BACKSPACE}', with_spaces=True)
remeber to add this library with
pip install pywinauto
I have copied a text from my software using pywinauto. Unfortunately, I don't know how to paste that to a text file. The following is the code that I wrote:
The last line of the code is not working as it should not. However, that is what I should do. Can anyone help me to solve this problem?
pywinauto.mouse.double_click(button='left', coords=(820,168))
pywinauto.keyboard.send_keys('^c')
f= open("trial.txt","w+")
f.write(pywinauto.keyboard.send_keys('^v'))```
I see that you're trying to paste the contents of clipboard, but there is no visual area to paste.
f.write() will accept text through a variable or, by passing some text. Invoking Ctrl + V is a GUI operation, which can't replace the text in f.write()
You can use pyperclip module to access the clipboard contents.
import pyperclip
"""yourcode"""
f.write(pyperclip.paste())
f.close()
You can also programatically copy something to system clipboard using pyperclip.
pyperclip.copy("This is a text copied to clipboard from Python script!!")
You can now check the contents by invoking Ctrl + V in some GUI application like notepad.
You can try send it hotkey
pyautogui.hotkey('ctrl','v')
I'm opening and archiving Visio files.
visio = comclt.Dispatch("Visio.Application")
wsh= comclt.Dispatch("WScript.Shell")
wsh.AppActivate("Microsoft Visio")
for i in os.listdir(path): #loops through the path
if i[-3:]=='vsd': #checks to see if it is a visio file
doc = visio.Documents.Open(path+'\\'+i)
But when I open certain Visio files, because the visio file was created on a different machine where a local stencil was present, there is an error of .vss is part of workspace but cannot be opened. This is not a problem, I can just hit ok. So I've put in code to send key ENTER.
Here is my problem. I have the code below. But it does't work because (I think) the code pauses on doc = visio.Documents.Open(path+'\\'+i) and does not continue until ok is pressed. Once I press ok manually, the code sleeps for 2 seconds before continuing.
time.sleep(2)
wsh.AppActivate("Microsoft Visio")
wsh.SendKeys("{ENTER}")
how do I tell python to not wait for doc = visio.Documents.Open(path+'\\'+i)? or is another way to solve this?
You can try to use .AlertResponse to prevent message boxes in Visio:
http://msdn.microsoft.com/en-us/library/office/ff767782.aspx
i.e. before opening the diagram, set
Visio.AlertResponse = 1
This should prevent the message from popping up.
Is there any easy way to handle multiple lines user input in command-line Python application?
I was looking for an answer without any result, because I don't want to:
read data from a file (I know, it's the easiest way);
create any GUI (let's stay with just a command line, OK?);
load text line by line (it should pasted at once, not typed and not pasted line by line);
work with each of lines separately (I'd like to have whole text as a string).
What I would like to achieve is to allow user pasting whole text (containing multiple lines) and capture the input as one string in entirely command-line tool. Is it possible in Python?
It would be great, if the solution worked both in Linux and Windows environments (I've heard that e.g. some solutions may cause problems due to the way cmd.exe works).
import sys
text = sys.stdin.read()
After pasting, you have to tell python that there is no more input by sending an end-of-file control character (ctrl+D in Linux, ctrl+Z followed by enter in Windows).
This method also works with pipes. If the above script is called paste.py, you can do
$ echo "hello" | python paste.py
and text will be equal to "hello\n". It's the same in windows:
C:\Python27>dir | python paste.py
The above command will save the output of dir to the text variable. There is no need to manually type an end-of-file character when the input is provided using pipes -- python will be notified automatically when the program creating the input has completed.
You could get the text from clipboard without any additional actions which raw_input() requires from a user to paste the multiline text:
import Tkinter
root = Tkinter.Tk()
root.withdraw()
text = root.clipboard_get()
root.destroy()
See also How do I copy a string to the clipboard on Windows using Python?
Use :
input = raw_input("Enter text")
These gets in input as a string all the input. So if you paste a whole text, all of it will be in the input variable.
EDIT: Apparently, this works only with Python Shell on Windows.