简体   繁体   中英

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?

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

It appears this is a known bug that was already fixed seven days ago as of writing: https://github.com/pndurette/gTTS/issues/137

The solution would be to upgrade the gTTS-token package.

Try the following:

pip install google_tts

it works the same

import google_tts
a = google_tts.TTS(text = 'hello world')
a.save('test.mp3')

If you are using Windows 7, 8, or 10 along with Anaconda for Python, open the Anaconda prompt and try this:

pip install gtts --upgrade

pip install gtts-token --upgrade

This work for me.

The problem occurs since the gtts version is outdated.

Run the following command in CLI

pip install gtts --upgrade

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM