DeepL Python API - ckertam/SWE573_SPRING_2023 GitHub Wiki

DeepL Python API

The DeepL is a best-in class language translation tool that can be used via its Website https://www.deepl.com. Also, the DeepL language translation is usable in Python via API calls. So it can be used in all products that are created with Python language. The API has mostly all possible Web features in it.

Authentication Key

You need an API authentication key to use DeepL API in Python. You can create an account from here then you can get your API Key via your account. And first 500.000 characters are free for every month.

Installation & Authentication Code

You can install the API library via using pip. pip install --upgrade deepl Then you need to enter your authentication key and construct a Translator.

import deepl

auth_key = "......" #Enter your auth key

translator = deepl.Translator(auth_key)

Basic Translation Call Example for Texts

After constructing a Translator

result = translator.translate_text("Hello, world!", target_lang="TR") print(result.text) # "Merhaba, Dünya!"

You can also translate from multiple languages and select specific English languages like British English below.

result = translator.translate_text(["お元気ですか?", "Nasılsın?"], target_lang="EN-GB")

And you can see the results below.

print(result[0].text) # "How are you?"

print(result[0].detected_source_lang) # "JA" the language code for Japanese

print(result[1].text) # "How are you?"

print(result[1].detected_source_lang) # "TR" the language code for Turkish

Available options for Text Translation

  1. source_lang
  2. target_lang
  3. split_sentences (Possible values: "on", "off", "nonewlines"
  4. preserve_formatting
  5. formality (Possible values: "less", "more")
  6. glossary
  7. tag_handling (Possible values: "xml", "html") Below options can only usable if you specify tag_handling = "xml"
  8. outline_detection
  9. splitting_tags
  10. non_splitting_tags
  11. ignore_tags

To see more detailed info about options you can go to API Documentation

To see more detailed info about text translation Text Translation

Basic Translation Call Example for Documents

To translate documents you need to specify the input and output paths for the document. And also you need to construct Translator.

input_path = "/path/to/Instruction Manual.docx"

output_path = "/path/to/Bedienungsanleitung.docx"

There are 2 possible way to do that. First one is using below code.

Using translate_document_from_filepath()

translator.translate_document_from_filepath( input_path, output_path, target_lang="DE", formality="more" )

Using translate_document() with file IO objects

with open(input_path, "rb") as in_file, open(output_path, "wb") as out_file:

translator.translate_document( in_file, out_file, target_lang="DE", formality="more")

You can find more detailed option via Document Translation

Account Usage Check

As mentioned above, there is a free limit for every month. So one may want to check usage for the API.

Via using get_usage() function you can get character usage and document usage for that mount and account.

usage = translator.get_usage()

if usage.character.valid:

print(f"Character usage: {usage.character.count} of {usage.character.limit}")

if usage.document.valid:

print(f"Document usage: {usage.document.count} of {usage.document.limit}")