问题
When executing the below code
from gtts import gTTS
tts = gTTS('hello')
tts.save('hello.mp3')
I am getting "AttributeError: 'NoneType' object has no attribute 'group'".
Traceback (most recent call last):
File "C:\Users\HP\Desktop\Desktop\programming\Python_code\New Text Document - Copy (8) - Copy.py", line 3, in <module>
tts.save('hello.mp3')
File "C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site-packages\gtts\tts.py", line 247, in save
self.write_to_fp(f)
File "C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site-packages\gtts\tts.py", line 187, in write_to_fp
part_tk = self.token.calculate_token(part)
File "C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site-packages\gtts_token\gtts_token.py", line 28, in calculate_token
seed = self._get_token_key()
File "C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\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'
Is it possible to use gTTS in Python?
回答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
My issue was fixed after I did a fresh install of both gtts and gtts-token.
The relevant github issue in gtts can be found here. Amazing this issue was fixed in 12 hours. Praise the open source gods! :D
回答2:
I checked the code on line 64 of gtts_token.py, and I tried to print(a)
, but it shows "NoneType".
I repaired that bug in line 64:
# bug
# a = re.search(“a\\\\x3d(-?\d+);”, tkk_expr).group(1)
# b = re.search(“b\\\\x3d(-?\d+);”, tkk_expr).group(1)
# debug
tkk = tkk_expr.replace(“\’”, “”).split(“;”)[0].split(“=”)[1]
a = tkk.split(“.”)[0]
b = tkk.split(“.”)[1]
回答3:
Simply uninstall both gtts-token and gtts and install them again.
pip uninstall gtts-token
pip uninstall gtts
pip install gtts-token
pip install gtts
来源:https://stackoverflow.com/questions/52439591/how-to-use-gtts-in-python