Cara menggunakan deepl translator python

Penjabaran dari 9apps DeepL Translate

DeepL Translate is a free app that delivers fast, high-quality, and affordable language translation. DeepL translation is a form of language translation automation that uses deep learning models to deliver more accurate translation.
DeepL Translate allows you to easily translate large volumes of text efficiently into many languages such as; English, German, French, Spanish, Portuguese, Italian, Dutch, Polish, Russian, Japanese, and Chinese.
The DeepL translation engines are always learning from new and expanded datasets to produce more accurate translations for a wide range of use cases.
- Beautiful and easy to use interface
- Instantly translate words and sentences
- Easily copy and paste sentences
- Provide instant online Translation
- Very useful app for easy and fast translations, which can be used like a dictionary
- Its completely free, faster and very easy solution for translations.
- Simple User Interface(Easy to use for Translation)
- Translations in French, Spanish, German and more
- You can also share your translated text, sentece with your friends through the app.
Language Translator
DeepL translator help to translate any languages to other and overcome the languages barrier. We provide many languages translations Deepl Translator users.
Text Translator
Text Translator is word input method through which you can write any text or sentence and press the translate button and it translate your words into other language in which you want to translate. It’s also have text copy paste option you can easily copy your translated text and share with others.
You can use the DeepL Translate app to translate from English to any languages of the modern world that you want, for example
translate English to German or Spanish, and vice versa. Make full use of it every day for any
purposes: business, travel, and even education.
DISCLAIMER: We are not directly affiliated with DeepL.com, this Android app provides enhanced user experience to help DeepL android users.

deep-translator provides out of the box usage of proxies. Just define your proxies config as a dictionary and pass it to the corresponding translator. Below is an example using the GoogleTranslator, but this feature can be used with all supported translators.

lang = batch_detection(['bonjour la vie', 'hello world'], api_key='your_api_key') print(lang) # output: [fr, en]9

Deep-translator supports a series of command line arguments for quick and simple access to the translators directly in your console.

DeepL Translate adalah aplikasi terjemahan andalan untuk menerjemahkan teks, ucapan, gambar, dan file dalam 29 bahasa. Setiap hari, jutaan orang menggunakannya untuk berkomunikasi dan mengatasi kendala bahasa. Mulai gunakan sekarang juga untuk mendapatkan terjemahan gratis, cepat, dan sangat akurat.

* Terjemahkan teks dengan cepat antara 29 bahasa dengan mengetik: Belanda, Bulgaria, Ceko, Denmark, Estonia, Finlandia, Hongaria, Indonesia, Inggris (Amerika), Inggris (Inggris), Italia, Jepang, Jerman, Latvia, Lituania, Mandarin (sederhana), Polandia, Portugis, Portugis (Brasil), Prancis, Romania, Rusia, Slovenia, Slowakia, Spanyol, Swedia, Turki, Yunani, Ukraina
* Terjemahkan ucapan ke teks: Gunakan mikrofon untuk menerjemahkan teks yang diucapkan dalam sebagian besar bahasa
* Teks ke ucapan: Dengarkan teks terjemahan dalam sebagian besar bahasa
* Mode gelap: Lindungi mata Anda dari cahaya terang dan hemat daya tahan baterai selagi menerjemahkan
* Deteksi cepat: Terjemahan dimulai setelah memasukkan beberapa huruf saja
* Kualitas tinggi: Terjemahan DeepL mengungguli produk pesaing dengan faktor sebesar 3:1
* Tanpa biaya: Aplikasi DeepL Translate sepenuhnya gratis
* Terjemahan alternatif: Lihat opsi alternatif untuk frasa singkat
* Terjemahan kamera instan: Terjemahkan teks dalam gambar secara instan dengan mengarahkan kamera (23 bahasa)
* Terjemahan foto: Impor foto untuk terjemahan berkualitas lebih tinggi (23 bahasa)
* Terjemahan tersimpan: Ketuk ikon bookmark dan simpan kata serta frasa sebagai referensi mendatang atau untuk membangun kosakata Anda sendiri (semua bahasa)
* DeepL Pro: Masuk ke akun DeepL Pro Anda

