Python how to paste clipboard not in terminal [duplicate] - python

I am trying to code a very basic code where I copy some text from one program and then paste it in a different program. I'm not sure how to do this as Pyperclip only seems to paste the text on the command window where I run the code. I want to be able to click on the text-editing program and then have my code paste the text there. I'm attaching my code
import pyperclip
import time
pyperclip.copy('testing')
time.sleep(5)
pyperclip.paste()
When I run this code nothing actually happens. It doesn't paste anything, not even on the command window. I have the sleep function there because that's when I take the time to click on the text-editing program so that Python pastes the text there but it doesn't work.

If all you want to do is copy content to another text editor, then try using pyautogui module. This module allows mouse/keyboard automation via python code.
Code:
import pyautogui
import time
time.sleep(5)
a = "testing"
pyautogui.typewrite(a)
The above code will start typing the word testing after 5 seconds of program execution, so you will have to open your text editor during that duration.
The best part (or the worst) about pyautogui module is that it is focus independent i.e. it works regardless of whether the current application has focus control or not.
Just a sidenote don't use pyperclip for copying/changing/accessing clipboard data, rather try win32clipboard, if you're on windows, as it allows a lot better control over the clipboard.

Although this is a rather old question the answer cost me several hours. My goal was to input a number into another program. That program however is autocompleting the input and therefore using typewrite() (write() in the current version of pyautogui) leads to unexpected behaviour.
However pyautogui helped me in the end, together with pyperclip. Here is the code I am using:
import pyperclip
import time
pyperclip.copy('hello')
time.sleep(5)
with pyautogui.hold('ctrl'):
pyautogui.press(['v'])
This solution is dirty, but it works.

Related

ubuntu python script use PyAutoGui to press some buttons not run when make the focus on the sublime text editor

I wrote this script in python
import pyautogui
import time
time.sleep(.1)
pyautogui.keyDown("ctrl")
pyautogui.press("a")
pyautogui.keyUp("ctrl")
pyautogui.press("c")
pyautogui.press("p")
pyautogui.press("p")
pyautogui.press("t")
pyautogui.press("enter")
pyautogui.keyDown("ctrl")
pyautogui.keyDown("shift")
pyautogui.press(",")
pyautogui.keyUp("ctrl")
pyautogui.keyUp("shift")
pyautogui.press("tab")
And I created a shortcut in ubuntu to run it. python3 Scripts/cpp.py
The script works correctly when I make focus on any text element (on the browser for example). But when I make focus on the sublime text or any other text editor It does not work.
What is the reason for this issue?
(This script makes sense and do something useful for me)
If you’re running your program as administrator, pyautogui won’t be able to interact with it. This can catch people out pretty easily because you can set certain programs to always run as administrator, so it won’t be the first thing you think of. This is the case for Windows anyway.
On Ubuntu, from your experience, it seems like it is actually important to run it as an admin. So I guess in general keep the privileges in mind when you have programs interacting with other programs.
Also, your script can be cleaned up a bit.
# Probably a good idea to have a bit of a slightly longer sleep.
time.sleep(0.3)
#pyautogui.keyDown("ctrl")
#pyautogui.press("a")
#pyautogui.keyUp("ctrl")
# Is equivalent to
pyautogui.hotkey("ctrl", "a")
# The next block looks like you're writing text. So write some text.
#pyautogui.press("c")
#pyautogui.press("p")
#pyautogui.press("p")
#pyautogui.press("t")
pyautogui.write("cppt")
pyautogui.press("enter")
#pyautogui.keyDown("ctrl")
#pyautogui.keyDown("shift")
#pyautogui.press(",")
#pyautogui.keyUp("ctrl")
#pyautogui.keyUp("shift")
# Again, use a hotkey here.
pyautogui.hotkey("ctrl", "shift", ",")
pyautogui.press("tab")

"Browse file" window freeze code execution in pywinauto

