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

     
  • 相关阅读:
    Jenkins+Tomcat+svn+maven自动化构建简单过程
    Eclipse常用的6个Debug技巧
    在linux服务器上发布web应用的完整过程
    【转】解决response.AddHeader("Content-Disposition", "attachment; fileName=" + fileName) 中文显示乱码
    springmvc缓存和mybatis缓存
    springmvc文件上传和下载
    博客园API
    整理一下CoreGraphic和Quartz2D的知识(二)
    整理一下CoreGraphic和Quartz2D的知识(一)
    CGPoint和CGSize以及CGRect的一些方法~
  • 原文地址:https://www.cnblogs.com/yescarf/p/13749629.html
Copyright © 2011-2022 走看看