I was wondering that can we change windows 10 background using python i did a bit research and find a module name ctypes but the problem was that it needs a image to apply but i want that instead of a locally saved file it should use a url of a image and refresh the background at a interval with a new image. Cuz i want to create a script which will apply an anime wallpaper in every 5 minute and the url will be taken from a api.
Please Help!
On Windows with python2.5 or higher, use ctypes to load user32.dll and call SystemParametersInfo() with SPI_SETDESKWALLPAPER action.
For example:
import ctypes
SPI_SETDESKWALLPAPER = 20
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, "image.jpg" , 0)
Your script will have to do that in two steps
Download the image from the url using a package like requests
Set the Windows Background
Related
I'm trying to make a macro program with python 3.7 on Windows10
I want to send keyboard/mouse inputs to any specific target window while the target window keep inactivated(minimized or background)
pyautogui seems to not be able to do that (If I miss something let me know plz)
pywinauto can send inputs to specific target window but it always make the target window activate.
Is there any way keeping target window inactivated????
import pywinauto
app = pywinauto.application.Application().connect(best_match='123 - Notepad', top_level_only=False, visible_only=False)
form = app.window(title_re='123 - Notepad')
for i in range(1, 10):
form.type_keys("12e12e21e")
I found the answer!!!!
app = Application(backend="win32").connect(process=12345)
form = app.window(title_re="windowtitle")
form.send_keystrokes("1234567")
Remote Execution Guide -> Tricks to run automation on a locked machine
Okey so i dont want to put my code in my python.py file
i want to load it from a pastebin/raw link.
example:
import requests
import re
r = requests.get('https://pastebin.com/raw/example')
run(r)
can this be posible in any way ?
why i need to do this ?
i have more than 25 scripts i need to update on 5 different systems, so i dont want to go in each system and update each py file
so i just need to edit my Pastebins from 1 system.
thanks.
I am writing an program for a web automation in python. Is here a ways to hide the geckodriver? So that the console (see picture) won't show up when I start the program.
console of geckodriver
here is a fraction of my code:
from selenium import webdriver
from selenium import *
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC`
driver=webdriver.Firefox()
wait=WebDriverWait(driver,120)
url = r"http://google.com"
driver.get(url) #This line starts the console (see picture)
To prevent geckodriver from displaying any windows, you need to pass -headless argument:
from selenium import webdriver
options = webdriver.FirefoxOptions()
options.add_argument('-headless')
driver = webdriver.Firefox(options=options)
driver.get('https://www.example.com')
...
driver.quit()
This worked for me in C#. It blocks both geckodriver and firefox window
FirefoxOptions f = new FirefoxOptions();
f.AddArgument("-headless");
var ffds = FirefoxDriverService.CreateDefaultService();
ffds.HideCommandPromptWindow = true;
driver = new FirefoxDriver(ffds,f);
I was able to do that after implementing PyVirtualDisplay
sudo pip install pyvirtualdisplay # Install it into your Virtual Environment
Then just import Display as follows:
from pyvirtualdisplay import Display
Then, before fetching, start the virtual display as follows:
# initiate virtual display with 'visible=0' activated
# this way you will hide the browser
display = Display(visible=0, size=(800, 600))
# Start Display
display.start()
...
# Do your fetching/scrapping
...
# Stop Display
display.stop()
I hope it helps
Here the approach which has solved it in my case. I used #thekingofravens suggestion but realized that it is just enough to create the Run.bat file which he mentions in his post. Previously I ran my programms in IDLE with F5. Because of this the Geckodriver popped up every few seconds.
My Solution: I just made the Run.bat file with the same code:
cd C:\PathToYourFileWhichIsCausingTheGeckodriverToPopUp
python FileWhichIsCausingTheGeckodriverToPopUp.py
And thats it. Just start this file when you want to run your code and the Geckodriver won't pop up. (That it works without the whole path to you program, Python has to be in PATH.)
Also, of course you can run your program from the command line with the same commands like above and without creating an extra bat file.
You have to take care of a couple of stuffs here:
Keep the useful imports. Unusual imports like from selenium import * (in your code) must be avoided.
Keep your code minimal. wait=WebDriverWait(driver,120) have no usage in your code block.
When you use the raw r switch remember to use single quotes '...'
If you initialize the webdriver instance remember to call the quit() method.
Be careful about indentation while using Python.
Here is the minimal code which uses geckodriver to open the url http://www.google.com, print the Page Title and quits the driver instance.
from selenium import webdriver
driver=webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
url = r'http://www.google.com'
driver.get(url)
print("Page Title is : %s" %driver.title)
driver.quit()
Console Output:
Page Title is : Google
So I found a very generic workaround to solve this on Windows (in Linux it doesn't seem to be an issue in the first place, can't speak for OSX).
So you need to make three files and its very awkward.
First, you make a file. call it start.bat (or anything) and put the following code in it:
wscript.exe "C:\Wherever\invisible.vbs" "C:\Some Other Place\Run.bat"
This will be the top level batch script. It will be visible for a split second while it launches a visual basic script and passes a batch script as an argument. The purpose of this is to make the next console invisible. Next we make the VBscript, invisible.vbs:
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False
Finally, we make the script that invisible.vbs is supposed to hide, we could call it Run.bat
cd C:\Wherever
python script_using_geckodriver.py
What happens is, as follows:
The first .bat file launches invisible.vbs
invisible.vbs launches the second .bat file without showing it on screen.
The second .bat file then launches the python program. Python (and geckodriver) output to the invisible cmd therefore hiding the geckodriver console window.
P.S. all of this works with PyInstaller to produce a single redistributable package the user can just click on.
Credit harrymc # superuser.com for this solution, which I found when trying to solve an unrelated problem. I tested and realized it was cross applicable to this.
https://superuser.com/questions/62525/run-a-batch-file-in-a-completely-hidden-way
I have a small program that works fine on my PC but I want to make it portable. What it does is download an image from the internet, set as desktop background, wait one minute and update the image. I know that I cannot write directly to folders like appdata, as I do not know the username of the person using the computer. I need to save the downloaded image somewhere, so I would save it in the windows Temp folder.
Some options I think would be to (However I don't know how to do this in python)
Use something like %temp% to access the folder.
Find out the username of the person running the program and insert into path
Use a variable for the username.
Use relative paths
I would like to try and not have to use another module not by default included in Python 3, as I want to cx_freeze it later on.
import pythoncom
from urllib import request
from win32com.shell import shell, shellcon
from time import sleep
def get_image():
f = open('C:\\Users\\MyUser\\Desktop\\Python\\bg\\bg.jpg', 'wb') #Open old image
f.write(request.urlopen('blalbla.com/foo/img.jpg').read()) #Get new image and write
f.close()
pathtoimg = 'C:\\Users\\MyUser\\Desktop\\Python\\bg\\bg.jpg'
count = 0
while 1:
get_image()
iad = pythoncom.CoCreateInstance(shell.CLSID_ActiveDesktop, None,
pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IActiveDesktop)
iad.SetWallpaper(pathtoimg, 0)
iad.ApplyChanges(shellcon.AD_APPLY_ALL)
count += 1
print(count)
sleep(60)
Use this to locate Temp:
import os
mytmpdir = os.environ['TEMP'] #Must be uppercase
I would like to be able to play a sound file in a ipython notebook.
My aim is to be able to listen to the results of different treatments applied to a sound directly from within the notebook.
Is this possible? If yes, what is the best solution to do so?
The previous answer is pretty old. You can use IPython.display.Audio now. Like this:
import IPython
IPython.display.Audio("my_audio_file.mp3")
Note that you can also process any type of audio content, and pass it to this function as a numpy array.
If you want to display multiple audio files, use the following:
IPython.display.display(IPython.display.Audio("my_audio_file.mp3"))
IPython.display.display(IPython.display.Audio("my_audio_file.mp3"))
A small example that might be relevant : http://nbviewer.ipython.org/5507501/the%20sound%20of%20hydrogen.ipynb
it should be possible to avoid gooing through external files by base64 encoding as for PNG/jpg...
The code:
import IPython
IPython.display.Audio("my_audio_file.mp3")
may give an error of "Invalid Source" in IE11, try in other browsers it should work fine.
The other available answers added an HTML element which I disliked, so I created the ringbell, which gets you both play a custom sound as such:
from ringbell import RingBell
RingBell(
sample = "path/to/sample.wav",
minimum_execution_time = 0,
verbose = True
)
and it also gets you a one-lines to play a bell when a cell execution takes more than 1 minute (or a custom amount of time for that matter) or is fails with an exception:
import ringbell.auto
You can install this package from PyPI:
pip install ringbell
If the sound you are looking for could be also a "Text-to-Speech", I would like to mention that every time a start some long process in the background, I queue the execution of a cell like this too:
from IPython.display import clear_output, display, HTML, Javascript
display(Javascript("""
var msg = new SpeechSynthesisUtterance();
msg.text = "Process completed!";
window.speechSynthesis.speak(msg);
"""))
You can change the text you want to hear with msg.text.