winrt-runtime

3.2.1 · active · verified Thu Apr 16

winrt-runtime is the foundational Python package providing the runtime support and the `winrt.system` module for interacting with Windows Runtime (WinRT) APIs. It is part of a community-supported fork of the original pywinrt project, which provides modular bindings for individual Windows SDK namespaces (e.g., `winrt-Windows.Foundation`). The library, currently at version 3.2.1, is actively maintained with frequent updates, enabling Python developers to access a wide range of Windows OS features, hardware, and to build WinUI applications.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `winrt-runtime` along with specific WinRT namespace packages to synthesize and play speech. It utilizes asynchronous WinRT APIs with Python's `asyncio`. Make sure to install `winrt-Windows.Media.SpeechSynthesis` and `winrt-Windows.Media.Playback` for this example to run.

import asyncio
from winrt.system import Object
from winrt.windows.media.speechsynthesis import SpeechSynthesizer
from winrt.windows.media.playback import MediaPlayer, MediaPlayerAudioCategory

async def speak_hello_world():
    synth = SpeechSynthesizer()
    # Asynchronous operation, must be awaited
    stream = await synth.synthesize_text_to_stream_async("Hello, World from WinRT!")

    media_ended_event = asyncio.Event()
    loop = asyncio.get_running_loop()

    def on_media_ended(sender: MediaPlayer, args: Object):
        loop.call_soon_threadsafe(media_ended_event.set)

    player = MediaPlayer()
    player.audio_category = MediaPlayerAudioCategory.SPEECH
    player.set_stream_source(stream)
    player.add_media_ended(on_media_ended)

    player.play()
    await media_ended_event.wait()
    print("Speech finished.")

if __name__ == "__main__":
    # Ensure required namespace packages are installed: 
    # pip install winrt-Windows.Media.SpeechSynthesis winrt-Windows.Media.Playback
    asyncio.run(speak_hello_world())

view raw JSON →