1

Hi all I am trying to do text to speech. I have tried a few python modules I have tried the pyttsx and system modules

I would like to know if there is a way to get either of these modules to read the contents of a variable.

name = bob

import pyttsx
engine = pyttsx.init()
engine.say("my name is",name)
engine.runAndWait()

what I am trying to do above does no work it just reads the string and quits after that. Is there a way to make this module read the string and the variable ?

same thing with system.

from os import system
system("say hello my name is",name)
Ray
  • 2,472
  • 18
  • 22
Max Powers
  • 1,119
  • 4
  • 23
  • 54

2 Answers2

3

You're passing it the wrong arguments. The second argument to say is a name to give the utterance, not more stuff to say. Concatenate the strings to get what you want:

engine.say("my name is " + name, "saymyname")

Similarly, os.system only takes one argument. You need to build a single string to pass to it:

os.system("say hello my name is " + name)
user2357112
  • 260,549
  • 28
  • 431
  • 505
  • Thanks this is what I was looking for. But one other question. can i have this module read only one variable ? or can i pass in multiple variables ? – Max Powers Mar 03 '14 at 06:06
  • You can build a string with the contents of as many variables as you want. – user2357112 Mar 03 '14 at 06:20
0

If you are on a mac, try doing this:

import subprocess
name = 'bob'
subprocess.call('say %s' %(name), shell=True)

This will call say bob in a terminal window, which will say it out loud. Or, as user2357112 says, you can do the following:

import os
name = 'bob'
os.system("say my name is %s" %(name))
Community
  • 1
  • 1
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76