Search code examples
pythonlinuxspeech-recognitionpyttsxwit.ai

fetch data from text file in pyttsx python


Hello respected persons,

I am trying to fetch data from a external text file with pyttsx package of python language.

I will save text in txt file and how pyttsx can read that text from txt file and can speak?

I am trying to do like this,

import pyttsx
engine = pyttsx.init()
engine.say("open file and read data from /users/exe/voice.txt")
engine.runAndWait()
engine.runAndWait()

how pyttsx can fetch and speak data of a text file ?

actually my real motto is using wit.ai with pyttsx how can I do it ?


Solution

  • I found the answer myself: I had to add two lines of code to open the file and read its lines into an array:

    import pyttsx
    engine = pyttsx.init()
    with open('/Users/exepaul/Desktop/a.txt') as f:
        lines = f.readlines()
    engine.say(lines)
    engine.runAndWait()
    engine.runAndWait()