A useful script that only its author knows how to start remains a personal tool. To become an office tool, anyone must be able to launch it, without opening a terminal, remembering paths or typing commands. On Windows a few simple measures are enough: a well-written batch file, a shortcut on the desktop, and perhaps the option to drag the files to be processed straight onto the icon.
The batch file as a launcher
A file with the .bat extension contains Windows command prompt commands, run in sequence with a double-click. It is the simplest way to wrap a Python script:
@echo off
cd /d "%~dp0"
"C:\Python312\python.exe" rename_scans.py
pause
Each line has a precise purpose:
@echo offstops the commands themselves from being printed on screen;cd /d "%~dp0"sets the working folder to where the batch file is:%~dp0is the path of the file's own folder, and/dallows the drive to change too. That way relative paths in the script always work;- the full path to the interpreter avoids depending on how the computer is configured;
pausekeeps the window open at the end, with the message "Press any key to continue".
The last line matters more than it seems. Without it, if the script fails, the window closes instantly and the user sees only a black flash, with no idea what happened.
Dragging files onto the icon
A very intuitive interface is drag and drop: you select some files in File Explorer and drop them onto the batch file's icon. Windows passes the paths as arguments, which the batch file forwards to the script with %*:
"C:\Python312\python.exe" "%~dp0convert_pdf.py" %*
In the Python script the paths arrive in the sys.argv list, from the second element onwards:
import sys
from pathlib import Path
for argument in sys.argv[1:]:
file = Path(argument)
print("Processing", file.name)
Notice that this version uses %~dp0 in front of the script name instead of the cd command: when files are dragged, it is better not to change folder before reading the arguments, and giving the script's path in absolute form is safer.
The "Send to" menu
For a frequently used tool, the context menu is even more convenient. Typing shell:sendto in the Run window (Win+R) opens the folder containing the entries of the Send to menu. Copying a shortcut to the batch file there makes the script available by right-clicking any file. On Windows 11 the Send to menu is under Show more options.
A shortcut with an icon and a hotkey
The batch file can stay in its folder alongside the script, while a shortcut goes on the desktop. From the shortcut's properties you can:
| Setting | Use |
|---|---|
| Change Icon | Make the tool recognisable |
| Shortcut key | Launch it with a combination such as Ctrl+Alt+R |
| Run: Minimised | Hide the window for scripts with no output |
| Comment | Description shown when hovering over the icon |
Virtual environments
If the script uses libraries installed in a virtual environment, the batch file must use that environment's interpreter, found in the environment's Scripts subfolder:
"%~dp0.venv\Scripts\python.exe" "%~dp0extract_tables.py" %*
This way the tool always uses the library versions it was tested with, regardless of what is installed in the rest of the system.
Building an executable
When the tool has to work on computers without Python installed, you can turn the script into an .exe file with PyInstaller:
pip install pyinstaller
pyinstaller --onefile convert_pdf.py
The result, in the dist folder, contains the interpreter and the libraries it needs. It is convenient, but it has some drawbacks: the file is fairly large, the --onefile version starts more slowly because it has to unpack its components, and some antivirus programs view executables built this way with suspicion. Moreover, every change to the script means rebuilding the executable. In my work I keep it for stable tools; for those that change often I prefer the batch file with a virtual environment in a shared folder.
What about macOS?
The equivalent of a batch file is a text file with the .command extension, containing the same commands you would type in Terminal. It must be made executable once with chmod +x name.command; from then on it starts with a double-click and opens a Terminal window.
In short
- A
.batfile withcd /d "%~dp0"andpausemakes a script launchable by anyone. %*lets you drag files onto the icon; the script reads them fromsys.argv.- The Send to menu and shortcuts with hotkeys keep the tool within reach.
- Use the virtual environment's interpreter to get tested library versions.
- PyInstaller builds executables for computers without Python, with some trade-offs.