I am working on automating program that requires choosing file path.
After I click the browse button using code:
dlg.child_window(auto_id='btnBrwsBinFile').click()
browse window opens and freeze execution of code and I cannot control the popup window.
I have tried also different approach. I have edited text filed using code:
dlg['Edit'].set_text(path)
but then program do not see the path, it treats the field like it was never edited, like it was empty
I would like to ask if someone solved this issue before.
try:
dlg.child_window(title='btnBrwsBinFile').click_input()
pywinauto documentation:https://pywinauto.readthedocs.io/en/latest/code/pywinauto.findwindows.html
You can use UISpy.exe to get the title, class name

how to add execution time in atom terminal

script output panel doesn't take input from user so I have to use the platformio terminal plugin in atom but it doesn't show the program finished time or execution time, I know, there are some packages to show the execution time like given below. But I don't wanna use the same code in each of my file so is there any way to add if somewhere in the plugin or making some kind of modification in plugin's code file, I have looked up for this but didn't get any relevant solution regarding this. If anyone could please tell me work around this issue.it is necessary to show the execution time in terminal to show the efficiency for the code.
import time
starttime=time.time()
for i in range(1,10000):
print(i)
endtime=round(time.time()-starttime,2)
print(f'Finshed [{endtime}]')

How to add systemtray to python script?

I want to add a systemtray to an existing python based project: https://github.com/piejanssens/premiumizer
How exactly do I need to do that? I am completely new to python and I am using it for the first time, just because I want to add a little feature to and existing script.
What I want to achieve is that when the script is running that there should be a system tray icon which opens http://localhost:5000 if it is double clicked. And if it is right clicked there should be an Exit/Quit option.
I have researched a bit and I think I could achieve it with one of these two scripts https://github.com/moses-palmer/pystray or with https://github.com/Infinidat/infi.systray (I also read that infi.systray should be used because it is not dependent on pywin32 because it uses the ctypes library because that one is part of the standard Python library).
So I tried to add this code for testing to the premiumizer.py file:
from infi.systray import SysTrayIcon
def say_hello(systray):
print "Hello, World!"
menu_options = (("Say Hello", None, say_hello),)
systray = SysTrayIcon("icon.ico", "Example tray icon", menu_options)
systray.start()
But now the Console is closing itself immediately. How can I check what went wrong? Is an error log saved somewhere ?
What do I need to do to make it work? Or is there an easier way for someone "stupid" like me ?
Welcome to the world of python!
Let me assume that you copied the script you've posted into a python file and just ran the file, correct? If so, the problem is that once the script is executed the program exits and with it the tray icon.
Start an interactive console by running python or (ipython if you have it installed) in a command window and paste in your code. You'll see that the tray icon appears and stays. It disappears once you close the console.
(Remark: the code above uses the python 2.x version of print without the () and will cause an error in python 3.x, there use print("Hello, World!").)
To make this work you need to put this code somewhere in the setup/initialization part of the premiumizer. Without knowing this project I cannot be of further help where to exactly.

Clear terminal up until point x - Python

A program I'm writing takes user commands from input(), executes corresponding functions, and displays relevant text.
After about 5 commands-worth of text, the terminal becomes cluttered even when the window is maximized. What I would like to do is clear the terminal after every five commands, but only clear the text that precedes (is above) the fifth command and its output text.
More specifically, after the user has typed in the fifth command, upon pressing Return (entering the command), I want commands 1-4 and their corresponding outputs to clear off the screen but have command 5 and its output remain at the top of the terminal.
For demonstration, here is what I want the screen would look like during this process:
The above becomes the below:
Using the os module and os.system('cls') or os.system('clear') functions will not exactly work in this situation. I don't want to clear all of the text on the screen, just the text before a certain point.
So, how can I do this on Windows with Python?
Note: If the solutions are simple, I would like both a method of obliterating the text so that it cannot be scrolled back up to as well as a method that would allow users to see previous commands and text.
Using simple terminal output, there isn't really a good way to do this. Even the operation of "clearing the screen" is outside what is normally considered simple terminal output, which is why you end up calling an external program to do it.
However, a different way of handling terminal output is to use the curses library. This library allows you extensive control over exactly how your output appears on the screen, and in fact includes functions like deleteln and insdelln to delete lines of text from the screen.

Categories