Gpyts is a library for Google translation and gTTS using Google Translation (API unofficially).
- Its fast, easy and has both async/sync version.
- Supports both Translation and Text to Speech.
- Option for using both api and web scrap.
- Allows multiple endpoint configurations and proxy.
Use the package manager pip to install Gpyts.
pip install Gpytsfrom gpyts.asyncGpyts import Gpyts
gpyts = Gpyts()
# Using full translation api. Returns all supported parameters listed.
translation = await gpyts.translate("hey, how are you?", to_lang = 'fr')
print("Text is : " + translation.text)
# Convert Text to Speech.
speech = await gpyts.tts("Fine, What about you?", lang = 'fr', slow = True, download = 'tts.mp3')
print("Saved as :" + speech.file)Simply import :
from gpyts.syncGpyts import Gpytsand remove await(s), thats it!
There are various configurations available (all Optional)
- tld - Custom tld's you can provide like
comorco.ukor a list ['tl', 'com']. (Random choosed iflistprovided). - proxy - Proxy to be used like
http://user:pass@ip:port. - endpoint - Custom endpoint url to be used. (Random choosed if
listprovided). - client - Custom client to be used.
- minimal - Make result simply a translated
text, defaultFalse. - labled - Custom return method to be used than default
True.
gpyts = Gpyts(tld = ['tl', 'co.uk'], proxy = 'http://user:pass@ip:port')Note :
Provide endpoint, client only if you know valid combination of values. Either use tld or endpoint, it wont work together. Just tld and proxy is required for most part even that is Optional too.
Parameters that could be passed
- text - Text to be translated.
- to_lang - Target language code.
- from_lang (Optional) - Source language code.
- i_enc (Optional) - Input encoding.
- o_enc (Optional) - Output encoding.
- web (Optional) - To use web scrap version.
Translating any text with provided language code to be converted
# Using full translation api. Returns all supported parameters listed.
translation = await gpyts.translate("hey, how are you?", to_lang = 'fr')
print("Text is : " + translation.text)You can also use web scrap mini version by passing gpyts.translate(..., web = True)
# Using web mini translation (Scrapped). Returns only translated text.
translation = await gpyts.translate("hey, how are you?", to_lang = 'fr', web = True)
print("Text is : " + translation.text)Result Translation object may contain attributes
- src - Detected source language code.
- text - Translated text.
- origin - Original text.
- translit - Transliteration if available.
- alternative - Alternative translation list.
- confidence - Confidence value of translation.
Parameters that could be passed
- text - Text to be converted.
- lang - Target language code.
- slow - Slow down the speech speed. Default False.
- i_enc (Optional) - Input encoding.
- download(Optional) - Could be either a file path or BytesIO object. Default creates
text2speech.mp3file.
Convert given text to speech
# Same folder as default text2speech.mp3
text2speech = await gpyts.tts("hey, how are you?", lang = 'ja')
print("Saved as : " + translation.file)
# Different folder (creates if not exists) as default text2speech.mp3
text2speech = await gpyts.tts("hey, how are you?", lang = 'ja', download = './saves/tts/')
print("Saved as : " + translation.file)
# Custom name
text2speech = await gpyts.tts("hey, how are you?", lang = 'ja', download = './tts.mp3')
print("Saved as : " + translation.file)You can also provide a BytesIO buffer object
# Create a ByteIO buffer
from io import BytesIO
buffer = BytesIO()
text2speech = await gpyts.tts("hey, how are you?", lang = 'ja', download = buffer)
# ... Later save it
with open('text2speech.mp3', 'wb') as file:
file.write(text2speech.file)Result TextToSpeech object may contain attributes
- lang - Language to which it was converted.
- text - Provided original text.
- file - File path where it was saved or BytesIO buffer.
Return lists of language iso code for both gts and tts.
lang = await gpyts.iso()
print('All supported transaltion iso code ', ' '.join(lang['gts']))
print('All supported text2speech iso code ', ' '.join(lang['tts']))