TLSK TLSK - 1 year ago 157
Python Question

Python PDF split pages to specific path

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)

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download