zoukankan      html  css  js  c++  java
  • pyttsx3--文字转语音库

    写在前面,部分window由于是盗版影响,部分语音库存在问题,报错无效的类字符串或者没有注册的类
    那么可能是win7 tts语音有问题体,提醒可以去下载相应补丁
    链接:https://pan.lanzou.com/i0h951c


    import pyttsx3

    #初始化
    engine=pyttsx3.init()
    #test1
    engine.say('小说说的真好')
    engine.say('what are about you ?')
    engine.runAndWait()#运行

    #test2
    #朗读文本文档或pdf文档 将文字导入say
    engine.say('读取新建文本或者文档')
    with open(r'C:Usersv_weijianyeDesktop est.txt',encoding='utf-8')as f:
    engine.say(f.read())
    engine.runAndWait()

    # #test3
    #将声音保存在文件中
    eng = pyttsx3.init()
    eng.save_to_file('好好学习天天向上,死亡如风,常伴吾身,面对疾风吧' , 'test.mp3')
    eng.runAndWait()

    # test4
    # 改变音色
    engine=pyttsx3.init()
    voices=engine.getProperty('voices')
    for voice in voices:
    engine.setProperty('voice', voice.id)
    engine.say('The quick brown fox jumped over the lazy dog.')
    engine.runAndWait()

    # engine.setProperty('voice',voices[1].id)
    # engine.say('the quick brown fox jumped over the lazy dog.')
    # engine.runAndWait()

    #test5
    #改变语速(每分钟字数的整数语音速率)
    engine = pyttsx3.init()
    rate = engine.getProperty('rate')
    engine.setProperty('rate', rate+50)
    engine.say('The quick brown fox jumped over the lazy dog.')
    engine.runAndWait()

    #test6(浮点类型0-1之间)
    #改变音量
    engine = pyttsx3.init()
    volume = engine.getProperty('volume')
    engine.setProperty('volume', volume+0.25)
    engine.say('The quick brown fox jumped over the lazy dog.')
    engine.runAndWait()


    #test7将加载到的本地文件转化为声音后存储为mp3输出
    engine=pyttsx3.init()
    with open(r'C:Usersv_weijianyeDesktop腾讯灯塔.txt',encoding='utf-8') as f:
    engine.save_to_file(f.read(),f'灯塔.mp3')
    engine.runAndWait()
    print('finish')

    import pyttsx3
    engine=pyttsx3.init()
    with open(r'C:UsersAdministratorDesktop est.txt','r',encoding='GBK')as f:
    #'utf-8' codec can't decode byte 0xbd in position 0: invalid start byte 这里encoding 报错编码格式改为GBK 或者GB2312即可解决
    engine.save_to_file(f.read(),f'test.mp3')
    engine.runAndWait()
    print('end')

     
  • 相关阅读:
    python入门 类的继承和聚合(五)
    如何快速找到多个字典中的公共键(1.4)
    python输入输出(二)
    python入门 集合(四)
    LOJ 3093: 洛谷 P5323: 「BJOI2019」光线
    LOJ 3049: 洛谷 P5284: 「十二省联考 2019」字符串问题
    【比赛游记】FJOI2019瞎打记
    ICPC World Finals 2019 题解
    LOJ 3043: 洛谷 P5280: 「ZJOI2019」线段树
    LOJ 2483: 洛谷 P4655: 「CEOI2017」Building Bridges
  • 原文地址:https://www.cnblogs.com/yescarf/p/13749629.html
Copyright © 2011-2022 走看看