0

I'm trying to extend the code i wrote for 2 files to make it work with an undetermined number of files.
The code works but the pdf created is empty.
I know the problem is in my list which is not a Pdf4 file writer object but I can't figure it out.
Who can help me?

import tkinter as tk
from tkinter.filedialog import askopenfilename
import PyPDF4

def extract_page(pdffile):
    for numPage in range(pdffile.numPages):
        page = pdffile.getPage(numPage)
        newPdf.addPage(page)
        
nb_files = int(input("How many Pdf files do you want to merge?"))
root = tk.Tk()
root.withdraw()

listPdf = []
for i in range(nb_files):
    file = askopenfilename()
    pdffile = PyPDF4.PdfFileReader(file)
    pdfpages = extract_page(pdffile)
    listPdf.append(pdfpages)
    
listPdf = PyPDF4.PdfFileWriter()    
pdfComb = open('C:/Users/***/Desktop/newPdf.pdf', 'wb')
listPdf.write(pdfComb)
pdfComb.close()```
  • I know it's a bad idea, maybe I could set a maximum number of files allowed, I just wanted it to look cleaner not just a bunch of lines filled with file1, file2,... – Christopher Pring Oct 25 '22 at 07:31
  • Use `pypdf` instead of PyPDF2/PyPDF3/PyPDF4. I am the maintainer of pypdf and PyPDF2. We improved pypdf a lot in 2022. – Martin Thoma Dec 26 '22 at 08:45

0 Answers0