pythonoperating-systemgtts

Unable to play & convert .txt to mp3 using GTTS


I'm trying to read a .txt file using Google's text-to-speech API. But, when I try to run it, it gives an error that I can't quite fathom. So your help will be greatly appreciated!

My Python code:

#Import the required module for text   
from gtts import gTTS 

#required to play the converted file
import os

#The file you want to convert
with open('/Users/humma/Desktop/python_projects/flowers.txt', 'r') as myFile:
    fileRead = myFile.read()

#passing file and language to the engine
myObj = gTTS(text = fileRead, lang = 'en-US', slow = False)
myObj.save('flowers.mp3')
os.system("flowers.mp3")

The error I get:

File "c:/Users/humma/Desktop/python_projects/txt-to-speech/txt-to-spch.py", line 12, in <module>
    myObj.save('flowers.mp3')
  File "C:\ProgramData\Anaconda3\lib\site-packages\gtts\tts.py", line 312, in save
    self.write_to_fp(f)
  File "C:\ProgramData\Anaconda3\lib\site-packages\gtts\tts.py", line 294, in write_to_fp
    raise gTTSError(tts=self, response=r)
gtts.tts.gTTSError: 200 (OK) from TTS API. Probable cause: Unknown

Thank you in advance for your time :)


Solution

  • It was lang = 'en-US' that caused the error. It's simply: lang = 'en'