Python library for the DeepL API.
Project description
DeepL Python Library
The DeepL API is a language translation API that allows other computer programs to send texts and documents to DeepL's servers and receive high-quality translations. This opens a whole universe of opportunities for developers: any translation product you can imagine can now be built on top of DeepL's best-in-class translation technology.
The DeepL Python library offers a convenient way for applications written in Python to interact with the DeepL API. All functions of the DeepL API are supported.
Getting an authentication key
To use the DeepL Python Library, you'll need an API authentication key. To get a key, please create an account here. You can translate up to 500,000 characters/month for free.
Installation
The library can be installed from PyPI using pip:
pip install --upgrade deepl
If you need to modify this source code, install the dependencies using poetry:
poetry install
Requirements
The library is tested with Python versions 3.6 to 3.9.
The requests
module is used to perform HTTP requests; the minimum is version 2.18.
Usage
import deepl
# Create a Translator object providing your DeepL API authentication key
translator = deepl.Translator("YOUR_AUTH_KEY")
# Translate text into a target language, in this case, French
result = translator.translate_text("Hello, world!", target_lang="FR")
print(result) # "Bonjour, le monde !"
# Note: printing or converting the result to a string uses the output text
# Translate multiple texts into British English
result = translator.translate_text(["お元気ですか?", "¿Cómo estás?"], target_lang="EN-GB")
print(result[0].text) # "How are you?"
print(result[0].detected_source_lang) # "JA"
print(result[1].text) # "How are you?"
print(result[1].detected_source_lang) # "ES"
# Translating documents
translator.translate_document_from_filepath(
"Instruction Manual.docx",
"Bedienungsanleitlung.docx",
target_lang="DE",
formality="more"
)
# Check account usage
usage = translator.get_usage()
if usage.character.limit_exceeded:
print("Character limit exceeded.")
# Source and target languages
for language in translator.get_source_languages():
print(f"{language.code} ({language.name})") # Example: "DE (German)"
num_languages = sum([language.supports_formality
for language in translator.get_target_languages()])
print(f"{num_languages} target languages support formality parameter")
Logging
Logging can be enabled to see the HTTP-requests sent and responses received by the library. Enable and control logging using Python's logging module, for example:
import logging
logging.basicConfig()
logging.getLogger('deepl').setLevel(logging.DEBUG)
Exceptions
All module functions may raise deepl.DeepLException
or one of its subclasses.
If invalid arguments are provided, they may raise the standard exceptions ValueError
and TypeError
.
Command Line Interface
The library can be run on the command line supporting all API functions. Use the --help
option for
usage information:
python3 -m deepl --help
The CLI requires your DeepL authentication key specified either as the DEEPL_AUTH_KEY
environment variable, or using
the --auth-key
option, for example:
python3 -m deepl --auth-key=YOUR_AUTH_KEY usage
Note that the --auth-key
argument must appear before the command argument. The recognized commands are:
Command | Description |
---|---|
text | translate text(s) |
document | translate document(s) |
usage | print usage information for the current billing period |
languages | print available languages |
For example, to translate text:
python3 -m deepl --auth-key=YOUR_AUTH_KEY text --to=DE "Text to be translated."
Wrap text arguments in quotes to prevent the shell from splitting sentences into words.
Development
The test suite depends on deepl-mock. Run it in another terminal while executing the tests, using port 3000. Set the mock-server listening port using the environment variable DEEPL_MOCK_SERVER_PORT
.
Execute the tests using tox
.
Issues
If you experience problems using the library, or would like to request a new feature, please create an issue.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file deepl-0.4.1.tar.gz
.
File metadata
- Download URL: deepl-0.4.1.tar.gz
- Upload date:
- Size: 18.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7bf26bf03cabdb97aa86e013018cada1b4446fdc9ccc9c6e9c7c1386de3ffb7d |
|
MD5 | eecdb07a6336b7e6e860b319fdb86993 |
|
BLAKE2b-256 | 0896c98fdf7f5c1dccd3fc0f4e18546603ff1b4c6bfabbbeb7f05bc5608f25a2 |
File details
Details for the file deepl-0.4.1-py3-none-any.whl
.
File metadata
- Download URL: deepl-0.4.1-py3-none-any.whl
- Upload date:
- Size: 18.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 156239fac50d8f391b01dc388737d59904ddf660c841a82a57826ab235d7a4cc |
|
MD5 | 0afbdfdc0c845634894c9d4bf029bc58 |
|
BLAKE2b-256 | 7c0107d2c29f3d0353c7b221131d113ba5d8cdfa60b7ca6ecedc72bf6ed21128 |