News & Columns
DeepL Using Python for translation
2nd Translation of text files

In this article, you will learn how to use the DeepL translation of how to use the API. This is the second article about file translation. API for file translation.
(see first issue here).
File translation requires a combination of three API calls.
Prepare a text file test.txt with appropriate Japanese.
Consider a mechanism to translate it into English (U.S.) and save it as test.trans.txt .
Step 1: Upload the source language file
When you submit a translation request to the DeepL server, specify the source language file.
$ curl https://api.deepl.com/v2/document \
$ -F file=@test.txt \
$ -F auth_key=${auth_key} \
$ -F target_lang=en-us
The file for which a translation request has been accepted is displayed in the document_id and document_key are returned from the server in json format,
are also returned from the server in json format.
The process up to the sending of the file can be written in Python as follows:
url = 'https://api.deepl.com/v2/document'
files = dict()
files['file'] = open(fn, 'rb')
files['auth_key'] = (None, get_key())
files['target_lang'] = (None, 'en-us')
res = requests.post(url, files=files)
The specification of auth_key and target_lang is quite tricky.
There are several ways to specify items in files, and in the case of a binary tuple,
one item is a file name and two items are objects.
If you want to specify a string, you can write it this way.
Another way is to store auth_key and target_lang in the data (dict format) as in
first string translation, and pass both data and files to
requests.post().
Step 2: Obtaining the status of the translation process
Inquire about the translation status of the file being translated:
$ document_id=[documentID]
$ document_key=[documentKey].
$ curl https://api.deepl.com/v2/document/${document_id}
$ -d auth_key=${auth_key} \{auth_key}
$ -d document_key=${document_key}
The status is queued (Accepted), (Translating), and translating (translation in progress), (translation error occurred), and
error (translation error occurred), (translation finished), and test.trans.txt (translation finished).
In the case of translating, an estimate of the time remaining in the process is also returned.
Assuming you have assigned the variable document_id and document_key variable has been assigned,
The process up to the sending of the file can be written in Python as follows:
url = f'https://api.deepl.com/v2/document/{document_id}'
data = dict()
data['auth_key'] = get_key()
data['document_key'] = document_key
res = requests.post(url, data=data)
Step 3: Download the target language file
If the translation status is done , the file is downloaded:
$ curl https://api.deepl.com/v2/document/${document_id}/result \}
$ -d auth_key=${auth_key} \{auth_key}
$ -d document_key=${document_key} > test.trans.txt
The download is only done once.
The translation results are redirected (saved) to test.trans.txt The results are redirected (saved) to
The process up to the sending of the file can be written in Python as follows:
url = f'https://api.deepl.com/v2/document/{document_id}/result'
data = dict()
data['auth_key'] = get_key()
data['document_key'] = document_key
res = requests.post(url, data=data)
File translation process
Finally, connect the above processes.
Since step 2 only returns the progress of the translation,
we need to wait for the result of that process. Basically:
・ Translating for the remaining time seconds_remaining for the
number of seconds sleep and check the progress again.
If done or error , exit the loop.
The above process can be written in Python as follows:
import requests
import json
from time import sleepdef get_key():
return open('key.txt').read().rstrip()
def upload_src(fn):
"'
file translation step 1: upload source language file
"'
url = 'https://api.deepl .com/v2/document'
files = dict()
files['file'] = open(fn, 'rb')
files ['auth_key'] = (None, get_key())
files['target_lang'] = (None, 'en-us ')res = requests.post(url, files=files)
res_status = res.status_code # 200 if successful (not used now)
res_text = res.text
res_data = json.loads(res_text)
document _id = res_data['document_id']
document_key = res_data['document_key']
return document_id,. document_key
def get_trans_status(document_id, document_key):
"'
sub for step 2: Get translation processing status
"'
url = f 'https://api.deepl.com/v2/document/{document_id}'
data = dict()
data['auth_key'] = get_ key()
data['document_key'] = document_keyres = requests.post(url, data=data)
res_status = res.status_code # 200 if successful (not used now)
res_text = res.text
res_data = json.loads(res_text)
return res_ data
def check_proceeding(document_id, document_key):
"'
file translation step 2: waiting for translation process
"'
while True:
res = get_trans_status(document_id, document_key)
status = res['status']
print(f'status: {status }', flush=True)
seconds_remaining = 0
if status == 'done' or status == 'error':
break
elif status == 'translating':
if 'seconds_remaining' in res:
seconds_remaining = int( res['seconds_remaining'])
# avoid errors
if seconds_remaining <= 0:
seconds_remaining = 10
else: # queued etc
pass
print(f '...waiting for (another) {seconds_remaining}s', flush=True)
sleep(seconds_remaining)
return status
def download_tgt(fn, document_id, document_key):
"'
file translation step 3: download target language file
"'
url = f 'https://api.deepl.com/v2/document/{document_id}/result'
data = dict()
data['auth_key' ] = get_key()
data['document_key'] = document_keyres = requests.post(url, data=data)
res_status = res.status_code # 200 if successful (not used now)
tgt_bin = res._content
with open(fn, 'w ', encoding='utf-8′) as f:
print(tgt_bin.decode('utf-8'), end=", file=f)
def main():
fn_src = 'test.txt'
fn_tgt = fn_src.replace('.txt', '.trans.txt ')print(f'fn_src: {fn_src}')
print(f'fn_tgt: {fn_tgt}')print(f'uploading: {fn_src}')
document_id, document_key = upload_src(fn_src)
status = check_proceeding(document_id , document_key)
if status == 'done':
print(f'downloading: {fn_tgt}')
download_tgt(fn_tgt, document_id, document_key)if __name__ == '__main__':
main()
The three API calls are similar processes, but
for the theme of "writing curl commands in Python,"
I have not dared to summarize them and write them in a verbose way.
That is all for this article:
I translated the file using the API of DeepL.
The curl command was expressed in Python and programmed.
Thank you for reading.