Pyautogui library is not working properly on CMD program - python

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.

Related

Python script only working when ran in Visual Studio Code

My python code would only run if it was placed into Visual Studio Code. The code still works but when I click on the script to run it, it doesn't work, and when I use a command to run it, it doesn't work either.
# importing the module
import time
import wikipedia
import io
# looks for a random page and stores the topic in random
random = wikipedia.random(pages=1)
#searches for the topic and stores it in wiki
wiki = wikipedia.summary((random), sentences = 1)
# print the output of wiki
file = open("test.txt", "w")
file.write(wiki)
file.close
I also have an AutoHotkey script to run it but that doesn't work either.
That could mean that you don't have python added to path.
This is how you add python to path:
1. Start the Run box and enter sysdm.cpl
2. In the System Properties window go to the Advanced tab and
click the Environment Variables button
3. In the System variable window, find the Path variable and
click Edit
4. Position your cursor at the end of the Variable value line
and add the path to the python.exe file, preceeded with
the semicolon character (;)

How to move line (not cell) up/down in jupyter notebook using keyboard shortcut?

How can I move a line (or set of selected lines) down or up in jupyter notebook. Are there any shortcuts?
After moving line 3 one line up:
For example ALT+UP or ALT+DOWN is for up/down displacement that is used in Eclipse IDE.
This question discusses cell displacement.
TLDR:
By following this tutorial, I was able to enable all the sublime-text shortcuts (which includes the moving lines with Ctrl+Up/Ctrl/Down).
I then was able to furthermore, customize the shortcuts by editing the sublime.js
Complete answer:
Find your jupyter path with jupyter --config-dir
Add the following code to <JUPYTER-PATH>/custom/custom.js:
require(["codemirror/keymap/sublime", "notebook/js/cell", "base/js/namespace"],
function(sublime_keymap, cell, IPython) {
// setTimeout(function(){ // uncomment line to fake race-condition
cell.Cell.options_default.cm_config.keyMap = 'sublime';
var cells = IPython.notebook.get_cells();
for(var cl=0; cl< cells.length ; cl++){
cells[cl].code_mirror.setOption('keyMap', 'sublime');
}
// }, 1000)// uncomment line to fake race condition
}
);
You need to find this codemirror/keymap/sublime.js (that is required in the code from point 2), for me, it was in <MY-PYTHON-ENVIRONMENT>/lib/python3.8/site-packages/notebook/static/components/codemirror/keymap/sublime.js
At the end of this file, you can edit the shortcuts, in the keyMap.pcSublime section of this file (if you have a PC).
For OP, the lines to edit would be: swapLineUp and swapLineDown
I did this with Ubuntu & sublime installed.
You can select the line with mouse in jupyter notebook. Drag it
up or down before the line where you want to move it. Press >
button on keyboard while your text is still selected.
This will bring the cursor to end of selected line,
but before the text of line being replaced. Press Enter.
Since jupyter works with code blocks you can use that shortcut between code blocks as
Ctrl + Shift + -
But this will only works between code cells not in the same code block. I don't think there is a built-in shortcut for that. For more shortcuts you can look here: https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/
There's no such a shortcut in Jupyter, however, you can manage your own shorcuts Go to Help > Edit keyboard Shortcuts and start setting your commands.
There's some good info on this other post which will lead you to manage your profile's notebook.json file and so on.
Hope this was useful ;)
Edit: typo
You can create your custom shortcuts in Jupyter notebook
Jupyter Shortcuts

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)

Deleting blank lines from Jupyter notebook

Is there an easy way of removing blank lines from IPython notebook?
I have picked up a habit of blank lines from web development and my fingers tend to hit enter automatically. This makes IPython notebooks less(by taking too much of my 14" screen), not more readable in most cases.
I was wondering if there is a way of automatically remove blank lines from the notebooks.
In notebooks, I think preferred way of splitting the code is by placing each separate method to a different cell.
Is commonly accepted style guide for notebooks or does PEP 8 apply as it is?
EDIT: I think question 2, Is answered by IPython docs. https://github.com/ipython/ipython/wiki/Dev:-Coding-style
If you are talking about deleting the empty line from jupyter notebook which opens in web browser then press Esc and D(keyboard key) 2 times.
You can also see all the keyboard shortcuts in the jupyter notebook file
by clicking on the help and then from dropdown on keyboard shortcuts.
Pressing Esc and D (2 times) will delete only blank lines starting from the beginning. It won't delete any lines which have some text or code.
Pressing Esc and D (2 times) deletes the complete cell.
I guess the Question is about deleting the blank lines, unless the author interpretation of blank lines are blank cells.
If you came here looking for "How to delete a single line (whether code line or blank line) in a jupyter notebook cell"
Ans: ctrl-d (in Edit mode)
Using a Mac:
to forward delete a line from the curser use Fn + command + delete
to backward delete a line from the curser use command + delete
to delete a whole cell use Esc + (d twice)
These, and other shortcuts, can be found under the Help tab.
Go to the required line and press: ESC + D (together twice)
Use Ctrl+Backspace or Ctrl+Delete both will delete blank lines.
Ctrl+Backspace can be used to delete whole text of line before cursor position.
Similarly Ctrl+Delete to delete text after cursor.
If you came here looking for "How to delete a single line (whether code line or blank line) in a jupyter notebook cell"
ctrl-d (in Edit mode)
I think the question is about deleting an empty new line of a cell.My solution is to write something in the line you want to delete, then go to the end of the line above it and press delete till the junk written in the line to be deleted is cleared.This deletes the empty new line as well.
Select the row you want to delete and click the scissors icon highlighted in the below screenshot -

Python win32com.client pauses from Visio error and does not continue to execute rest of code

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.

Categories