I want to make an script that read all the pdf
files in directory, copy the second page of each one and write it in one output pdf (with all the seconds pages).
I have already wrote a code but it give me a pdf with blank pages. And that is really strange because I have another code which take the second page of each pdf and make a new pdf for each second page, and that code works. I think my problem may be related with the addPage()
.
I am using PyPDF2 library to use the pdf files.
import pathlib
from PyPDF2 import PdfFileReader, PdfFileWriter
files_list = [file for file in pathlib.Path(__file__).parent.iterdir() if (file.is_file() and not str(file).endswith(".py"))]
total = len(files_list)
writer = PdfFileWriter()
for file in files_list:
with open(file, 'rb') as infile:
reader = PdfFileReader(infile)
reader.decrypt("")
writer.addPage(reader.getPage(1))
with open('Output.pdf', 'wb') as outfile:
writer.write(outfile)
print('Done.')