LoginSignup
28
29

More than 5 years have passed since last update.

Pythonでパワポスライドを自動翻訳する

Last updated at Posted at 2018-09-24

python-pptxとGoogleAppsScriptを使うことでパワポを自動で翻訳する。
ここでは事前準備から入力された日本語スライドを英語に自動で翻訳し、出力するまでを述べる。

あらまし

先日、とある講演会に参加した際に英語のスライドの人や日本語のスライドの人、あるいは混ぜこぜになっている人など様々であった。
確かに一度作ったスライドを翻訳するのはなかなかの手間である。
そこで、Pythonでパワポを操作できるライブラリであるpython-pptxとGoogleの翻訳APIを使えるGoogleAppsScriptを組み合わせることで実現する。

環境はPython 3.5@windows8.1

GoogleAppsScriptについて

GoogleAppsScriptはグーグルの提供するGmailやスプレッドシートなどのサービスをスクリプトを通して操作できるサービスである。
今回はGoogleTranslateを使用するが、その方法は下記記事で述べられているので省略する。

3 分で作る無料の翻訳 API with Google Apps Script

この記事に則ってスクリプトを作成し、公開を行うと以下のようなURLが表示される。
https://script.google.com/macros/s/[api_key]/exec
※[api_key]は作成者によって異なる。

このURLに行いたい処理のパラメータを書き加えることで翻訳できる。
試しに「こんにちは」という日本語を英語に翻訳する場合は以下のようなURLにしてブラウザに入力すると翻訳された「Hello」が表示される。

https://script.google.com/macros/s/[api_key]/exec?text=こんにちは&source=ja&target=en

GoogleAPIScript.png

シンプルな表示

PythonからURLにアクセス

ここではPythonから先ほどのURLにアクセスする方法として以下のサイトを参考にした。

一つは先ほどのGoogleAppsScriptにJavaScriptでアクセスする例。
日英翻訳スクリプト on コマンドライン

もう一つはGoogleAPIにPythonでアクセスする例。
Python で、Google の Translation API を使う

上記の例を参考にpythonのrequestsライブラリを用いる。

pip install requests

ここでは後で利用しやすくするため関数として定義している。

def translate(str_in, source="ja", target="en"):
    url="https://script.google.com/macros/s/"
    url += api_key
    url += "/exec?text=" + str_in
    url += "&source=" + source
    url += "&target=" + target
    rr = requests.get(url)

    return rr.text

※api_keyは上記GoogleAppsScriptで取得したもの。

パワポを読み込んで文字列にアクセスする例

python-pptxの使い方については公式のドキュメントやそれを簡単に説明させて頂いた拙著などを見ていただければと思う。
拙著:Pythonでパワポの説明資料(報告書)を生成する

python-pptx公式のチュートリアルでスライド中の文字列をすべて抽出する例がある。

pip install python-pptx

以下の公式の例では指定したファイルのスライドを全て読み込み、その中からText形式のものを抽出している。

from pptx import Presentation

prs = Presentation(path_to_presentation)

# text_runs will be populated with a list of strings,
# one for each text run in presentation
text_runs = []

for slide in prs.slides:
    for shape in slide.shapes:
        if not shape.has_text_frame:
            continue
        for paragraph in shape.text_frame.paragraphs:
            for run in paragraph.runs:
                text_runs.append(run.text)

引用:python-pptx documentation Getting Started

スライド中文字列の抽出と翻訳

上記の例から、文字列を抜き出しつつ、先ほど作成したGoogleAppsScriptのURLにアクセスする関数を使って翻訳するという流れで翻訳を試みる。

以下が作成した例。

import requests
from pptx import Presentation
from time import sleep

if __name__ == '__main__':
    path_to_presentation = "test.pptx"

    prs = Presentation(path_to_presentation)

    print("start")
    for ns, slide in enumerate(prs.slides):
        for nsh, shape in enumerate(slide.shapes):
            if not shape.has_text_frame:
                continue
            for np, paragraph in enumerate(shape.text_frame.paragraphs):
                for rs, run in enumerate(paragraph.runs):
                    str_in = run.text
                    str_out = translate(str_in)
                    prs.slides[ns].shapes[nsh].text_frame.paragraphs[np].runs[rs].text = str_out
                    sleep(1.5)
                    print(np)

    prs.save('test_trans.pptx')
    print("end")

※本文中のtranslateは先ほど作成したもの。
また、本例にはエラー処理等は入っていない。また、Googleへの過負荷を避けるためリクエスト毎にSleepで1秒以上のインターバルを開けている。

入力例としてロボット工学三原則や運動の法則の書かれたスライドを用意した。
入力スライド.png

出力の結果、無事に翻訳できている。
翻訳の正しさは議論しない。
翻訳結果.png

注意点として、フォント、フォントサイズの異なる場合は別の文字列として扱われるので一文として翻訳したい場合はその辺りに注意が必要。

おわりに

本記事ではパワポスライドを翻訳する方法について述べた。
この例と同様に、WordやExcelを操作するPythonライブラリもあることから、
ドキュメントの多言語化が容易になるものと考えられる。

28
29
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28
29