How can I make a Python script utilize Windows Narrator to read only specific text output?

52 Views Asked by At

I'm developing a Python assistant app with various features like media playback, datetime, math calculations, and volume adjustments. One of the essential functionalities I'm working on is Text-to-Speech (TTS) capability. While I've successfully implemented TTS using Sapi5 through pyttsx3 and integrated voices like Zira and Hazel, I'm encountering difficulties in utilizing the natural voice "Sonia" from the Windows Narrator app.

I've attempted solutions such as copying the required files to the directory and replicating the registry path, but these methods haven't yielded the desired outcome. As my project aims to be entirely local, offline, and free, I prefer not to rely on online services or make purchases. Moreover, the project is intended for personal use and sharing with a few friends who share my interest.

My current approach involves using the Windows Narrator app itself for speech output, but I'm uncertain how to configure it to read only the text output generated by my assistant script. I lack knowledge about coding for the Windows OS platform and aim to avoid potential errors or complications.

I have succeeded in launching the narrator app and narrating the text, however it also narrates the name of the terminal window along with every other button out there that I press on. I will provide a basic script that works well but doesn't have the desired functions in it at the moment. Where should I go from here?

import os
import time

def speak_text(text):
    # Use os.system to launch the Narrator app and read the provided text
    os.system(f'Narrator.exe /s "{text}"')

def main():
    print("Testing Windows Narrator Text-to-Speech")

    tongue_twisters = [
        "How can a clam cram in a clean cream can?",
        "She sells seashells by the seashore.",
        "Peter Piper picked a peck of pickled peppers."
    ]

    math_operation = "Eight plus five equals thirteen."

    print("\nTongue Twisters:")
    for i, twister in enumerate(tongue_twisters, start=1):
        print(f"Tongue Twister {i}:")
        print(twister)
        speak_text(twister)
        time.sleep(2)  # Add a short delay between speaking each tongue twister

    print("\nMathematical Operation:")
    print(math_operation)
    speak_text(math_operation)

if __name__ == "__main__":
    main()
0

There are 0 best solutions below