小案例:用Python制作一个语音朗读工具

朗读工具使用步骤如下:

        1.需要输入要朗读的内容。

        2.需要设置语速和发音

        3.朗读内容

下面是程序展示:

小案例:用Python制作一个语音朗读工具_第1张图片 

设计该程序时需要导入两个第三方库: PySimpleGUI,pyttsx3。要想让朗读效果更佳可以导入_thread库新建线程以供朗读。

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'}

接着,我们需要让pyttsx3先初始化以下:

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('退出')]
    ]

最后是窗口的代码:

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()

接下来是全部代码展示:

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()

以上便是全部内容,感谢大家的观看!

你可能感兴趣的:(python,开发语言,windows)