1. 程式人生 > >使用PyPDF2、reportlab和google翻譯實現對英文pdf文件的自動翻譯

使用PyPDF2、reportlab和google翻譯實現對英文pdf文件的自動翻譯

前言

工作中遇到很多時候需要閱讀英文PDF文件,英文讀起來比較費勁,閱讀效率也比較低,於是就想有沒有辦法自動把英文PDF自動翻譯成中文呢?答案是肯定的,雖然有道詞典這些工具可以自動取詞翻譯,但都是隻能單個詞的翻譯,而現在google翻譯已經做得非常好非常好準確了,可以整段整段的翻譯,這對於輔助閱讀,提高閱讀效率是很有幫助的。

有了想法,馬上就說幹就幹,思路就是讀取PDF每頁進行處理,呼叫google翻譯的介面翻譯成中文後後生成新的一頁PDF,然後插入到原PDF頁的後面,這樣方便對照英文原文來看。

PDF讀取和寫入

PDF的讀取和寫入,可以使用PyPDF2方案,直接使用 pip 安裝就可以了

pip install PyPDF2

PyPDF2 包含了 PdfFileReader PdfFileMerger PageObject PdfFileWriter 四個常用的主要 Class,其中PdfFileReader是用來讀取pdf的,PdfFileWriter用來寫pdf檔案的,PageObject代表pdf每頁的物件結構。

讀取PDF檔案的程式碼,這是獲取所有頁的文字:

from PyPDF2.pdf import PdfFileReader, PdfFileWriter, ContentStream

def getDataUsingPyPdf2(filename)
:
pdf = PdfFileReader(open(filename, "rb")) content = "" num = pdf.getNumPages() for i in range(0, num): extractedText = pdf.getPage(i).extractText() content += extractedText + "\n" return content

我們需要對每一頁的文字進行翻譯處理:

def dopage(page):  
    content = page["/Contents"
].getObject() if not isinstance(content, ContentStream): content = ContentStream(content, pdf) text = u_("") for operands, operator in content.operations: #print operator, operands if operator == b_("Tj"): _text = operands[0] if isinstance(_text, TextStringObject): text += _text + " " elif operator == b_("rg"): text += "\n" elif operator == b_("T*"): text += "\n" elif operator == b_("'"): text += "\n" _text = operands[0] if isinstance(_text, TextStringObject): text += operands[0] + " " elif operator == b_('"'): _text = operands[2] if isinstance(_text, TextStringObject): text += _text + " " elif operator == b_("TJ"): for i in operands[0]: if isinstance(i, TextStringObject): text += i text += " " texts = text.split('. ') results='' for i in range(len(texts)): try: results = results + translate(str(texts[i])) + "\n" except Exception as e: print e return results

這個函式其實是從extractText摘取出來的,只是為了更靈活的對文字進行處理而已,因為PDF的文字是割裂的,需要拼接起來,這個其實沒完全處理好,只是做了簡單的處理。

翻譯方案

翻譯採用的是google翻譯,是從網上直接摘取的程式碼,做了少許修改優化,改成支援中文的,並且修改了翻譯輸出結果不完整的bug

#-*- coding:utf-8 -*

import re
import execjs
import urllib,urllib2
import sys
import json 

reload(sys)
sys.setdefaultencoding( "utf-8" )

class Py4Js():
    def __init__(self):
        self.ctx = execjs.compile("""
        function TL(a) {
        var k = "";
        var b = 406644;
        var b1 = 3293161072;

        var jd = ".";
        var $b = "+-a^+6";
        var Zb = "+-3^+b+-f";

        for (var e = [], f = 0, g = 0; g < a.length; g++) {
            var m = a.charCodeAt(g);
            128 > m ? e[f++] = m : (2048 > m ? e[f++] = m >> 6 | 192 : (55296 == (m & 64512) && g + 1 < a.length && 56320 == (a.charCodeAt(g + 1) & 64512) ? (m = 65536 + ((m & 1023) << 10) + (a.charCodeAt(++g) & 1023),
            e[f++] = m >> 18 | 240,
            e[f++] = m >> 12 & 63 | 128) : e[f++] = m >> 12 | 224,
            e[f++] = m >> 6 & 63 | 128),
            e[f++] = m & 63 | 128)
        }
        a = b;
        for (f = 0; f < e.length; f++) a += e[f],
        a = RL(a, $b);
        a = RL(a, Zb);
        a ^= b1 || 0;
        0 > a && (a = (a & 2147483647) + 2147483648);
        a %= 1E6;
        return a.toString() + jd + (a ^ b)
    };

    function RL(a, b) {
        var t = "a";
        var Yb = "+";
        for (var c = 0; c < b.length - 2; c += 3) {
            var d = b.charAt(c + 2),
            d = d >= t ? d.charCodeAt(0) - 87 : Number(d),
            d = b.charAt(c + 1) == Yb ? a >>> d: a << d;
            a = b.charAt(c) == Yb ? a + d & 4294967295 : a ^ d
        }
        return a
    }
    """)

    def getTk(self, text):
        return self.ctx.call("TL", text)



def open_url(url):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
    req = urllib2.Request(url=url, headers=headers)
    response = urllib2.urlopen(req)
    data = response.read().decode('utf-8')
    return data


def translate(content):
    #print "content: ", content
    js = Py4Js()
    tk = js.getTk(content)

    texts = ""
    content = urllib2.quote(content)
    url = "http://translate.google.cn/translate_a/single?client=t" \
          "&sl=EN&tl=zh-CN&hl=zh-CNdt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca" \
          "&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&clearbtn=1&otf=1&pc=1" \
          "&srcrom=0&ssel=0&tsel=0&kc=2&tk=%s&q=%s" % (tk, content)

    result = open_url(url)
    re = json.loads(result)
    str = ""
    for i in re[0]:
        if i[0]:
            str += i[0]
            #print " ========>", i[0]
    return str


if __name__ == "__main__":
    text="您好"
    texts=text.split('.')
    results=''
    for i in range(len(texts)):
        try:
            results = results + translate(str(texts[i]))
        except Exception as e:
            print e
    print results

PDF頁面生成

PDF頁面生成採用的是reportlab方案,支援中文的話需要下載字型檔案。PDF對換行處理也很麻煩,網上看了很多換行的方案,覺得都太複雜,還不如自己來實現換行,先用尋找換行符分割,然後每行42個字元,每drawString一行,座標就就往下移即可,實現最簡單的中文自動換行功能。

import StringIO
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont 
pdfmetrics.registerFont(TTFont('msyh', 'Msyh.ttf')) 

filename = "test.pdf"
pdf = PdfFileReader(open(filename, "rb"))
pdf_output = PdfFileWriter()
num = pdf.getNumPages()
for i in range(0, num):
    print "do page: ", i
    page = pdf.getPage(i)
    pdf_output.addPage(page)
    cnstr = dopage(page)

    packet = StringIO.StringIO()
    can = canvas.Canvas(packet, pagesize=letter)
    can.setFont("msyh", 8)
    can.setFontSize(12)
    cnstrarr = cnstr.split("\n")
    top = 750
    for strline in cnstrarr:
        for i in range(0, int(len(strline)/42)+1):
            can.drawString(50, top, strline[i*42:i*42+42])
            top -= 20
        print "strline: ", strline
    can.showPage()
    can.save()
    packet.seek(0)
    new_pdf = PdfFileReader(packet)
    pdf_output.addPage(new_pdf.getPage(0))

pdf_output.write(open("out.pdf", 'wb'))
print "Success!!"