Caso pequeño: haz una herramienta de lectura de voz con Python

Los pasos para utilizar la herramienta de lectura son los siguientes:

        1. Debe ingresar el contenido para que se lea en voz alta.

        2. Debe configurar la velocidad del habla y la pronunciación

        3. Leer contenido

Esta es la presentación del programa:

 

Al diseñar este programa, se deben importar dos bibliotecas de terceros: PySimpleGUI, pyttsx3. Para mejorar el efecto de lectura, puede importar la biblioteca _thread para crear un nuevo hilo para leer.

from pyttsx3 import init
from PySimpleGUI import Window,Slider,B,T,ML,Drop
from _thread import start_new_thread

Luego, necesitamos configurar la pronunciación, necesitamos usar el diccionario para configurar la pronunciación con el nombre de pronunciación seleccionado por el usuario:

vioce = {'普通话':'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_ZH-CN_HUIHUI_11.0','粤语':'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_ZH-HK_TRACY_11.0',
         '台湾普通话':'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_ZH-TW_HANHAN_11.0'}

A continuación, debemos dejar que pyttsx3 inicialice lo siguiente:

say = init()

Cree un nuevo hilo para leer el código. Los parámetros son el contenido de lectura, la pronunciación de lectura, la voz de lectura y la velocidad de lectura:

def read_text(text,rate,volume,voice):
    say.setProperty('rate',rate)
    say.setProperty('volume',volume / 100)
    say.setProperty('voice',voice)
    say.say(text)
    say.runAndWait()
    say.stop()

El siguiente es el diseño de la ventana de la herramienta de lectura:

layout = [
    [T('请在下面写入要朗读的内容',font = (None,15))],
    [ML(font = ('宋体',13),size = (50,10),autoscroll = 1)],
    [T('请设置朗读语速',font = ('宋体',12)),Slider(range = (1,250),default_value = 150,orientation = 'h')],
    [T('请设置朗读音量',font = ('宋体',12)),Slider(range = (1,100),default_value = 100,orientation = 'h')],
    [T('请设置朗读发音',font = ('宋体',12)),Drop(list(vioce.keys()),size = (10,1),default_value = '普通话')],
    [B('开始朗读'),B('退出')]
    ]

Y finalmente el código de la ventana:

while 1:
    event,values = window.read()
    if event in (None,'退出'):
        break
    elif event == '开始朗读':
        try:
            start_new_thread(read_text,(values[0],values[1],values[2],vioce[values[3]]))
        except RuntimeError:
            print('不要再朗读的过程中点击其他按钮哦!')
window.close()

A continuación se muestra el código completo:

from pyttsx3 import init
from PySimpleGUI import Window,Slider,B,T,ML,Drop
from _thread import start_new_thread

vioce = {'普通话':'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_ZH-CN_HUIHUI_11.0','粤语':'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_ZH-HK_TRACY_11.0',
         '台湾普通话':'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_ZH-TW_HANHAN_11.0'}

say = init()

def read_text(text,rate,volume,voice):
    say.setProperty('rate',rate)
    say.setProperty('volume',volume / 100)
    say.setProperty('voice',voice)
    say.say(text)
    say.runAndWait()
    say.stop()

layout = [
    [T('请在下面写入要朗读的内容',font = (None,15))],
    [ML(font = ('宋体',13),size = (50,10),autoscroll = 1)],
    [T('请设置朗读语速',font = ('宋体',12)),Slider(range = (1,250),default_value = 150,orientation = 'h')],
    [T('请设置朗读音量',font = ('宋体',12)),Slider(range = (1,100),default_value = 100,orientation = 'h')],
    [T('请设置朗读发音',font = ('宋体',12)),Drop(list(vioce.keys()),size = (10,1),default_value = '普通话')],
    [B('开始朗读'),B('退出')]
    ]

window = Window('文字朗读工具',layout)

while 1:
    event,values = window.read()
    if event in (None,'退出'):
        break
    elif event == '开始朗读':
        try:
            start_new_thread(read_text,(values[0],values[1],values[2],vioce[values[3]]))
        except RuntimeError:
            print('不要再朗读的过程中点击其他按钮哦!')
window.close()

Eso es todo por ahora, ¡gracias por mirar!

Supongo que te gusta

Origin blog.csdn.net/hu20100913/article/details/129758083
Recomendado
Clasificación