[PYTHON] Format the text of an English dissertation with a single shortcut key and plunge into DeepL translation

The most difficult thing when reading an English dissertation is to copy the sentence for DeepL Translator and then format it every time. .. (?) We have automated the process so that it can be done with a single shortcut key. 図.png It is like this. It is intended for Windows.

Method

  1. Take out the statement stored in the clipboard with a Python script, format it, and then throw it to DeepL Translator.
  2. Allow this Python script to be executed from a batch file
  3. Make the batch file executable from the shortcut key.

Python script

ClipboardCopyToDeepL.py


import re
import pyperclip
import webbrowser
import urllib.parse

text = urllib.parse.quote(pyperclip.paste(), safe = "|").replace("|", "%5C%7C").replace("%0D", "%20").replace("%0A", "").replace("-%20", "")
webbrowser.open("https://www.deepl.com/en/translator#en/ja/" + text)

(5/27 postscript: After all I decided to use urlLib. For some reason, "|" was "% 5C% 7C" instead of the standard "% 7C", so I changed it separately)

It's pretty simple, but I use pyperclip to retrieve the text on the clipboard and then format it. The rules of plastic surgery are

--Eliminate the - that enters when English words are separated by line breaks --Replace line breaks with spaces

only. Finally, throw a web request to DeepL Translator and you're done. For DeepL Translator, enter the character string you want to translate at the end of https://www.deepl.com/en/translator#en/ja/ and it will jump to the site where the translation result is displayed, so use that. Did.

Save this script somewhere so that it will be executed when requested in a batch file.

batch file

Place the following batch file in the same hierarchy as the python script

"Python execution directory" "python script directory"

For example, if you are using anaconda and place the created script under "ClipboardCopyToDeepL /" on the desktop

ClipboardCopyToDeepL.bat


C:\Users\User name\anaconda3\python.exe C:\Users\User name\Desktop\ClipboardCopyToDeepL\ClipboardCopyToDeepL.py

It becomes.

Shortcut key assignment

Create a shortcut for the batch file. 図1.png Set the shortcut key from the shortcut properties. 図2.png Click the frame and hit the key you want to assign, and it will be entered without permission.

(Addition: I can use this shortcut by placing it under C: \ ProgramData \ Microsoft \ Windows \ Start Menu \ Programs \.)

At the end

This will make your dissertation easier to read.

Recommended Posts

Format the text of an English dissertation with a single shortcut key and plunge into DeepL translation
Automatically translate DeepL into English with Python and Selenium
Calculate the shortest route of a graph with Dijkstra's algorithm and Python
The story of making a sound camera with Touch Designer and ReSpeaker
Make a DNN-CRF with Chainer and recognize the chord progression of music