0

I am trying to split a large pdf based on the list of names and list of pages. For example first name has three pages, second has one page, the third has five pages and so on. I created the following script and it is not working correctly. For instance when the pdf splits the first one is correct three pages, the second has four pages it should only have one page, the third one has nine pages it should have five pages and so on the last pdf has all pages it should have one page. What am I missing?

for arry, name in zip(pglist, deptNames5):
    for page in arry:
        pagenumber = pdfDoc.getPage(page)
        NewPdf.addPage(pagenumber)
        with open(name + b'.pdf', 'wb') as f:
            NewPdf.write(f)
1
  • NewPdf.addPage(pagenumber) -- you just keep adding pages there. You should rewind it once the output was written.
    – taras
    Apr 29, 2016 at 19:10

1 Answer 1

1

I think the correct version of this code should be

for arry, name in zip(pglist, deptNames5):
    NewPdf = PdfFileWriter()
    for page in arry:
        pagenumber = pdfDoc.getPage(page)
        NewPdf.addPage(pagenumber)
    with open(name + b'.pdf', 'wb') as f:
        NewPdf.write(f)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.