Skip to content

CPME/pdf_merge

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 

Repository files navigation

PDF Merger Script

This Python script merges all PDF files found within a specified directory into a single output PDF file using command-line arguments for configuration.

Features

  • Merges multiple PDF files into one.
  • Sorts input PDF files alphabetically (A-Z) by default.
  • Option to sort in reverse alphabetical order (Z-A).
  • Configurable input directory, output directory, and output filename via command-line arguments.
  • Provides default values for paths and filename if not specified.
  • Includes error handling for file operations.
  • Uses the pypdf library.

Prerequisites

  • Python 3.x
  • pypdf library

Installation

  1. Clone the repository or download the script.
  2. Install the required library:
    pip install pypdf

Usage

Navigate to the directory containing the script in your terminal and run it using Python. You can specify the input directory, output directory, and output filename using command-line arguments.

Command-Line Arguments

  • -input <path>: Specifies the directory containing the PDF files to merge.
    • Default: ./pdfs
  • -output <path>: Specifies the directory where the merged PDF file will be saved. The directory will be created if it doesn't exist.
    • Default: ./output
  • -name <filename>: Specifies the name for the final merged PDF file.
    • Default: merged_document.pdf
  • -r, --reverse: Sorts the input PDF files in reverse alphabetical order (Z-A). If omitted, files are sorted alphabetically (A-Z).
  • -h, --help: Displays the help message showing available options and exits.

Examples

  1. Merge PDFs using default settings: (Assumes PDFs are in ./pdfs, saves to ./output/merged_document.pdf, sorts A-Z)

    python pdf_merge.py
  2. Specify input/output directories and filename:

    python pdf_merge.py -input "/path/to/your/pdfs" -output "/path/to/save" -name "final_merged.pdf"
  3. Specify paths and sort in reverse order (Z-A):

    python pdf_merge.py -input "./my_pdfs" -output "./merged_files" -name "report_reversed.pdf" -r

    or

    python pdf_merge.py -input "./my_pdfs" -output "./merged_files" -name "report_reversed.pdf" --reverse
  4. Display help:

    python pdf_merge.py --help

Output

The script will print:

  • The directory it's searching for PDFs.
  • The number of PDF files found.
  • The sorting order being used (A-Z or Z-A).
  • The name of each PDF file as it's being added to the merge process.
  • A final confirmation message indicating the save location of the merged PDF, or error messages if issues occur.

About

A simple script to merge pdfs.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages