python中文朗读_Python:语音处理,实现在线朗读RFC文档或本地文本文件
本文主要讲解如何使用python来实现将文本转为语音,以一个小例子为例,写了一下用pyTTS来朗读本地方件或在线朗读RFC文档,当然也可以修改一下,做成在线朗读新闻之类的,另本来想实现一个读中文小说的小程序,目前没有发现对中文支持得非常好的,且是免费的语音处理引擎,只能使用TTS实现一个英文的了,就当是用来练习听力了。
1、准备:
b. 下载SpeechSDK51:下载
c. 下载SpeechSDK51 patch,支持中文和日文,本例没有使用,下载。
2、实现:
代码:#!/usr/bin/env python
# -*- coding: utf-8 -*-
#程序说明:此程序实现了通过TTS将文本内容读出来,提供了两种方式,一种是读本地文本文件,
#另一种方式为在线读RFC文档,要属入rfc编号,会将其内容逐行读出来打印到终端,其中音量
#大小,语速,朗读者可能过配置文件来设置,测试了下基本还算清楚,发现免费的TTS引擎对中文
#的支持均不是很好,所以本程序暂时没有处理对中文文件的阅读
import pyTTS
import ConfigParser
def read_local_file(tts):
'''
Function:朗读本地文件
Input:TTS对象
Output: NONE
Author: socrates
Blog:http://blog.csdn.net/dyx1024
Date:2012-02-19
'''
#输入要朗读的文本文件名
file_name = raw_input("please input a text file name (for example: rfc4960.txt)").strip()
try:
fobj = open(file_name, 'r')
except IOError, err:
print('file open error: {0}'.format(err))
return
else:
#逐行输出并朗读的文本内容
for eachLine in fobj:
print(eachLine)
tts.Speak(eachLine)
fobj.close()
def read_online_rfc(tts):
'''
Function:在线朗读RFC文档
Input:TTS对象
Output: NONE
Author: socrates
Blog:http://blog.csdn.net/dyx1024
Date:2012-02-19
'''
import urllib
#输入要朗读的RFC编号
rfc_id = raw_input("please input a rfc number (for example: 4960):")
#打开RCF文档
try:
pager = urllib.urlopen("http://tools.ietf.org/rfc/rfc%s.txt" % rfc_id)
except Exception, err:
print("open url failed, ret = %s" % err.args[0])
return
#逐行读取
while True:
if len(pager.readline()) == 0:
break
else:
strtmp = pager.readline()
print strtmp
tts.Speak(strtmp)
def Init_tts():
'''
Function:初始化TTS引擎
Input:NONE
Output: NONE
Author: socrates
Blog:http://blog.csdn.net/dyx1024
Date:2012-02-19
'''
tts_config = ConfigParser.ConfigParser()
#读取TTS相关配置文件
try:
tts_config.readfp(open('tts_config.ini'))
except ConfigParser.Error:
print 'read tts_config.ini failed.'
#创建TTS对象
tts = pyTTS.Create()
#设置语速
tts.Rate = int(tts_config.get("ttsinfo", "TTS_READ_RATE"))
#设置音量
tts.Volume = int(tts_config.get("ttsinfo", "TTS_READ_VOLUME"))
#设置朗读者
tts.SetVoiceByName(tts_config.get("ttsinfo", "TTS_READ_READER"))
return tts
def show_menu():
'''
Function:系统菜单
Input:NONE
Output: NONE
Author: socrates
Blog:http://blog.csdn.net/dyx1024
Date:2012-02-19
'''
prompt = '''
l. read local file.
2. read rfc online.
3. exit
please input your choice (1 or 2):
'''
command_name = {'1':read_local_file, '2':read_online_rfc}
while True:
while True:
try:
choice = raw_input(prompt).strip()[0]
except (EOFError, KeyboardInterrupt, IndexError):
choice = '3'
if choice not in '123':
print 'error input, try again'
else:
break
if choice == '3':
break
command_name[choice](Init_tts())
if __name__ == '__main__':
show_menu()
配置文件tts_config.ini:
[ttsinfo]
TTS_READ_RATE=-2 ;语速,默认为0,大于0表示快,小于0表示慢
TTS_READ_VOLUME=100 ;音量,0-100之间
TTS_READ_READER=MSMike ;朗读者,取值MSSam、MSMary、MSMike
测试一:
l. read local file.
2. read rfc online.
3. exit
please input your choice (1 or 2):
1
please input a text file name (for example: rfc4960.txt)english.txt
China says it condemns all acts of violence against innocent civilians
BEIJING - China's negative vote on a draft resolution on Syria at the United Nations General Assembly on Thursday was consistent with China's independent foreign policy of peace and in the best interests of the Syrian situation, officials and experts said.
China opposes armed intervention or forcing a so-called regime change in Syria, China's deputy permanent representative to the UN Wang Min said in explanatory remarks.
"We condemn all acts of violence against innocent civilians and urge the government and all political factions of Syria to immediately and fully end all acts of violence, and quickly restore stability and the normal social order," Wang said.
测试二:<>标记对中的内容仅打印,不朗读,各协议文档编号可从http://tools.ietf.org/rfc/中查询。
l. read local file.
2. read rfc online.
3. exit
please input your choice (1 or 2):
2
please input a rfc number (for example: 4960):330
IETF Tools
/* HTML element styles */
background-color: white;
padding: 0;
}
font-family: "Times New Roman", times, serif;
margin: 0;
h1{ font-size: 150%; }
h4{ margin: 0.45em 0 0 0; }
.menu form{ margin: 0; }
input.frugal,textarea.frugal {
border-left: groove 2px #ccc;
此博客上传不了音频文件,有兴趣的朋友可以自己运行一下收听。
python中文朗读_Python:语音处理,实现在线朗读RFC文档或本地文本文件相关推荐
- python语音在线编辑-Python:语音处理,实现在线朗读RFC文档或本地文本文件
本文主要讲解如何使用python来实现将文本转为语音,以一个小例子为例,写了一下用pyTTS来朗读本地方件或在线朗读RFC文档,当然也可以修改一下,做成在线朗读新闻之类的,另本来想实现一个读中文小说的 ...
- Python:语音处理,实现在线朗读RFC文档或本地文本文件
本文主要讲解如何使用python来实现将文本转为语音,以一个小例子为例,写了一下用pyTTS来朗读本地方件或在线朗读RFC文档,当然也可以修改一下,做成在线朗读新闻之类的,另本来想实现一个读中文小说的 ...
- python mysql 遍历_Python自动化办公系列六(pdf文档处理)
PDF 表示 Portable Document Format,使用 .pdf 作为文件扩展名.虽然 PDF 支持许多功能,但现在我们专注于最常做的两件事:从 PDF 读取文本内容和从已有的文档生成新 ...
- python借助jieba包对单独test和txt文档进行中文分词
python借助jieba包对单独test和txt文档进行中文分词 一.单独test分词 import jieba jieba.cut("大连圣亚在大连") *#输出:<ge ...
- python合并word表格单元格_Python实战009:读取Word文档中的表格数据及表格合并问题解决...
同事最近被安排整理资料,主要工作室将文档中的表格数据提取出来并整理层Excel表格供我们FII刀具商城进行资料维护.由于刀具的种类繁多且规格无数,所以要处理的数据量相当的庞大.人工核对整理既费时又费力 ...
- Python+Tesseract-OCR识别图片文字并保存到word文档
目录 使用Python+Tesseract-OCR识别图片文字并保存到word文档 安装Tesseract-OCR 配置Tesseract-OCR 通过CMD验证Tesseract-OCR工作 安装p ...
- RFC文档(中文翻译版本)
RFC文档官方在线阅读地址:https://tools.ietf.org/rfc/index 以下是部分中文翻译的文档连接 RFC文档目录 RFC1 主机软件 RFC2 主机软件 RFC3 文档规范 ...
- 前端页面预览word_前端实现在线预览文档
前端实现在线预览文档 发布时间:2019-06-03 17:22, 浏览次数:529 <>前端实现在线预览文档 最近项目开发中需要实现在线预览Word,Excel,PowerPoint格式 ...
- Latex在线编辑器帮助文档
Latex在线编辑器帮助文档 区域分布 选择区域 文本区域 下载区域 AI区域 识别结果 公式检测 公式识别 Attention Latex在线编辑器 区域分布 选择区域 用于帮助用户记住各种Late ...
最新文章
- 把握每天的第一个钟头
- 实践出真知:全网最强秒杀系统架构解密!!
- java url 生成图片_JAVA 通过URL生成水印图
- 关联规则(Association Rules)笔记
- Oracle-表分析和索引分析解读
- 60万元奖金 “人脸攻防战”,怎么打?丨独家公开课实录(附baseline)
- 视觉SLAM十四讲(3):三维空间刚体运动
- django 集成个推_Django动态添加定时任务之djangocelery的使用
- Sql Server中判断日志是否为一个星期
- 1012. 数字分类 (20)-PAT乙级真题
- HTML学习---HTML状态码
- 在ubuntu下安装Dynamips
- crosstab交叉表_数据透视之交叉表 crosstab()
- Docker 下载安装 Docker 配置镜像加速器
- 最重要Python面试题,逻辑题,Python与数学之美
- JavaScript数据数据类型判断
- 网商基本法:网上贸易,用网商币
- 物联卡是什么?有什么用?可以用在什么设备上?
- 微型计算机中 存储器容量最大的部件是,2012年计算机一级考试B练习题及答案
- 包装行业ERP解决方案