I can't write arabic letters in pyautogui - python

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

Related

Issues to write a file path on Windows with PyAutoGUI

I am writing a simple Python script that goes on a website (with Selenium) and upload a file on the website. I'm using PyAutoGUI to enter the filename and press "Enter" because the website doesn't use an input.
driver.get("https://website_url.com/upload/")
elm = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "file_Picker")))
driver.find_element(By.CLASS_NAME, "file_Picker").click()
pyautogui.write("C:\\Users\\Lulucmy\\PythonProject\\test.png")
pyautogui.press('enter')
time.sleep(2)
The issue is that each time PyAutoGUI write on the upload window, the colon is replaced by a slash :
C/\\Users\\Lulucmy\\PythonProject\\test.png
What I've tried :
Replacing pyautogui.write by pyautogui.typewrite
Using pyautogui.press(':') and dividing the file path in two parts
I think the issue comes from the keyboard layout, but I couldn't find how to change it on PyAutoGUI. Also, if you could think of a solution without using PyAutoGUI I'd be glad to know it.
Thank you for your help!
No, you don't click on the filePicker. Just send the path of the file and it will work:
driver.get("https://website_url.com/upload/")
elm = WebDriverWait(driver, 20).until(EC.element_to_be_clickable
((By.CLASS_NAME, "file_Picker")))
elm.SendKeys("C:\\Users\\Lulucmy\\PythonProject\\test.png")
time.sleep(2)
I found an easy way to fix it - but if you have a "cleaner way" to solve the issue I'm happy to hear it.
The problem came from my AZERTY keayboard; PyAutoGUI seems to use the QWERTY layout by default. I used pyperclip to copy the path in the clipboard, and then paste it (ctrl + v) using .hotkey with PyAutoGUI:
import pyperclip
driver.get("https://website_url.com/upload/")
elm = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "file_Picker")))
driver.find_element(By.CLASS_NAME, "file_Picker").click()
pyperclip.copy("C:\\Users\\Lulucmy\\PythonProject\\test.png")
pyautogui.hotkey('ctrl', 'v')
pyautogui.press('enter')

Print element copied with the send_keys (Keys.CONTROL, "v")

How do I display a text that I copied with the function send_keys (Keys.CONTROL, "v") to display it with the print command?
Try to get an input and save it to a variable.
Like this:
your_text = driver.find_element_by_id("my_id").get_attribute("value")
print(your_text)
No, you can't do this with selenium alone. Selenium is a browser control and the clipboard is a system level process. You would need to involve other software to properly manipulate the clipboard.
See alos this answer for confirmation of the above statements.

How to focus on a window with python?

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)

How to send copied text to a text file using 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')

Write to a notepad typing

Can anyone help me fix this code? Trying to get it to write to a notepad but it just opens the file then stops. Any help is appreciated.
(I also want it to be like its typing instead of just c&p)
import subprocess as sp
sp.Popen(['notepad', "helloworld.txt"])
f = open('helloworld.txt','w')
import sys
from time import sleep
words = "hi"
for char in words:
sleep(0.5)
f.write(char)
sys.stdout.flush()
Let's assume that, when you request that notepad be opened in the second line of your code the file helloworld.txt does not exist.
I wrote request that notepad be opened because this takes time. Meanwhile, the rest of your code is proceeding. If you were to omit the sleep and the import statements then it would execute in a heartbeat, well ahead of the time it takes to start up notepad.
Assuming that you had closed the file helloworld.txt it would be available to notepad by the time it opened!
However, we usually don't depend on uncertain timing. (Trust me.)
We would create the file, then open it with notepad, more or less like this.
f = open('helloworld.txt','w')
words = "hi"
for char in words:
f.write(char)
f.close()
import subprocess as sp
sp.Popen(['notepad', "helloworld.txt"])
I must add that this is not the same as writing directly into notepad itself. If you're asking about that then please see pywinauto or one of the other libraries mentioned on that page.

Categories