from PyPDF2 import PdfReader, PdfWriter def extract_pdf_section(pdf_path, start_page, end_page, output_path): # Open the PDF file pdf = PdfReader(open(pdf_path, 'rb')) # Create a PdfWriter object writer = PdfWriter() # Add pages to the writer for page_num in range(start_page - 1, end_page): writer.add_page(pdf.pages[page_num]) # Write the extracted section to a new file with open(output_path, 'wb') as output_file: writer.write(output_file) return output_path