问题
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?
回答1:
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
回答2:
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.
回答3:
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')
回答4:
The problem occurs since the gtts version is outdated.
Run the following command in CLI
pip install gtts --upgrade
回答5:
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.
来源:https://stackoverflow.com/questions/52555027/python-gtts-error-attributeerror-nonetype-object-has-no-attribute-group