I have a document library which consists of several hundred PDF Documents. I am attempting to export the first page of each PDF document. Below is my script which extracts the page. It saves each page as an individual PDF. However, the files which are exported seem to be exporting in unreadable or damaged format.
Is there something missing from my script?
import os
from PyPDF2 import PdfReader, PdfWriter
# get the file names in the directory
input_directory = "Fund_Docs_Sample"
entries = os.listdir(input_directory)
output_directory = "First Pages"
outputs = os.listdir(output_directory)
for output_file_name in entries:
reader = PdfReader(input_directory + "/" + output_file_name)
page = reader.pages[0]
first_page = "\n" + page.extract_text() + "\n"
with open(output_file_name, "wb") as outputStream:
pdf_writer = PdfWriter(output_file_name + first_page)