3

windows 10-64bit

I'm trying to use some text-to-speech tool to read text from lines of .txt document, something like this:

so with pyttsx:

import pyttsx
engine = pyttsx.init()
engine.say('my voice')
engine.runAndWait() 

I got this error:

Traceback (most recent call last):
  File "...", line 1, in <module>
    import pyttsx
  File "/.../pyttsx/__init__.py", line 18, in <module>
    from engine import Engine
ImportError: No module named 'engine'

now gTTS, available as gtts_token, so how to use it? because this way module is unrecognizable:

import gtts
blabla = ("my voice")
tts = gtts.gTTS(text=blabla, lang='en')
tts.save("C:/rec.mp3")

or:

from gtts import gTTS
blabla = ("my voice")
tts = gTTS(text=blabla, lang='en')
tts.save("C:/rec.mp3")

error:

 import gtts
ImportError: No module named 'gtts'

also I'm want try to use espeak but not sure how to install it, is it available with pip install or I have to install it some other way to try it:

import subprocess
text = '"my voice"'
subprocess.call('espeak '+text, shell=True)

or:

import os
os.system("espeak 'my voice'")

so I'm trying to find some solution, but everything I tried is not working here...

  • see [pip: dealing with multiple Python versions?](http://stackoverflow.com/questions/2812520/pip-dealing-with-multiple-python-versions) and make sure you installed `gtts` to the same version of python you are using. – Tadhg McDonald-Jensen Mar 31 '16 at 04:02
  • pyttsx does not seem to be designed for python 3, see [pyttsx: No module named 'engine'](http://stackoverflow.com/questions/29615235/pyttsx-no-module-named-engine) but the answer there did not make it work for me. – Tadhg McDonald-Jensen Mar 31 '16 at 04:13
  • to install `espeak` just go to the dowload section of the [sourceforge site](http://espeak.sourceforge.net) and once it is installed the code you provided should work but I'm not certain since I'm using a mac. good luck to you though! – Tadhg McDonald-Jensen Mar 31 '16 at 04:16
  • 'hi thanks, with pyttsx nothing helps. with your help gtts works, takes text from txt file `blabla = (line[0])` but first creates mp3 file, then if I want listen, I must call this mp3, so it is good but in my case I want avoid any audio files, need just read from text file. is it possible somehow use google voice to read my text file? I have not tried yet espeak... well if I can use only gTTS now have problem with mp3 file which I described in detail here because now it is a another question http://stackoverflow.com/questions/36347786/how-to-play-mp3-from-gtts –  Apr 01 '16 at 04:54

3 Answers3

2

I am using windows 10 and Python 2.7.

For pyttsx:

Below code is working fine for me. I did get ImportError: No module named win32api error for which I had to install win32api from here

After that I could play "my voice". Although the quality and fidelity of spoken sound was very low. gtts is much better in that regards.

import pyttsx
engine = pyttsx.init()
engine.say('my voice')
engine.runAndWait() 

For the error you are getting, Can you look into your python folder and see if engine.py file is present?

For e.g. in my case, I've pyttsx modules installed at following location C:\Python27\Lib\site-packages\pyttsx and here is a list of files,

 Name
 ----
 drivers
 driver.py
 driver.pyc
 engine.py
 engine.pyc
 voice.py
 voice.pyc
 __init__.py
 __init__.pyc

Since import of engine is failing, I am wondering if you have engine.py file in the correct folder or present at all.

For gtts:

I tried playing sound with winsound, but it did not work. Using pydub I was able to play the audio file. But, since your requirement is not to use a file, this may be a moot point.

import gtts
import winsound
from pydub import AudioSegment
from pydub.playback import play

blabla = ("my voice")
tts = gtts.gTTS(text=blabla, lang='en')
tts.save("rec.mp3")
print "Playing sound .."
#winsound.PlaySound("rec.wav", winsound.SND_FILENAME)
song = AudioSegment.from_mp3("rec.mp3")
play(song)

Hope this helps.

Anil_M
  • 8,692
  • 2
  • 32
  • 59
  • I responded to that question as well, check my answer and see if that helps. – Anil_M Apr 05 '16 at 19:27
  • hello gTTS reported here https://github.com/pndurette/gTTS/issues/32 and actual error: http://stackoverflow.com/questions/37176555/gtts-httperror-403-client-error-forbidden-for-url and with pyttsx I have no idea how to figure out, because seems like it is installed, but I can't find any directory by path you shown in other words inside the site-packages. –  May 13 '16 at 03:29
  • I've install it with **pip install pyttsx** and install process was successful, and with https://sourceforge.net/projects/pywin32/files/pywin32/ same I've tried different but got same message "python version 3.5 required, which one is not found in directory". also I can't figure out with espeak, same problem here http://stackoverflow.com/questions/37127139/how-to-install-espeak-for-python-3-5-on-windows-10 have no idea what to do next –  May 13 '16 at 03:30
  • I've `windows ` with `python 2.7` running `pyttsx` successfully. I downloaded and installed from `https://github.com/parente/pyttsx` . Try that see if it works. [ may want to uninstall previous versions] – Anil_M May 13 '16 at 15:15
  • hello, I use python 3.5 on windows 10 –  May 14 '16 at 21:12
2

for python3 use

pyttsx3

Its a new library compatible with both python3 and python2. Unlike gTTS it doesn't need internet connection and there is no delay in the sound produced.

Install:

pip install pyttsx3

Usage :

import pyttsx3
engine = pyttsx3.init()
engine.say("Hi this is working ");
engine.setProperty('volume',0.9)
engine.runAndWait()
Community
  • 1
  • 1
Natesh bhat
  • 7,780
  • 5
  • 52
  • 90
0

I'm using python2.7 on Ubuntu.

Try to replace "from engine import Engine" with "from .engine import Engine" in the engine module.It work for me!

Alejandro
  • 11
  • 4