We can use the pycaw library to set the Windows Audio volume using Python.
First, we install the library using
pip install pycaw
Note: pycaw does not work with WSL (Windows Subsystem for Linux)! You actually need to install it using a Python environment running on Windows. I recommend Anaconda.
Now we can set the volume to half the current volume using this script:
from ctypes import cast, POINTER from comtypes import CLSCTX_ALL from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume import math # Get default audio device using PyCAW devices = AudioUtilities.GetSpeakers() interface = devices.Activate( IAudioEndpointVolume._iid_, CLSCTX_ALL, None) volume = cast(interface, POINTER(IAudioEndpointVolume)) # Get current volume currentVolumeDb = volume.GetMasterVolumeLevel() volume.SetMasterVolumeLevel(currentVolumeDb - 6.0, None) # NOTE: -6.0 dB = half volume !