TLSK TLSK - 1 year ago 165
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()

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

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

output = PdfFileWriter()
with open("page%s.pdf" % i,"wb") as 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:

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