deep-translator library
deep-translator is a versatile Python library designed for translating text between various languages using a multitude of free and commercial online translation services. It is currently at version 1.11.4 and maintains an active development pace with frequent updates, bug fixes, and additions of new translator support.
Warnings
- gotcha Many commercial or advanced translators (e.g., DeepL, ChatGPT, Baidu) require an API key from the respective service provider. Without a valid key, translation requests will fail or be severely rate-limited.
- breaking As `deep-translator` relies on external, third-party translation APIs, changes in these APIs (e.g., endpoint alterations, rate limit policy changes) can cause specific translators to break without warning. This often necessitates updates to the `deep-translator` library.
- gotcha To use certain translators (e.g., DeepL, ChatGPT, document translation), you must install `deep-translator` with specific optional dependencies using `pip install "deep-translator[translator_name]"` or `"deep-translator[all]"`. Forgetting this step will lead to `ImportError` or `ModuleNotFoundError`.
- gotcha Translators have underlying `max_chars` limits imposed by the external services. While `deep-translator` fixed an internal `max_chars` bug in v1.11.2, extremely long texts might still be rejected or truncated by the API, even if the local bug is resolved.
- gotcha Document translation (introduced in v1.10.0 for PDF and DOCX) has additional requirements. PDF translation relies on OCR via Tesseract, which needs to be installed as a system dependency and available in your PATH, alongside `pytesseract` and `PyPDF2` Python packages.
Install
-
pip install deep-translator -
pip install "deep-translator[all]" -
pip install "deep-translator[chatgpt]"
Imports
- GoogleTranslator
from deep_translator import GoogleTranslator
- DeepLTranslator
from deep_translator import DeepLTranslator
- ChatGptTranslator
from deep_translator import ChatGptTranslator
- single_translation
from deep_translator import single_translation
- batch_translation
from deep_translator import batch_translation
- document_translation
from deep_translator import document_translation
Quickstart
import os
from deep_translator import GoogleTranslator, DeepLTranslator
# Example 1: Google Translator (no API key typically required for basic use)
translated_text_google = GoogleTranslator(source='auto', target='fr').translate(text='Hello world!')
print(f"Google Translation: {translated_text_google}")
# Example 2: DeepL Translator (API key usually required for production/higher usage)
# Ensure deep-translator[deepl] is installed (pip install "deep-translator[deepl]")
deep_l_api_key = os.environ.get('DEEPL_API_KEY', '') # Load from environment variable
if deep_l_api_key:
try:
# use_free_api=True for DeepL Free API tier
translator = DeepLTranslator(api_key=deep_l_api_key, source='en', target='de', use_free_api=True)
translated_text_deepl = translator.translate(text='Hello world!')
print(f"DeepL Translation: {translated_text_deepl}")
except Exception as e:
print(f"DeepL translation failed: {e}. Check API key and installation.")
else:
print("DeepL API key not found (DEEPL_API_KEY). Skipping DeepL translation.")