當前位置:編程學習大全網 - 編程語言 - 實現python文本寫入word

實現python文本寫入word

準備

我測試使用的Python版本為2.7.10,如果妳的版本是Python3.5的話,這裏就不太適合了。

使用Speech API

原理

我們的想法是借助微軟的語音接口,所以我們肯定是要進行調用 相關的接口。所以我們需要安裝pywin32來幫助我們完成這壹個底層的交互。

示例代碼

import win32com.clientspeaker = win32com.client.Dispatch("SAPI.SpVoice")speaker.Speak("Hello, it works!")

小總結

是的,調用接口來實現語音功能就是這麽簡單,但是我們不得不來聊壹聊這種方式的缺點。

對中文支持的不夠好,僅僅是這壹點,估計在中國沒幾個用它的了。

還有就是語速不能很好的控制

pyttsx方式

原理

pyttsx 是Python的壹個關於文字轉語音方面的很不錯的庫。我們還可以借助pyttsx來實現在線朗讀rfc文件或者本地文件等等,最為關鍵的是,它對中文支持的還是不錯的。

示例代碼

# coding:utf-8import sysreload(sys)sys.setdefaultencoding('utf8')# __author__ = '郭 璞'# __date__ = '2016/8/6'# __Desc__ = 文字轉語音輸出import pyttsxengine = pyttsx.init()engine.say('hello world')engine.say('妳好,郭璞')engine.runAndWait()# 朗讀壹次engine.endLoop()

小總結

使用pyttsx,我們可以借助其強大的API來實現我們基本的業務需求。很酷吧。

pyttsx深入研究

做完上面的小實驗,妳肯定會覺得怎麽這麽不過癮呢?

別擔心,下面我們就壹起走進pyttsx的世界,深入的研究壹下其工作原理吧。

語音引擎工廠

類似於設計模式中的“工廠模式”,pyttsx通過初始化來獲取語音引擎。當我們第壹次調用init操作的時候,會返回壹個pyttsx的engine對象,再次調用的時候,如果存在engine對象實例,就會使用現有的,否則再重新創建壹個。

pyttsx.init([driverName : string, debug : bool]) → pyttsx.Engine

從方法聲明上來看,第壹個參數指定的是語音驅動的名稱,這個在底層適合操作系統密切相關的。如下:

1.drivename:由pyttsx.driver模塊根據操作系統類型來調用,默認使用當前操作系統可以使用的最好的驅動

sapi5 - SAPI5 on Windows

nsss - NSSpeechSynthesizer on Mac OS X

espeak - eSpeak on every other platform

2.debug: 這第二個參數是指定要不要以調試狀態輸出,建議開發階段設置為True

引擎接口

要想很好的運用壹個庫,不了解其API是不行的。下面來看看pyttsx。engine.Engine的引擎API。

方法簽名 參數列表 返回值 簡單釋義

connect(topic : string, cb : callable) topic:要描述的事件名稱;cb:回調函數 → dict 在給定的topic上添加回調通知

disconnect(token : dict) token:回調失聯的返回標記 Void 結束連接

endLoop() None → None 簡單來說就是結束事件循環

getProperty(name : string) name有這些枚舉值“rate, vioce,vioces,volumn → object 獲取當前引擎實例的屬性值

setProperty(name : string) name有這些枚舉值“rate, vioce,vioces,volumn → object 設置當前引擎實例的屬性值

say(text : unicode, name : string) text:要進行朗讀的文本數據; name: 關聯發音人,壹般用不到 → None 預設要朗讀的文本數據,這也是“萬事俱備,只欠東風”中的“萬事俱備”

runAndWait() None → None 這個方法就是“東風”了。當事件隊列中事件全部清空的時候返回

startLoop([useDriverLoop : bool]) useDriverLoop:是否啟用驅動循環 → None 開啟事件隊列

元數據音調

在pyttsx.voice.Voice中,處理合成器的發音。

age

發音人的年齡,默認為None

gender

以字符串為類型的發音人性別: male, female, or neutral.默認為None

id

關於Voice的字符串確認信息. 通過 pyttsx.engine.Engine.setPropertyValue()來設置活動發音簽名. 這個屬性總是被定義。

languages

發音支持的語言列表,如果沒有,則為壹個空的列表。

name

發音人名稱,默認為None.

更多測試

朗讀文本

import pyttsxengine = pyttsx.init()engine.say('Sally sells seashells by the seashore.')engine.say('The quick brown fox jumped over the lazy dog.')engine.runAndWait()

事件監聽

import pyttsxdef onStart(name): print 'starting', namedef onWord(name, location, length): print 'word', name, location, lengthdef onEnd(name, completed): print 'finishing', name, completedengine = pyttsx.init()engine.say('The quick brown fox jumped over the lazy dog.')engine.runAndWait()

打斷發音

import pyttsxdef onWord(name, location, length): print 'word', name, location, length if location > 10: engine.stop()engine = pyttsx.init()engine.say('The quick brown fox jumped over the lazy dog.')engine.runAndWait()

更換發音人聲音

engine = pyttsx.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 = pyttsx.init()rate = engine.getProperty('rate')engine.setProperty('rate', rate+50)engine.say('The quick brown fox jumped over the lazy dog.')engine.runAndWait()

音量控制

engine = pyttsx.init()volume = engine.getProperty('volume')engine.setProperty('volume', volume-0.25)engine.say('The quick brown fox jumped over the lazy dog.')engine.runAndWait()

執行壹個事件驅動循環

engine = pyttsx.init()def onStart(name): print 'starting', namedef onWord(name, location, length): print 'word', name, location, lengthdef onEnd(name, completed): print 'finishing', name, completed if name == 'fox': engine.say('What a lazy dog!', 'dog') elif name == 'dog': engine.endLoop()engine = pyttsx.init()engine.say('The quick brown fox jumped over the lazy dog.', 'fox')engine.startLoop()

使用壹個外部的驅動循環

engine = pyttsx.init()engine.say('The quick brown fox jumped over the lazy dog.', 'fox')engin(www.alOnely.Com.Cn)e.startLoop(False)# engine.iterate() must be called inside externalLoop()externalLoop()engine.endLoop()

總結

以上就是Python如何實現文本轉語音的全部內容,看完了上面的講述,是不是感覺Python實現文本轉語音還是蠻簡單的?那麽,大家快來嘗試嘗試吧。希望本文對大家學習Python有所幫助。

  • 上一篇:Java編程名稱
  • 下一篇:2023年郴州市海納中等職業學校招生簡章收費標準地址師資怎麽樣
  • copyright 2024編程學習大全網