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:\Users\v_weijianye\Desktop\test.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:\Users\v_weijianye\Desktop\騰訊燈塔.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:\Users\Administrator\Desktop\test.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')

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM