Search code examples
pythontext-to-speechgoogle-text-to-speech

Python GTTS Error : AttributeError: 'NoneType' object has no attribute 'group'


I have a project where I was doing text to speech conversion. My audio file is being stored as a mp3.

But now, when I check the Gtts api is throwing error. I tried searching but couldn't find a workable solution for the bug.

My code is as follows:

def synth(sent,language='en',slow = False):
    """
    Synthesize text into audio
    """  
    os.system('clear')
    print("Speaker Output:" + sent)
    gt_ob = gTTS(text=sent, lang=language, slow=slow)
    file_name = hashlib.md5(sent.encode('utf-8')).hexdigest()
    print("File Name " + file_name)
    gt_ob.save("media/audio.mp3")
    print("Till here")
    os.system("ffmpeg -nostats -loglevel 0 -y -i media/audio.mp3 -ar 16000 media/"+ file_name + ".wav")

if __name__ == "__main__":
    synth("good morning","en")

And the Error message that I am getting is :

File "file_name.py", line 30, in <module>
synth("good morning","en")
  File "file_name.py", line 25, in synth
    gt_ob.save("media/audio.mp3")
  File "/home/arqam/anaconda3/lib/python3.6/site-packages/gtts/tts.py", line 247, in save
    self.write_to_fp(f)
  File "/home/arqam/anaconda3/lib/python3.6/site-packages/gtts/tts.py", line 187, in write_to_fp
    part_tk = self.token.calculate_token(part)
  File "/home/arqam/anaconda3/lib/python3.6/site-packages/gtts_token/gtts_token.py", line 28, in calculate_token
    seed = self._get_token_key()
  File "/home/arqam/anaconda3/lib/python3.6/site-packages/gtts_token/gtts_token.py", line 62, in _get_token_key
    a = re.search("a\\\\x3d(-?\d+);", tkk_expr).group(1)
AttributeError: 'NoneType' object has no attribute 'group'

So how can we resolve this bug that has popped up?


Solution

  • There is an official fix now. It had to do with an upstream dependency of gtts, gtts-token. It has been fixed in gtts-token==1.1.2

    The issue was fixed after I did a fresh install of both gtts and gtts-token. Now it is working. Thanks to open source gods and @carrey-cole

    Link: https://github.com/pndurette/gTTS/issues/137