Skip to main content

Python library for the DeepL API.

Project description

DeepL Python Library

PyPI version Supported Python versions License: MIT

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. We intend to support all API functions with the library, though support for new features may be added to the library after they’re added to the API.

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.

After you have created an account, you can find your API authentication key on your DeepL Pro Account.

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"

# Translate a formal document from English to German 
translator.translate_document_from_filepath(
    "Instruction Manual.docx",
    "Bedienungsanleitlung.docx",
    target_lang="DE",
    formality="more"
)

# Glossaries allow you to customize your translations
glossary_en_to_de = translator.create_glossary(
    "My glossary",
    source_lang="EN",
    target_lang="DE",
    entries={"artist": "Maler", "prize": "Gewinn"},
)

with_glossary = translator.translate_text_with_glossary(
    "The artist was awarded a prize.", glossary_en_to_de
)
print(with_glossary)  # "Der Maler wurde mit einem Gewinn ausgezeichnet."

without_glossary = translator.translate_text(
    "The artist was awarded a prize.", target_lang="DE"
)
print(without_glossary)  # "Der Künstler wurde mit einem Preis ausgezeichnet."


# Check account usage
usage = translator.get_usage()
if usage.character.limit_exceeded:
    print("Character limit exceeded.")
else:
    print(f"Character usage: {usage.character}")

# Source and target languages
print("Source languages:")
for language in translator.get_source_languages():
    print(f"{language.code} ({language.name})")  # Example: "DE (German)"

print("Target languages:")
for language in translator.get_target_languages():
    if language.supports_formality:
        print(f"{language.code} ({language.name}) supports formality")
    else:
        print(f"{language.code} ({language.name})")

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
glossary create, list, and remove glossaries

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

deepl-1.1.1.tar.gz (26.8 kB view details)

Uploaded Source

Built Distribution

deepl-1.1.1-py3-none-any.whl (27.5 kB view details)

Uploaded Python 3

File details

Details for the file deepl-1.1.1.tar.gz.

File metadata

  • Download URL: deepl-1.1.1.tar.gz
  • Upload date:
  • Size: 26.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.4

File hashes

Hashes for deepl-1.1.1.tar.gz
Algorithm Hash digest
SHA256 78a3b8c62bd32c08236684e177ba236acef3d8e806a12b140ec4ef72e87c55a9
MD5 6faa85d8d49d0658bb6672a256c4cb8f
BLAKE2b-256 5cef56a129b42854dc55ad1de9e30487ac146d7e1048ebf3d03fe7df28ee4935

See more details on using hashes here.

File details

Details for the file deepl-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: deepl-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 27.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.4

File hashes

Hashes for deepl-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e2e05cfe882c5e4320dad568f3c519d4e5ccbd381d3463f3601ee4052ad6b24c
MD5 c617f22b3a0b91631b1ec2600b496e43
BLAKE2b-256 de33fdfb616406abc4eba851d3ea7d2ff1a332d504ba066ade288f213da647c4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page