2

I've been trying to build an application which takes input as text and gives output as speech.

I referred this site to get to know about Text-To-Speech modules in python: https://pythonprogramminglanguage.com/text-to-speech/

when i ran the program it did the job perfectly but i couldn't use other functions like pause or resume. So i tried to create a new thread for the speech function so that i can alter it speech whenever i want to.

Here is the program:

import threading
import win32com.client as wincl
speak = wincl.Dispatch("SAPI.SpVoice")
t=threading.Event()

def s():
    global t
    t.set()
    data="""This is a story of two tribal Armenian boys who belonged to the 
         Garoghlanian tribe. """
    s=speak.Speak(data)

t1=threading.Thread(target=s)
t1.start

However i am trying to implement the program in GUI using tkinter. I want the application to read the text when the user is clicking the button. Since tkinter's button takes command as a function, i made a function for the initialization and starting of the new thread but it is producing an error which i could not interpret and find a solution.

Here is the program thats making error:

import threading
import win32com.client as wincl
speak = wincl.Dispatch("SAPI.SpVoice")
t=threading.Event()

def s():
    global t
    t.set()
    data="""This is a story of two tribal Armenian boys who belonged to the 
         Garoghlanian tribe. """
    s=speak.Speak(data)

def strt():
    t1=threading.Thread(target=s)
    t1.start()

Here is the error:

Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Application\Python\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Application\Python\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\absan\Desktop\Python\Project-SpeakIt\SI-1.py", line 32, in 
speakITheart
s=speak.Speak(data)
File "C:\Users\absan\AppData\Local\Temp\gen_py\3.6\C866CA3A-32F7-11D2-9602- 
00C04F8EE628x0x5x4.py", line 2980, in Speak
, 0)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, 
None, 0, -2147221008), None)

EDIT: Guys i somehow found a way to fix it when i was writing this post. I just added these lines to the program

import pyttsx3
engine = pyttsx3.init()

i really don't know how or why it fixed the error but it works!! So this post might be helpful for someone who is facing the same problem.

Cheers!!

Santhosh
  • 81
  • 2
  • 11
  • I am having the same problem. Did you add those to the beginning of the script? – Moondra Apr 17 '19 at 22:48
  • yeah just after importing win32com.client import pyttsx3 and init it. The most probable reason for the fix is pyttsx3 is a wrapper for win32com.client i suppose. – Santhosh Apr 18 '19 at 09:47
  • I actually asked a separate question and got a different answer that works as well: https://stackoverflow.com/questions/55737030/using-win32client-sapi-spvoice-with-multi-threading-leads-to-pywintypes-com-erro/55737116#55737116 – Moondra Apr 18 '19 at 17:59
  • so yeah basically that's more efficient because in my program i am importing the whole module and initializing it which also has the coinitialize statement... – Santhosh Apr 20 '19 at 17:30

1 Answers1

1

I like the solution from the comments. Just include in non-main thread this line to be able to use COM in it (right before the Speak call)

pythoncom.CoInitialize()
self.engine.Speak(self.msg)
Wotori Movako
  • 147
  • 1
  • 7