I'm trying to create a script to open a webpage and to type stuff there, but first I wanted to try to do it using Notepad
import time
import os
import webbrowser
import pyautogui
os.system("notepad.exe")
#time.sleep(3)
pyautogui.write('Hello world!', interval=0.25)
This is what I have so far. The problem is that it only opens the Notepad and only after I close the Notepad, 'Hello world!' is printed on my terminal where I'm running the script.
What am I missing here? Is there any other way to write text with a python script?
Thanks!
This is called file handling
with open('Filename.txt', 'w') as f:
f.write('What you want to put in')
But this also works
import time
import os
import webbrowser
from pynput.keyboard import Key, Controller as K
from pynput.mouse import Button, Controller as M
os.startfile("notepad.exe")
M().position = (900,400)
M().click(Button.left, 1)
#time.sleep(3)
K().type('Hello world!')
You might want to use pyautogui's mouse functions to click the notepad and then type it. For more information on the mouse functions, go here:
https://automatetheboringstuff.com/2e/chapter20/
Related
For example, I want to open a file D:\happy\0.pyw
here I want to edit the file with IDLE, not just click open it.
When I open a .py/.pyw document, I click the right part of the mouse and choose Edit with IDLE, it works. I want to find how to do this as a code in python.
Assume I have known the position of idle.py is C:\python\idle.py
My code is below. How to change the code?
import os
def open_app(dir):
os.startfile(dir)
if __name__ == "__main__":
b=r'D:\happy\0.pyw'
open_app(b)
I wrote some code to help to copy and paste multiple lines in a program that doesn't allow you to copy and paste multiple rows nicely like excel does. I've tried writing this tool in AHK but something about AHK doesn't seem to play nice with the program in question (RSLogix studio 5000, AHK always misses lines when copy and pasting). This python code works nicely and doesn't miss lines when copying or pasting, but I've encountered a new issue. Despite what I've set the hotkeys to (ctrl 1 brings up a window asking how many lines you'd like to copy or paste, ctrl 2 copies that many lines, ctrl 3 pastes that many lines) pressing ctrl always brings up the input window. If I remove the input window and hardcode the value for "lines" then everything works as expected. Something about this window is making the program act strangely. Does anyone have any ideas?
thanks in advance for your time:)
import pyautogui
import easygui
import keyboard
import pyperclip
import time
from pynput.keyboard import HotKey, Key, KeyCode, Listener
from pynput import keyboard
copy_list=[]
lines = 0
def function_1():
global lines
lines = easygui.integerbox("Enter number of lines to copy")
print(lines)
def function_2():
global lines
copy_list.clear()
for x in range(int(lines)):
pyautogui.hotkey('ctrl', 'c')
clp=pyperclip.paste()
pyperclip.copy("")
copy_list.append(clp)
pyautogui.press('down')
print(copy_list)
def function_3():
global lines
for x in range(int(lines)):
pyperclip.copy(copy_list[x])
pyautogui.hotkey('ctrl', 'v')
pyautogui.press('enter',presses=3,interval=0.05)
with keyboard.GlobalHotKeys({
'<ctrl>+1': function_1,
'<ctrl>+2': function_2,
'<ctrl>+3': function_3}) as h:
h.join()
I tried to use this script to prevent windows screen lock. The script works for moving the mouse, but it doesn't prevent windows 10 from locking.
import pyautogui
import time
import win32gui, win32con
import os
Minimize = win32gui.GetForegroundWindow()
win32gui.ShowWindow(Minimize, win32con.SW_MINIMIZE)
x = 1
while x == 1:
pyautogui.moveRel(1)
pyautogui.moveRel(-1)
time.sleep (300)
Yes it can. But sadly not by moving mouse which I don't know why and would like to know. So, my suggestion is to use pyautogui KEYBOARD EVENTS if possible. I have solved my problems by using VOLUME-UP & VOLUME-DOWN keys. Example code is provided below:
import pyautogui
import time
while True:
pyautogui.press('volumedown')
time.sleep(1)
pyautogui.press('volumeup')
time.sleep(5)
You can use any other keys if you want.
import ctypes
# prevent
ctypes.windll.kernel32.SetThreadExecutionState(0x80000002)
# set back to normal
ctypes.windll.kernel32.SetThreadExecutionState(0x80000000)
https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate
Tested on python 3.9.1, win 10 64bit
I have a selection of excel data that I am analyzing, and have just recently added the ability for the user to open the file explorer and locate the file visually, as opposed to entering the file location on the command line. I found this question (and answer) to make the window appear, which worked for a while.
I am still using the command line for everything except locating the file. Currently, this is a skeleton of what I have to open the window (nearly identical to the answer of the question linked above)
Tk().withdraw()
data_file_path = askopenfilename()
# other code with prompts, mostly print statements
Tk().withdraw()
drug_library_path = askopenfilename()
Once the code reaches the first two lines of code, the command line just sits with a blinking cursor, like it's waiting for input (my guess, for askopenfilename() to return a file location), but nothing happens. I can't ctrl+C to get out of the program, either.
I have found this question, which is close to what I'm looking for, but I'm on Windows, not Mac, and I can't even get the window to open -- most questions I see talk about not being able to close the window.
Thanks for any help!
Note: At this point in the program, no data from excel has been loaded. This is one of the first lines that is ran.
Try easygui instead. It's also built on tkinter, but unlike filedialog it's made to run without a full GUI.
Since you are using Windows, use this command in the command line (not in python) to install easygui:
py -m pip install easygui
then try this code:
import easygui
data_file_path = easygui.fileopenbox()
# other code with prompts, mostly print statements
drug_library_path = easygui.fileopenbox()
If you want to use an internal module, you can import tkFileDialog, and call:
filename = tkFileDialog.askopenfilename(title="Open Filename",filetypes=(("TXT Files","*.txt"),("All Files","*.*")))
I use this in many projects, you can add arguments such as initialdir, and you can specify allowable filetypes!
I had the same problem, but I found that the issue was that I was getting input with input() before I called askopenfilename() or fileopenbox().
from tkinter import Tk
from tkinter.filedialog import askopenfilename
var = input()
Tk().withdraw()
filepath = askopenfilename()
I simply switched the positions of askopenfilename() (or fileopenbox()) and input(), and it worked as usual.
Tk().withdraw()
filepath = askopenfilename()
var = input()
I´m trying to execute an external script (named EC.py) by pressing a button on my GUI (named BEN.py) and I want it to be inserted on a list (list1) in my GUI.
My external script (EC.py) is like this:
import scipy
import numpy as np
from scipy import misc
from scipy import ndimage
I = scipy.misc.imread('lena.jpg').astype(int)
J = (I/10)*10
K = J + 10
Print K
and my GUI (BEN.py) code is:
import os
import Tkinter as tk
import ttk
def Execute():
EC.K
list1.insert(END, K)
my button widget:
mybutton = Button(myGUI, text=”Execute Code”, command = Execute).pack()
my output list:
list1 = Listbox(myGUI, height=20, width=80)
Everything goes reasonably fine, except that just by running my GUI code it already reads the EC.py script, before I press the button. What I want is to get this script to run only when I press the button.
I suppose (because you didn't wrote complete source code) you wrote import EC
before calling EC.K
Of course Python produce EC.pyc (compiled version) at startup to optimise multi module imports and validate the syntaxe of all implied modules.
What you want in a way is to have dynamical generated code. EC.py
I don't focused on security issues of this behaviour but the simplest way is to use:
try: execfile ("EC.py")
except Exception,msg:
print msg
raise