Home > Software design >  i cant make loop code in merge PDF file by python
i cant make loop code in merge PDF file by python

Time:10-31

from PyPDF2 import PdfFileReader, PdfFileWriter, PdfFileMerger

merger =PdfFileMerger()

merger.append(PdfFileReader(open("seperated1 PDF file556.pdf", 'rb')))
merger.append(PdfFileReader(open("seperated1 PDF file557.pdf", 'rb')))
merger.append(PdfFileReader(open("seperated1 PDF file558.pdf", 'rb')))
merger.append(PdfFileReader(open("seperated1 PDF file559.pdf", 'rb')))
merger.append(PdfFileReader(open("seperated1 PDF file560.pdf", 'rb')))


merger.write("output01.pdf")

I want to merge All Pdf file but i cant make loop

so i input one by one like that code

so how i make loop code and merge that all pdf file

*i have "seperated1 PDF file1~660.pdf"

Captured vscode

CodePudding user response:

Did you mean this?

from PyPDF2 import PdfFileReader, PdfFileWriter, PdfFileMerger

merger = PdfFileMerger()

for n in range(1,601):
    merger.append(PdfFileReader(open(f"seperated1 PDF file{n}.pdf", 'rb')))

merger.write("output01.pdf")
  • Related