PDF Generator, merging 3 or more pdf files

57 views Asked by At

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()```
0

There are 0 answers