実行した環境
Ubuntu Stdio 17.10
LibreOffice 6.0.0.3
Python 3.5.4(LibreOfficeバンドル)
googletrans 2.2.0というGoogle翻訳のパッケージを見つけました
インストール(LibreOfficeバンドルのPythonではなく、UbuntuのPython 3.6.3にインストール)
$ pip3 install googletrans
LibreOfficeバンドルのPythonで使用するには、
LibreOfficeバンドルのPythonにUbuntu - Pythonのパッケージのパスを追加する
https://qiita.com/ty21ky/items/3b944a7abe23e3bcaf8f
を実行します。
ドキュメント
https://media.readthedocs.org/pdf/py-googletrans/latest/py-googletrans.pdf
主なcountry code : LANGUAGES(ドキュメントの最後に記載)
'zh-cn' : '中国語(簡体字)'
'zh-tw' : '台湾語'
'en' : '英語'
'fr' : 'フランス語'
'de' : 'ドイツ語'
'ja' : 'japanese'
'ko' : 'korean'
主な使い方(下記のサンプルプログラムはLibreOfficeではなくPythonで実行する。)
class googletrans.models.Translated(src,dest,origin,text,pronunciation)
Translate result object(オブジェクトの翻訳)
Parameters
•src– source langauge ソース言語(default: auto)省略すると自動検出
•dest– destination language 翻訳後の言語(default: en)省略すると英語
•origin– original text(元のテキスト)
•text– translated text(翻訳されたテキスト)
•pronunciation– pronunciation(発音記号)
翻訳(英語>日本語)
import pyperclip
from googletrans import Translator
translator = Translator()
a = 'Googletrans is a free and unlimited python library that implemented Google Translate API. This uses the Google Translate Ajax API to make calls to such methods as detect and translate.'
print(translator.translate(a, dest='ja').text)
$ python3 translate.py
Googletransは、Google Translate APIを実装した無制限のPythonライブラリです。これは、Google Translate Ajax APIを使用して、検出や翻訳などのメソッドを呼び出します。
翻訳(日本語>台湾語)
import pyperclip
from googletrans import Translator
translator = Translator()
a = '実行する前に文章をコピーする。'
print(translator.translate(a, src='ja',dest='zh-tw').text)
$ python3 translate1.py
運行前複製句子。
発音記号を表示(src=destの時のみ)
import pyperclip
from googletrans import Translator
translator = Translator()
a = '実行する前に文章をコピーする。'
print(translator.translate(a, dest='ja').pronunciation)
$ python3 pronunciation.py
Jikkō suru mae ni bunshō o kopī suru.
ソース言語が何語か調べる
import pyperclip
from googletrans import Translator
translator = Translator()
a = 'Merci pour votre gentillesse.'
print(translator.translate(a, dest='ja').src)
$ python3 src.py
fr
オリジナルの文章を表示
import pyperclip
from googletrans import Translator
translator = Translator()
a = 'Merci pour votre gentillesse.'
print(translator.translate(a, dest='ja').origin)
$ python3 origin.py
Merci pour votre gentillesse.