0

I have created a function for a PDF page Splitter. I can choose a PDF file, save the path to pdfOne and after that I can choose what pages I want to split. The problem is that split pages goes in the same path as the original PDF. I don't want that, I want to send the split pages to a different folder-path.

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)
CDspace
  • 2,639
  • 18
  • 30
  • 36
TLSK
  • 275
  • 1
  • 6
  • 25

1 Answers1

1

use os.path.join to construct a path to the destination file:

import os.path

[...]

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

You can use ..for the parent directory, / for the root directory

tiwo
  • 3,238
  • 1
  • 20
  • 33