PDF文档合并器

基于pyPdf模块的PDF文档合并器

有一阵子没有写博客了……
因为要考英语,所以前一段时间几乎把所有手头上的工作都放了下来,大概有两个月没有碰电脑了吧。说实话,我以前真的不敢相信两个月不碰电脑是什么感觉。
在俺闭关的时候亲爱的网络安全老师布置了一个非常“有趣”的实验,并且必须要去交一份所谓的报告。既然非得写,我倒是也没敷衍,熬了一个通宵用Markdown搞定了报告的正文部分。但给老师交报告总要有个像样的封面吧,于是就有用其他软件生成了一个封面的pdf。
然后怎么把这两个pdf合并在一起就成为了主要的问题(分开打印再钉起来的方法我还真想过~~)幸运的是前人栽树,后人乘凉。python有一个pyPdf的模块专门用来操作pdf文件。于是就用这个模块写了一个小工具。
import sys
from pyPdf import PdfFileWriter, PdfFileReader

def mergePdfFiles(outputFile, inputFiles):
        output = PdfFileWriter()
        for inputFile in inputFiles:
                print 'Adding file' + inputFile
                input = PdfFileReader(file(inputFile, "rb"))
                for page in input.pages:
                        output.addPage(page)

        print 'All files added'

        #From writer to file
        outputStream = file(outputFile, "wb")
        output.write(outputStream)
        outputStream.close()

if __name__ == '__main__':
        print 'Merging Pdf Files...'
        mergePdfFiles("union.pdf", ["1.pdf","2.pdf"])
        print 'Merge Completed'
使用的时候只需把需要合并的文件放到与.py文件同一目录下,改名为1.pdf、2.pdf……即可生成合并后的union.py文件。

那啥,一天后我手贱又写了个界面,累死本宝宝了
#coding=utf-8

import sys
import os
import codecs
from pyPdf import PdfFileWriter, PdfFileReader
from PyQt4 import QtGui ,Qt ,QtCore

global filename1
global filename2
filename1 = " "
filename2 = " "

def cur_file_dir():
     #获取脚本路径
     path = sys.path[0]
     if os.path.isdir(path):
         return path
     elif os.path.isfile(path):
         return os.path.dirname(path)

class Window( QtGui.QWidget ):
    def __init__( self ):
        super( Window, self ).__init__()
        self.setWindowTitle( "Pdf Tool" )
        self.setFixedSize(180, 80)

        gridlayout = QtGui.QGridLayout()

        button1 = QtGui.QPushButton( "Part1" )
        self.connect( button1, QtCore.SIGNAL( 'clicked()' ), self.OnButton1 )
        gridlayout.addWidget( button1, 0, 0)

        button2 = QtGui.QPushButton( "part2" )
        self.connect( button2, QtCore.SIGNAL( 'clicked()' ), self.OnButton2 )
        gridlayout.addWidget( button2, 0, 1)

        button3 = QtGui.QPushButton( "Merge" )
        self.connect( button3, QtCore.SIGNAL( 'clicked()' ), self.mergePdfFiles )
        gridlayout.addWidget( button3, 1, 0, 1, 2 )

        self.setLayout( gridlayout )


    def OnButton1(self):
        global filename1
        filename1 = self.on_openfile_clicked()
        QtGui.QMessageBox.about( self, 'Part1', "Get: %s" % filename1 )
        print (filename1)

    def OnButton2(self):
        global filename2
        filename2 = self.on_openfile_clicked()
        QtGui.QMessageBox.about( self, 'Part2', "Get: %s" %filename2 )
        print (filename2)

    def mergePdfFiles(self):
        global filename1
        global filename2

        output = PdfFileWriter()

        input = PdfFileReader(file(filename1, "rb"))
        for page in input.pages:
            output.addPage(page)

        input = PdfFileReader(file(filename2, "rb"))
        for page in input.pages:
            output.addPage(page)   

        outputStream = file("union.pdf", "wb")
        output.write(outputStream)
        outputStream.close()
        QtGui.QMessageBox.about( self, 'Merge', "File Merged!path:%s\union.pdf" % cur_file_dir())
        print ("ok %s" % cur_file_dir())

    def on_openfile_clicked(self):
        dlg = QtGui.QFileDialog(self)  
        self.filename = dlg.getOpenFileName()  
        from os.path import isfile  
        if isfile(self.filename):
           return self.filename


app = QtGui.QApplication( sys.argv )
win = Window()
win.show()
app.exec_()
最后附下载地址:http://download.csdn.net/detail/spirtsong/9379692
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值