LoginSignup
1
4

More than 3 years have passed since last update.

英語論文をpdfからコピペしてGoogle翻訳に入れたい

Last updated at Posted at 2020-01-13

1.概要

論文をpdfからコピペし、Google翻訳したいときに使用するもの。
出力したconv.htmlを開いて右クリックから翻訳する。
急遽必要だったためめちゃくちゃ適当に作成。メンテの予定も無し。

2.ソースコード

"""
論文をpdfからコピペし、Google翻訳したいときに使用するもの。
.txtのパスを与えると_conv.htmlで出すのでchromeで開いて右クリックから翻訳する。

・やってること
pdfでctrl + A して貼り付けると改行が無駄に入って上手く翻訳してくれないので
それを回避する。
"""

import sys
import os

filepath = sys.argv[1]

# ---テキストをGoogle翻訳用に加工
with open(filepath, "r", encoding="utf-8") as f:

    lines = f.readlines()
    newlines = []
    lenbuf = []  # lineのlenのバッファ
    for line in lines:
        if len(line) >= 2:
            if line[-2] == ".":  # 末端がピリオドなら改行を入れる
                line = line[:-1] + "<br>"
            else:  # そうじゃないならスペース入れる
                line = line[:-1] + " "
        newlines.append(line)
        lenbuf.append(len(line))
f.close()

# ---改行を入れた方がよさげだったら改行を入れる(line長さの平均*0.8を閾値にする)
ave_len = sum(lenbuf) / len(lenbuf)
for n, linelen in enumerate(lenbuf):
    if linelen < ave_len * 0.8:
        newlines[n] = newlines[n] + "<br>"

# ---_conv付けて保存
savepath = os.path.splitext(filepath)[0] + "_conv.html"

with open(savepath, "w", encoding="utf-8") as f:
    f.writelines(newlines)
1
4
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
1
4