赞
踩
import os
import pyttsx3
import sys
# 创建 TTS 引擎对象
def get_engine(voice_id=None):
engine = pyttsx3.init(driverName='sapi5') # 使用sapi5驱动
if voice_id is not None:
voices = engine.getProperty('voices')
for voice in voices:
print(voice.id)
if voice.id == voice_id:
engine.setProperty('voice', voice.id)
break
return engine
#HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_ZH-CN_HUIHUI_11.0
#HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0
#HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_ZH-CN_KANGKANG_11.0
#HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_ZH-CN_YAOYAO_11.0
def main():
print("This is the main function.")
if __name__ == "__main__":
# 获取命令行参数列表
args = sys.argv
directory = args[1]
# 使用默认语音
engine = get_engine("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_ZH-CN_KANGKANG_11.0")
#engine = pyttsx3.init()
engine.setProperty('rate', 150)
# 设置输出文件格式为 WAV
engine.setProperty('outputFormat', 'wav')
# 遍历目录下的所有文件
for filename in os.listdir(directory):
# 检查文件是否以 .txt 结尾
if filename.endswith('.txt'):
# 构建文件的完整路径
filepath = os.path.join(directory, filename)
output_path = filepath[:-4]+'.wav'
# 打开 TXT 文件
#print(filepath)
with open(filepath, 'r', encoding='utf-8') as file:
# 读取文件内容
content = file.read()
# 打印文件名和内容
print(f"File: {filename}")
#print(content)
engine.save_to_file(content, output_path)
engine.runAndWait()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。