Syarat & Ketentuan: [//www.deepl.com/app-terms]

Kebijakan Privasi: [//www.deepl.com/privacy.html]

Dukungan DeepL: [//www.deepl.com/support]

Pemberitahuan Izin:

DeepL Translate dapat meminta izin untuk mengakses fitur-fitur berikut:
- Akses mikrofon: Pengenalan ucapan
- Akses kamera: Terjemahan kamera instan

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, . With a DeepL API Free account 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

On Ubuntu 22.04 an error might occur: import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"0. Use the workaround import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"1 as explained in this bug report.

Requirements

The library is tested with Python versions 3.6 to 3.11.

The import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"2 module is used to perform HTTP requests; the minimum is version 2.0.

Usage

Import the package and construct a import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"3. The first argument is a string containing your API authentication key as found in your DeepL Pro Account.

Be careful not to expose your key, for example when sharing source code.

import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"

This example is for demonstration purposes only. In production code, the authentication key should not be hard-coded, but instead fetched from a configuration file or environment variable.

import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"3 accepts additional options, see for more information.

Translating text

To translate text, call import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"5. The first argument is a string containing the text you want to translate, or a list of strings if you want to translate multiple texts.

import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"6 and import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"7 specify the source and target language codes respectively. The import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"6 is optional, if it is unspecified the source language will be auto-detected.

Language codes are case-insensitive strings according to ISO 639-1, for example import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"9, # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # 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" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'0, # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # 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" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'1. Some target languages also include the regional variant according to ISO 3166-1, for example # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # 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" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'2, or # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # 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" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'3. The full list of supported languages is in the API documentation.

There are additional optional arguments to control translation, see below.

import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"5 returns a # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # 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" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'5, or a list of # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # 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" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'5s corresponding to your input text(s). # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # 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" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'5 has two properties: # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # 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" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'8 is the translated text, and # Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # 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" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'9 is the detected source language code.

# Translate text into a target language, in this case, French: result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !" # 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" the language code for Japanese print(result[1].text) # "How are you?" print(result[1].detected_source_lang) # "ES" the language code for Spanish # Translate into German with less and more Formality: print( translator.translate_text( "How are you?", target_lang="DE", formality="less" ) ) # 'Wie geht es dir?' print( translator.translate_text( "How are you?", target_lang="DE", formality="more" ) ) # 'Wie geht es Ihnen?'

Text translation options

In addition to the input text(s) argument, the available import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"5 arguments are:

  • import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"6: Specifies the source language code, but may be omitted to auto-detect the source language.
  • import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"7: Required. Specifies the target language code.
  • # Translate a formal document from English to German input_path = "/path/to/Instruction Manual.docx" output_path = "/path/to/Bedienungsanleitung.docx" try: # Using translate_document_from_filepath() with file paths translator.translate_document_from_filepath( input_path, output_path, target_lang="DE", formality="more" ) # Alternatively you can use 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" ) except deepl.DocumentTranslationException as error: # If an error occurs during document translation after the document was # already uploaded, a DocumentTranslationException is raised. The # document_handle property contains the document handle that may be used to # later retrieve the document from the server, or contact DeepL support. doc_id = error.document_handle.id doc_key = error.document_handle.key print(f"Error after uploading ${error}, id: ${doc_id} key: ${doc_key}") except deepl.DeepLException as error: # Errors during upload raise a DeepLException print(error)3: specify how input text should be split into sentences, default: # Translate a formal document from English to German input_path = "/path/to/Instruction Manual.docx" output_path = "/path/to/Bedienungsanleitung.docx" try: # Using translate_document_from_filepath() with file paths translator.translate_document_from_filepath( input_path, output_path, target_lang="DE", formality="more" ) # Alternatively you can use 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" ) except deepl.DocumentTranslationException as error: # If an error occurs during document translation after the document was # already uploaded, a DocumentTranslationException is raised. The # document_handle property contains the document handle that may be used to # later retrieve the document from the server, or contact DeepL support. doc_id = error.document_handle.id doc_key = error.document_handle.key print(f"Error after uploading ${error}, id: ${doc_id} key: ${doc_key}") except deepl.DeepLException as error: # Errors during upload raise a DeepLException print(error)4.
    • # Translate a formal document from English to German input_path = "/path/to/Instruction Manual.docx" output_path = "/path/to/Bedienungsanleitung.docx" try: # Using translate_document_from_filepath() with file paths translator.translate_document_from_filepath( input_path, output_path, target_lang="DE", formality="more" ) # Alternatively you can use 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" ) except deepl.DocumentTranslationException as error: # If an error occurs during document translation after the document was # already uploaded, a DocumentTranslationException is raised. The # document_handle property contains the document handle that may be used to # later retrieve the document from the server, or contact DeepL support. doc_id = error.document_handle.id doc_key = error.document_handle.key print(f"Error after uploading ${error}, id: ${doc_id} key: ${doc_key}") except deepl.DeepLException as error: # Errors during upload raise a DeepLException print(error)5 (# Translate a formal document from English to German input_path = "/path/to/Instruction Manual.docx" output_path = "/path/to/Bedienungsanleitung.docx" try: # Using translate_document_from_filepath() with file paths translator.translate_document_from_filepath( input_path, output_path, target_lang="DE", formality="more" ) # Alternatively you can use 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" ) except deepl.DocumentTranslationException as error: # If an error occurs during document translation after the document was # already uploaded, a DocumentTranslationException is raised. The # document_handle property contains the document handle that may be used to # later retrieve the document from the server, or contact DeepL support. doc_id = error.document_handle.id doc_key = error.document_handle.key print(f"Error after uploading ${error}, id: ${doc_id} key: ${doc_key}") except deepl.DeepLException as error: # Errors during upload raise a DeepLException print(error)6): input text will be split into sentences using both newlines and punctuation.
    • # Translate a formal document from English to German input_path = "/path/to/Instruction Manual.docx" output_path = "/path/to/Bedienungsanleitung.docx" try: # Using translate_document_from_filepath() with file paths translator.translate_document_from_filepath( input_path, output_path, target_lang="DE", formality="more" ) # Alternatively you can use 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" ) except deepl.DocumentTranslationException as error: # If an error occurs during document translation after the document was # already uploaded, a DocumentTranslationException is raised. The # document_handle property contains the document handle that may be used to # later retrieve the document from the server, or contact DeepL support. doc_id = error.document_handle.id doc_key = error.document_handle.key print(f"Error after uploading ${error}, id: ${doc_id} key: ${doc_key}") except deepl.DeepLException as error: # Errors during upload raise a DeepLException print(error)7 (# Translate a formal document from English to German input_path = "/path/to/Instruction Manual.docx" output_path = "/path/to/Bedienungsanleitung.docx" try: # Using translate_document_from_filepath() with file paths translator.translate_document_from_filepath( input_path, output_path, target_lang="DE", formality="more" ) # Alternatively you can use 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" ) except deepl.DocumentTranslationException as error: # If an error occurs during document translation after the document was # already uploaded, a DocumentTranslationException is raised. The # document_handle property contains the document handle that may be used to # later retrieve the document from the server, or contact DeepL support. doc_id = error.document_handle.id doc_key = error.document_handle.key print(f"Error after uploading ${error}, id: ${doc_id} key: ${doc_key}") except deepl.DeepLException as error: # Errors during upload raise a DeepLException print(error)8): input text will not be split into sentences. Use this for applications where each input text contains only one sentence.
    • # Translate a formal document from English to German input_path = "/path/to/Instruction Manual.docx" output_path = "/path/to/Bedienungsanleitung.docx" try: # Using translate_document_from_filepath() with file paths translator.translate_document_from_filepath( input_path, output_path, target_lang="DE", formality="more" ) # Alternatively you can use 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" ) except deepl.DocumentTranslationException as error: # If an error occurs during document translation after the document was # already uploaded, a DocumentTranslationException is raised. The # document_handle property contains the document handle that may be used to # later retrieve the document from the server, or contact DeepL support. doc_id = error.document_handle.id doc_key = error.document_handle.key print(f"Error after uploading ${error}, id: ${doc_id} key: ${doc_key}") except deepl.DeepLException as error: # Errors during upload raise a DeepLException print(error)9 (# Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries0): input text will be split into sentences using punctuation but not newlines.
  • # Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries1: controls automatic-formatting-correction. Set to # Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries2 to prevent automatic-correction of formatting, default: # Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries3.
  • # Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries4: controls whether translations should lean toward informal or formal language. This option is only available for some target languages, see .
    • # Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries5 (# Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries6): use informal language.
    • # Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries7 (# Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries8): use formal, more polite language.
  • # Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries9: specifies a glossary to use with translation, either as a string containing the glossary ID, or a # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )0 as returned by # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )1.
  • # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )2: type of tags to parse before translation, options are # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )3 and # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )4.

The following options are only used if # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )2 is # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )4:

  • # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )7: specify # Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries3 to disable automatic tag detection, default is # Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries2.
  • # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)0: list of XML tags that should be used to split text into sentences. Tags may be specified as an array of strings (# Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)1), or a comma-separated list of strings (# Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)2). The default is an empty list.
  • # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)3: list of XML tags that should not be used to split text into sentences. Format and default are the same as for # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)0.
  • # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)5: list of XML tags that containing content that should not be translated. Format and default are the same as for # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)0.

For a detailed explanation of the XML handling options, see the API documentation.

Translating documents

To translate documents, you may call either # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)7 using file IO objects, or # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)8 using file paths. For both functions, the first and second arguments correspond to the input and output files respectively.

Just as for the import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"5 function, the import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"6 and import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"7 arguments specify the source and target language codes.

There are additional optional arguments to control translation, see below.

# Translate a formal document from English to German input_path = "/path/to/Instruction Manual.docx" output_path = "/path/to/Bedienungsanleitung.docx" try: # Using translate_document_from_filepath() with file paths translator.translate_document_from_filepath( input_path, output_path, target_lang="DE", formality="more" ) # Alternatively you can use 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" ) except deepl.DocumentTranslationException as error: # If an error occurs during document translation after the document was # already uploaded, a DocumentTranslationException is raised. The # document_handle property contains the document handle that may be used to # later retrieve the document from the server, or contact DeepL support. doc_id = error.document_handle.id doc_key = error.document_handle.key print(f"Error after uploading ${error}, id: ${doc_id} key: ${doc_key}") except deepl.DeepLException as error: # Errors during upload raise a DeepLException print(error)

# Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)7 and # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)8 are convenience functions that wrap multiple API calls: uploading, polling status until the translation is complete, and downloading. If your application needs to execute these steps individually, you can instead use the following functions directly:

  • entries = translator.get_glossary_entries(my_glossary) print(entries) # "{'artist': 'Maler', 'prize': 'Gewinn'}"4,
  • entries = translator.get_glossary_entries(my_glossary) print(entries) # "{'artist': 'Maler', 'prize': 'Gewinn'}"5 (or entries = translator.get_glossary_entries(my_glossary) print(entries) # "{'artist': 'Maler', 'prize': 'Gewinn'}"6), and
  • entries = translator.get_glossary_entries(my_glossary) print(entries) # "{'artist': 'Maler', 'prize': 'Gewinn'}"7

Document translation options

In addition to the input file, output file, import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"6 and import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"7 arguments, the available # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)7 and # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)8 arguments are:

  • # Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries4: same as in .
  • # Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries9: same as in .

Glossaries

Glossaries allow you to customize your translations using user-defined terms. Multiple glossaries can be stored with your account, each with a user-specified name and a uniquely-assigned ID.

Creating a glossary

You can create a glossary with your desired terms and name using text = "The artist was awarded a prize." with_glossary = translator.translate_text( text, source_lang="EN", target_lang="DE", glossary=my_glossary, ) print(with_glossary) # "Der Maler wurde mit einem Gewinn ausgezeichnet." # For comparison, the result without a glossary: without_glossary = translator.translate_text(text, target_lang="DE") print(without_glossary) # "Der Künstler wurde mit einem Preis ausgezeichnet."4. Each glossary applies to a single source-target language pair. Note: Glossaries are only supported for some language pairs, see for more information. The entries should be specified as a dictionary.

If successful, the glossary is created and stored with your DeepL account, and a # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )0 object is returned including the ID, name, languages and entry count.

# Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries

You can also upload a glossary downloaded from the DeepL website using text = "The artist was awarded a prize." with_glossary = translator.translate_text( text, source_lang="EN", target_lang="DE", glossary=my_glossary, ) print(with_glossary) # "Der Maler wurde mit einem Gewinn ausgezeichnet." # For comparison, the result without a glossary: without_glossary = translator.translate_text(text, target_lang="DE") print(without_glossary) # "Der Künstler wurde mit einem Preis ausgezeichnet."6. Instead of supplying the entries as a dictionary, specify the CSV data as text = "The artist was awarded a prize." with_glossary = translator.translate_text( text, source_lang="EN", target_lang="DE", glossary=my_glossary, ) print(with_glossary) # "Der Maler wurde mit einem Gewinn ausgezeichnet." # For comparison, the result without a glossary: without_glossary = translator.translate_text(text, target_lang="DE") print(without_glossary) # "Der Künstler wurde mit einem Preis ausgezeichnet."7 either as a file-like object or string or bytes containing file content:

# Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )

The API documentation explains the expected CSV format in detail.

Getting, listing and deleting stored glossaries

Functions to get, list, and delete stored glossaries are also provided:

  • # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )1 takes a glossary ID and returns a # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )0 object for a stored glossary, or raises an exception if no such glossary is found.
  • poetry install00 returns a list of # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )0 objects corresponding to all of your stored glossaries.
  • poetry install02 takes a glossary ID or # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )0 object and deletes the stored glossary from the server, or raises an exception if no such glossary is found.

# Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)

Listing entries in a stored glossary

The # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )0 object does not contain the glossary entries, but instead only the number of entries in the poetry install05 property.

To list the entries contained within a stored glossary, use poetry install06 providing either the # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )0 object or glossary ID:

entries = translator.get_glossary_entries(my_glossary) print(entries) # "{'artist': 'Maler', 'prize': 'Gewinn'}"

Using a stored glossary

You can use a stored glossary for text translation by setting the # Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries9 argument to either the glossary ID or # Open the CSV file assuming UTF-8 encoding. If your file contains a BOM, # consider using encoding='utf-8-sig' instead. with open('/path/to/glossary_file.csv', 'r', encoding='utf-8') as csv_file: csv_data = csv_file.read() # Read the file contents as a string my_csv_glossary = translator.create_glossary_from_csv( "CSV glossary", source_lang="EN", target_lang="DE", csv_data=csv_data, )0 object. You must also specify the import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"6 argument (it is required when using a glossary):

text = "The artist was awarded a prize." with_glossary = translator.translate_text( text, source_lang="EN", target_lang="DE", glossary=my_glossary, ) print(with_glossary) # "Der Maler wurde mit einem Gewinn ausgezeichnet." # For comparison, the result without a glossary: without_glossary = translator.translate_text(text, target_lang="DE") print(without_glossary) # "Der Künstler wurde mit einem Preis ausgezeichnet."

Using a stored glossary for document translation is the same: set the # Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries9 argument and specify the import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"6 argument:

poetry install0

The # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)7, # Retrieve a stored glossary using the ID glossary_id = "559192ed-8e23-..." my_glossary = translator.get_glossary(glossary_id) # Find and delete glossaries named 'Old glossary' glossaries = translator.list_glossaries() for glossary in glossaries: if glossary.name == "Old glossary": translator.delete_glossary(glossary)8 and entries = translator.get_glossary_entries(my_glossary) print(entries) # "{'artist': 'Maler', 'prize': 'Gewinn'}"4 functions all support the # Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries9 argument.

Checking account usage

To check account usage, use the poetry install17 function.

The returned poetry install18 object contains three usage subtypes: poetry install19, poetry install20 and poetry install21. Depending on your account type, some usage subtypes may be invalid; this can be checked using the poetry install22 property. For API accounts:

  • poetry install23 is # Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries2,
  • poetry install25 and poetry install26 are # Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries3.

Each usage subtype (if valid) has poetry install28 and poetry install29 properties giving the amount used and maximum amount respectively, and the poetry install30 property that checks if the usage has reached the limit. The top level poetry install18 object has the poetry install32 property to check all usage subtypes.

poetry install1

Listing available languages

You can request the list of languages supported by DeepL for text and documents using the poetry install33 and poetry install34 functions. They both return a list of poetry install35 objects.

The poetry install36 property gives the name of the language in English, and the poetry install37 property gives the language code. The poetry install38 property only appears for target languages, and indicates whether the target language supports the optional # Create an English to German glossary with two terms: entries = {"artist": "Maler", "prize": "Gewinn"} my_glossary = translator.create_glossary( "My glossary", source_lang="EN", target_lang="DE", entries=entries, ) print( f"Created '{my_glossary.name}' ({my_glossary.glossary_id}) " f"{my_glossary.source_lang}->{my_glossary.target_lang} " f"containing {my_glossary.entry_count} entries" ) # Example: Created 'My glossary' (559192ed-8e23-...) EN->DE containing 2 entries4 parameter.

poetry install2

Listing available glossary languages

Glossaries are supported for a subset of language pairs. To retrieve those languages use the poetry install40 function, which returns an array of poetry install41 objects. Each has import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"6 and import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"7 properties indicating that that pair of language codes is supported.

poetry install3

You can also find the list of supported glossary language pairs in the API documentation.

Note that glossaries work for all target regional-variants: a glossary for the target language English (poetry install44) supports translations to both American English (poetry install45) and British English (poetry install46).

Exceptions

All module functions may raise poetry install47 or one of its subclasses. If invalid arguments are provided, they may raise the standard exceptions poetry install48 and poetry install49.

Configuration

Logging

Logging can be enabled to see the HTTP requests sent and responses received by the library. Enable and control logging using Python's poetry install50 module, for example:

poetry install4

Server URL configuration

You can override the URL of the DeepL API by specifying the poetry install51 argument when constructing a poetry install52. This may be useful for testing purposes. You do not need to specify the URL to distinguish API Free and API Pro accounts, the library selects the correct URL automatically.

poetry install5

Proxy configuration

You can configure a proxy by specifying the poetry install53 argument when constructing a poetry install52:

poetry install6

The proxy argument is passed to the underlying import deepl auth_key = "f63c02c5-f056-..." # Replace with your key translator = deepl.Translator(auth_key) result = translator.translate_text("Hello, world!", target_lang="FR") print(result.text) # "Bonjour, le monde !"2 session, see the ; a dictionary of schemes to proxy URLs is also accepted.

Command Line Interface

The library can be run on the command line supporting all API functions. Use the poetry install56 option for usage information:

poetry install7

The CLI requires your DeepL authentication key specified either as the poetry install57 environment variable, or using the poetry install58 option, for example:

poetry install8

Note that the poetry install58 argument must appear before the command argument. The recognized commands are:

CommandDescriptiontexttranslate text(s)documenttranslate document(s)usageprint usage information for the current billing periodlanguagesprint available languagesglossarycreate, list, and remove glossaries

For example, to translate text:

poetry install9

Wrap text arguments in quotes to prevent the shell from splitting sentences into words.

Issues

If you experience problems using the library, or would like to request a new feature, please open an issue.

Development

We welcome Pull Requests, please read the contributing guidelines.

Tests

Execute the tests using poetry install60. The tests communicate with the DeepL API using the auth key defined by the poetry install57 environment variable.

Be aware that the tests make DeepL API requests that contribute toward your API usage.

The test suite may instead be configured to communicate with the mock-server provided by deepl-mock. Although most test cases work for either, some test cases work only with the DeepL API or the mock-server and will be otherwise skipped. The test cases that require the mock-server trigger server errors and test the client error-handling. To execute the tests using deepl-mock, run it in another terminal while executing the tests. Execute the tests using poetry install60 with the poetry install63 and poetry install64 environment variables defined referring to the mock-server.

Note: for testing on Python 3.6, you need to install an older version of poetry install60 manually, using poetry install66.

Postingan terbaru

LIHAT SEMUA