首页 > 编程语言 >python 文字转语音

python 文字转语音

时间:2022-10-08 16:15:32浏览次数:60  
标签:文字 python speaker 语音 print import audio

#pyttsx3文字转语音

import pyttsx3
engine2 = pyttsx3.init()
while True:
content = input('请输入播放内容:')
engine2.say(content)
engine2.runAndWait()

#文字转语音
from win32com.client import Dispatch
msg = '你好'
speaker = Dispatch('SAPI.SpVoice')
speaker.Speak(msg)
del speaker

#comtypes模块 文字文件转语音文件(当前仅支持英文)

from comtypes.client import CreateObject
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()

#PocketSphinx模块 SpeechRecognition模块 语音转文字
import speech_recognition as sr
audio_file = 'demo_audio.wav'
r = sr.Recognizer()
with sr.AudioFile(audio_file) as source:
audio = r.record(source)
# try:
# print('文本内容:',r.recognize_sphinx(audio,language="zh_CN"))
print('文本内容:',r.recognize_sphinx(audio))

# except Exception as e:
# print(e)
#https://sourceforge.net/projects/cmusphinx/files/Acoustic%20and%20Language%20Models/ 中文包的网址

标签:文字,python,speaker,语音,print,import,audio
From: https://www.cnblogs.com/lld76/p/15995227.html

相关文章

  • python 音频合成,图片转视频
    importosimportcv2,reimporttimeimportpyttsx3importffmpegimportsubprocessfromPILimportImageimportnumpyasnpimportimageioimageio.plugins.freeimage.dow......
  • Python基础语法:函数
    1函数定义 1.1函数概述在程序设计中,函数的使用可以提升代码的复用率和可维护性。提升代码的复用率:程序设计中,一些代码的功能是相同的,操作是一样的,只不过针对的数据......
  • Python tkinter 制作文章搜索软件,有没有方便快捷不知道,好玩就行了
    前言无聊的时候做了一个搜索文章的软件,有没有更加的方便快捷不知道,好玩就行了环境使用Python3.8Pycharm模块使用importrequestsimporttkinteras......
  • Python多线程
    一、概念线程是CPU分配资源的基本单位,当程序开始运行,这个程序就变成了一个进程;当有多线程编程时,一个进程包含多个线程(含主线程),使用线程可以实现程序大的开发任务。多线......
  • python -list赋值给变量-读取list中数据
    常规读取list中数据#使用索引list=[1,2,3]a=list[1]#循环遍历foriinlist: print(i)快速赋值a,b,c=list#这种方式只有当左边的操作数个数和list长度相同,也......
  • 【Python小工具】爬虫之获取图片验证码
    Python小工具系列是一个使用Python实现各种各样有意思的小玩意儿的系列,包括制作个性化的二维化、词云、简单爬虫等,持续更新中,如果你感兴趣就关注一波吧!一、基本介绍接上一篇......
  • 【Python小工具】爬虫之使用OpenCV识别数字+字母验证码详解,告别收费
    Python小工具系列是一个使用Python实现各种各样有意思的小玩意儿的系列,包括制作个性化的二维化、词云、简单爬虫等,持续更新中,如果你感兴趣就关注一波吧!一、基本介绍......
  • python 报错“UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte“的解决办法
    用python读取一个txt文件withopen(file,encoding='utf-8')asf:conlines=f.readlines()运行报错:UnicodeDecodeError:‘utf-8’codeccan’tdecode......
  • python
    1    2    3    4    5    6    7    8    9    10    实战1   ......
  • python-列表list--[ ]
    创建列表List=['wade','james','bosh','haslem']可以创建空列表list添加新元素List.append('allen')方式一:向list结尾添加参数objecta=[1,2,3,4]a.......