ANALYSIS TOOL BOX

File Management

CreateCopyOfPDF

Copy PDF file or extract specific page range to a new PDF document.

filesioexportimport

Introduction

A 200-page report often only needs to share five of those pages, and manually re-exporting a subset from a PDF viewer is tedious and easy to get wrong. CreateCopyOfPDF handles this programmatically with PyPDF2: pass a page range and it writes a new PDF containing just those pages (or the whole document, if you leave the range unset), preserving formatting, fonts, images, and metadata exactly as they appear in the source.

Teaching Note

PDF copying and page extraction is essential for:

  • Splitting large PDFs into smaller, manageable documents
  • Extracting specific chapters or sections from reports
  • Creating document excerpts for sharing or review
  • Removing unwanted pages while preserving others
  • Preparing presentation handouts from slide decks
  • Archiving specific portions of lengthy documents
  • Creating customized document compilations
  • Reducing file sizes by extracting relevant content only

Parameters

ParameterTypeDefaultDescription
input_filerequiredAbsolute or relative path to the source PDF file to copy or extract from. The file must exist and be a valid PDF document.
output_filerequiredPath where the output PDF file will be created. Include the desired filename with .pdf extension. Parent directories will not be created automatically.
start_pageNoneFirst page number to include in the output (1-indexed). If None, starts from the first page of the document. Defaults to None.
end_pageNoneLast page number to include in the output (1-indexed, inclusive). If None, includes through the last page of the document. Defaults to None.

Returns

None — the function writes the selected pages to a new PDF at output_file.

Example

python
from analysistoolbox.file_management import CreateCopyOfPDF

# Extract a specific page range
CreateCopyOfPDF(
    input_file='full_document.pdf',
    output_file='chapter_3.pdf',
    start_page=25,
    end_page=45
)
# Extracts pages 25-45 (inclusive) to a new PDF