1

I am trying to extract the 6th page from each pdf in a folder in my directory and place each page in a different folder with new file names. I used the following source: Extract specific pages of PDF and save it with Python and Extract pdf for most of my code but I get an error that the file is a string

from PyPDF2 import PdfFileReader, PdfFileWriter
from pathlib import Path
import glob
file_path = 'mypath\*pdf'
new_filepath='mynewfile_path'
file_path=glob.glob(file_path)
i=1
for file in file_path:   
   input_pdf = PdfFileReader(file)
   first_page = input_pdf.getPage(5)
   pdf_writer = PdfFileWriter()
   i=str(i)  
   with open(i+ subset.pdf'.format(new_filepath), 'wb') as f:
       pdf_writer.write(f)
       f.close()
  i=int(i)
  i+=1
Monica
  • 11
  • 3
  • please format your code properly – m1el Sep 27 '21 at 17:08
  • This would be better handled by a shell script or batch file running standalone tools like `cpdf` and `pdftk`. You don't really need Python for this. – Tim Roberts Sep 27 '21 at 17:21
  • You have not added your page to the `pdf_writer`. And, of course, the line attempting to open the new file has two or three errors. What, exactly, do you want the output file names to be? – Tim Roberts Sep 27 '21 at 17:23
  • file names to be 1subset.pdf, 2subset.pdf, etc. – Monica Sep 27 '21 at 17:27

0 Answers0