If you get an error using gtts module

Wanna transform text into an mp3? Thanks google for gtts

error in gtts
error in gtts

The gtts module, is the api that python can use to access to the google text to speech engine, so that you can transform text into mp3 easily in any language. It can happen, if you do not update gtts that you find an error like this:

AttributeError: ‘NoneType’ object has no attribute ‘group’

To avoid it install the upgrade of gtts, but also of the gtts-token module:

pip install gtts –upgrade

pip install gtts-token –upgrade

This will avoid the error

Here is some code to get a text and transform it into an mp3

# remember to update gtts and gtts-token

import os
from gtts import gTTS


def txt_to_mp3(text):
    with open(text, 'r', encoding='utf-8') as filetospeak:
        s = gTTS(filetospeak.read(), "it")
        s.save(text[:-4] + ".mp3")

    os.system(text[:-4] + ".mp3")


txt_to_mp3("finanziamenti.txt")

You must have the .txt file into the same directory where you exectute the code.

Links

The page where the error has been discussed on github

The documentation of gtts

Utilities

 

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.