How to add a Python script to the “Send To” menu in Windows

By Stefan Nikolaj on December 17, 2023. Tags: python, tutorial.

I always like looking for automated solutions to repetitive tasks, even if they take me much more time to make than I save by automating them – something about context switching or whatever that in the end makes me more productive. Also, writing scripts helps me practice programming languages.

As I have a blog and am currently in Germany, I want to save electricity so that the turtles don’t all die out or something. A long time ago, I switched all of the photos on my blog to WebP – I even have a blog post about that! However, the process of converting photos to WebP was always manual for me through the command line. I don’t trust free online converters, so this was the next best option. However, given my newfound productivity and the need to post more things online, I needed more and more conversions, which I found quite annoying to do. So, this morning I decided to make a script to convert photos to WebP using a very simple and effective method – the Windows “Send To” menu (the option that appears when you right click on a thing). This is a full tutorial on how to do this for your own scripts, assuming you’re on Windows 10/11.

First, you need to write the Python script. I used a virtual environment since my system kept breaking. Alright, follow me in the command line. First, go to a folder where you want to keep your code:

mkdir script
cd script
py -m venv .

(if you’re on Powershell and this command doesn’t work, open another Powershell window as an administrator and run the command Set-ExecutionPolicy -ExecutionPolicy RemoteSigned)

.\scripts\activate

pip install pyinstaller

These commands will make a directory with a virtual environment which has pyinstaller installed. This library will help us later to turn our python file into an executable, which is preferred for the “Send To” capability.

This tutorial will use an image-to-WebP converter as the example, but you can do whatever you want and Python can do. This example will use Google’s cwebp.exe converter which you can find on Google’s website (cwebp  |  WebP  |  Google for Developers, look for the current link at the bottom). You need to have it downloaded and put the exe in the root of your folder which you created. In the same root of the folder, create a new file called script.py or something script-y. 

I wrote this code for my conversion (you can find the most current version on GitHub):

import sys
import subprocess

# these extensions can be converted, special exception is animated PNGs
# you can change this for the extension you want
image_extensions = ["jpg", "jpeg", "png", "tiff"]

# CHANGE THIS TO YOUR CWEBP LOCATION
cwebp_location = "C:\\Users\\Stefan\\Desktop\\Skripte\\python_cwebp\\cwebp.exe"

# CHANGE THIS IF YOU WANT A DIFFERENT QUALITY NUMBER, GOES FROM 0 TO 100
quality = "80"

def run_script(full_filepath):
    # the following two lines remove the file location from the filename
    # can be used for other stuff
    file_location_split = full_filepath.split("\\")[0:-1]
    file_location = "\\".join(file_location_split)
    # recreate the filename and location and add a webp extension
    filename_webp = file_location + "\\" + arg.split("\\")[-1].split(".")[0] + ".webp"
    #print(cwebp_location)
    #print(full_filepath)
    #print(filename_webp)

    # the arguments need to be in an array to run
    cwebp_args = [cwebp_location, "-q", quality, full_filepath, "-o", filename_webp]
    subprocess.run(cwebp_args)

for arg in sys.argv:
    # run the script for all image extensions, doesn't cost a lot in performance
    for extension in image_extensions:
        # does the file extension match any of the given ones
        if arg.split(".")[-1].lower() == extension:
            # run the function with the full filename and filepath
            run_script(arg)

Now, in the same folder with the same virtual environment, run this command to convert the Python file to a portable executable (change the filename to your file’s name):

pyinstaller.exe -F .\py_cwebp_wrapper.py 

Your exe will be located in the dist folder. Copy the path to this folder, and then press Win + R to open a Run prompt and write “shell:sendTo”. This will open the folder where all the shortcuts to all the different options you see when you right click a file and open the “Send To” submenu. Whatever you put in here will indeed run in the menu. Here you want to create a shortcut to the executable created by the Python file. That’s it.

Table of contents: