使用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!!"
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值