美文网首页
Python文本转语音

Python文本转语音

作者: 火卫控 | 来源:发表于2021-12-07 01:43 被阅读0次

    利用Python 的 库文本转语音

    结果为:


    .wav音频

    原文本为:

    好好学习
    天天向上
    
    

    代码如下:

    # -*- coding: utf-8 -*-
    # @Time : 2019/10/20 2:44
    # @Author : Administrator 
    # @File : yuyin1.py
    # @Software: PyCharm
    from comtypes.client import CreateObject
    
    import comtypes.client # Importing comtypes.client will make the gen subpackage
    
    try:
        from comtypes.gen import SpeechLib # comtypes
    except ImportError:
        # Generate the SpeechLib lib and any associated files
        engine = comtypes.client.CreateObject("SAPI.SpVoice")
        stream = comtypes.client.CreateObject("SAPI.SpFileStream")
        from comtypes.gen import SpeechLib
    
    #from comtypes.gen import SpeechLib
    engine=CreateObject('SAPI.SpVoice')
    stream=CreateObject('SAPI.SpFileStream')
    infile='demo.txt'
    outfile='demo_audio.wav'
    stream.open(outfile,SpeechLib.SSFMCreateForWrite)
    engine.AudioOutputStream=stream
    #读取文本内容
    f=open(infile,'r',encoding='utf-8')
    theText=f.read()
    f.close()
    engine.speak(theText)
    stream.close()
    

    相关文章

      网友评论

          本文标题:Python文本转语音

          本文链接:https://www.haomeiwen.com/subject/evtyxrtx.html