Python PDF将页面拆分为特定路径

时间:2017-04-30 21:02:40

标签: python python-2.7 wxpython pypdf

我为PDF页面分割器创建了一个功能。我可以选择一个PDF文件,保存pdfOne的路径,之后我可以选择要拆分的页面。问题是拆分页面与原始PDF的路径相同。我不想要,我想将拆分页面发送到不同的文件夹路径。

def onFindPage(self, event):

    pdfOne = self.pdfOne.GetValue()
    spgcf=int(self.spgcfrom.GetValue())
    spgcu=int(self.spgcuntil.GetValue())

    inputpdfpdfOne = pyPdf.PdfFileReader(file(pdfOne, "rb"))

    for i in xrange((spgcf-1),spgcu):

        output = PdfFileWriter()            
        output.addPage(inputpdfpdfOne.getPage(i))
        with open("page%s.pdf" % i,"wb") as outputStream:
           output.write(outputStream)

1 个答案:

答案 0 :(得分:1)

使用os.path.join构建目标文件的路径:

import os.path

[...]

outputFilename = os.path.join(destination_directory, "page%s.pdf" % i)
with open (outputFilename, "wb") as outputStream:
    output.write(outputStream)

您可以将..用于父目录,/用于根目录