Use DeepL with python (for dissertation translation)

tran.py



import json
import urllib.parse
import urllib.request
import re


def translate(text, s_lang='', t_lang=''):
    DEEPL_TRANSLATE_EP = 'https://api.deepl.com/v2/translate'
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded; utf-8'
    }

    params = {
        'auth_key': "",#Enter your API key here
        'text': text,
        'target_lang': t_lang
    }

    if s_lang != '':
        params['source_lang'] = s_lang

    req = urllib.request.Request(
        DEEPL_TRANSLATE_EP,
        method='POST',
        data=urllib.parse.urlencode(params).encode('utf-8'),
        headers=headers
    )

    try:
        with urllib.request.urlopen(req) as res:
            res_json = json.loads(res.read().decode('utf-8'))
            res_json=res_json["translations"][0]["text"]
            return json.dumps(res_json, indent=2, ensure_ascii=False)
    except urllib.error.HTTPError as e:
        return "Could not get it."
#Added the above


with open('text.txt') as f:
    text = f.read()

text=re.sub('\n', ' ', text)
text = translate(text, s_lang='', t_lang='JA')
text=re.sub('\n', ' ', text)
print(text)

How to use

Place tran.py and text.txt in the same hierarchy, and enter the English text you want to translate into text.txt.

python tran.py

Run tran.py on.

reference

I tried using the DeepL API in Python

Recommended Posts

Use DeepL with python (for dissertation translation)
Use mecab with Python3
Use DynamoDB with Python
Use Python 3.8 with Anaconda
Use python with docker
[Translation] Getting Started with Rust for Python Programmers
Use logger with Python for the time being
Wrap C with Cython for use from Python
Wrap C ++ with Cython for use from Python
Use Trello API with python
Use Twitter API with Python
Use TUN / TAP with Python
Use Python installed with pyenv for PL / Python execution environment
Use subsonic API with python3
Python: How to use async with
Use PointGrey camera with Python (PyCapture2)
Use vl53l0x with Raspberry Pi (python)
[Python] Use Basic/Digest authentication with Flask
Getting Started with Python for PHPer-Classes
Use NAIF SPICE TOOLKIT with Python
Next, use Python (Flask) for Heroku!
Use rospy with virtualenv in Python3
Use Python in pyenv with NeoVim
How to use FTP with Python
Use Windows 10 speech synthesis with Python
Beginners use Python for web scraping (1)
Use OpenCV with Python 3 in Window
Use PostgreSQL with Lambda (Python + psycopg2)
Beginners use Python for web scraping (4) ―― 1
Getting Started with Python for PHPer-Functions
How to use an external editor for Python development with Grasshopper
INSERT into MySQL with Python [For beginners]
WEB scraping with Python (for personal notes)
Manually ssh registration for coreserver with python
Use smbus with python3 under pyenv environment
Memo to ask for KPI with python
Amplify images for machine learning with python
Use Amazon Simple Notification Service with Python
Tips for dealing with binaries in Python
[Python] Use string data with scikit-learn SVM
[Introduction to Python] Let's use foreach with Python
Use PIL and Pillow with Cygwin Python
Use cryptography library cryptography with Docker Python image
[Python] Organizing how to use for statements
Tips for using python + caffe with TSUBAME
[Shakyo] Encounter with Python for machine learning
Process multiple lists with for in Python
Getting Started with Python for PHPer-Super Basics
Debug for mysql connection with python mysql.connector
Use Application Insights with Python 3 (including bottles)
[Python] Read images with OpenCV (for beginners)
How to use "deque" for Python data
WebApi creation with Python (CRUD creation) For beginners
Use C ++ functions from python with pybind11
Use pathlib in Maya (Python 2.7) for upcoming Python 3.7
Array buffer object for use with Cython
Use selenium phantomjs webdriver with python unittest
Until you can use opencv with python
Use Python and MeCab with Azure Functions
Preparation for scraping with python [Chocolate flavor]
[For beginners] Try web scraping with Python