Using DeepL translation from Python (Part 1)

This article introduces how to use the DeepL translation API, divided into Part 1 and Part 2, for string translation and file translation.
*A rudimentary knowledge of Python and Linux is assumed.
Please also refer to the manual of the original source.
https://www.deepl.com/en/docs-api
We use Python 3.9.
API specifications are current as of July 2022. Please be aware of this.
To use the API, you must register as a user and obtain an access key ( auth_key) from
.
In this article, we will use the term [authKey], so please replace it with your own access key.
The document ID and document key strings that appear later will also be denoted as [documentID] [documentKey].
Translation of tagged strings
First, translate the strings.
Translate Japanese with xml tags (html tags) into English (U.S.).
The code examples in the manual are described in two formats: the HTTP Request format and curl, which is the Linux
standard command tool.
We will look at the curl one, which is easier to try, as a reference.
If you want to translate the Japanese string " Example: <br /><span style="font-size:1.2em">Hello, world</span>"
into English (U.S.), the following will do:
$ src='Example: <br /><span style="font-size:1.2em">Hello, World</span>'
$ auth_key=[authKey]
$ curl https://api.deepl.com/v2/ translate \
$ -d auth_key=${auth_key} \
$ -d text=${src} \
$ -d target_lang=en-us \
$ -d tag_handling=xml
This url is for the paid version. Please refer to the manual for the url of the free version.
Since we are dealing with xml tags, we also specify tag_handling in the request.
The original language is not specified, but in this case it will be detected automatically.
The execution result is omitted in this article. Please read on while actually checking it.
This can be written in Python using requests.post() as follows:
(If you do not have requests, please install it)
import requests
import json
def get_key():
return open('key.txt').read().rstrip()
def translate_xml(src):
"'
translate xml tagged string
write curl command in python
"'
url = 'https://api.deepl.com/v2/translate&# 8217;
headers = dict()
headers['Content-Type'] = 'application/x-www-form-urlencoded'
data = dict()
data['auth_ key'] = get_key()
data['text'] = src
data['target_lang'] = 'en-us'
data['tag_handling’ ;] = 'xml'
res = requests.post(url, headers=headers, data=data)
res_text = res.text
res_data = json.loads(res_text)
tgt = res_data['translations&# 8217;][0]['text']
return tgt
src = 'Example text:<br /><span style="font-size:1.2em">Hello, world</span>'
tgt = translate_xml(src)
print( tgt)
The access key string is read and used from the one stored in the
text file key.txt to avoid writing it in the source code.
res_text is the result received from the DeepL server and is a string in json format.
We use json.loads() to convert it once into a dict and then retrieve the translation results.
One of the purposes of this article is to show an example of the
Python representation of the manual curl command.
You can compare the two to get a feel for them.
That's all for this article:
We used the API of DeepL to translate the string.
In the next article, we will use the API of DeepL to translate string files.
Thank you for reading.