Created
July 26, 2023 12:55
-
-
Save asahicantu/b6f6a99c0bf3fc4cae2fb6c6422bc5ef to your computer and use it in GitHub Desktop.
This script merges all pdf files in a folder into one pdf file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## This script merges all pdf files in a folder into one pdf file | |
## Usage: python pdfmerger.py <path to folder with pdf files> <path to output file> | |
## Example: python pdfmerger.py ./pdfs ./merged.pdf | |
## Author: @asahicantu | |
## Date: 2021-10-07 | |
## Version: 1.0 | |
## License: MIT | |
## Requirements: pypdf | |
## Tested on: Windows 10 | |
## How to use: | |
## 1. Install pypdf: pip install pypdf | |
## 2. Run the script: python pdfmerger.py <path to folder with pdf files> <path to output file> | |
## 3. The script will merge all pdf files in the folder into one pdf file | |
## 4. The script will create the output file if it doesn't exist | |
## 5. The script will overwrite the output file if it exists | |
## 6. The script will create the output file if it doesn't exist | |
import os | |
import sys | |
from pypdf import PdfReader, PdfWriter # pip instal pypdf | |
pdf_path = sys.argv[1] # sys.argv[0] is the script name | |
out_path = sys.argv[2] | |
print(f"Reading path {pdf_path}") | |
pdf_files = os.listdir(pdf_path) | |
pdf_writer = PdfWriter() | |
for pdf_file in pdf_files: | |
pdf_file = os.path.join(pdf_path, pdf_file) | |
print(f"Opening {pdf_file}...") | |
pdf_reader = PdfReader(pdf_file) | |
print(f"file contains {len(pdf_reader.pages)}") | |
print(f"merging pages...") | |
for page in pdf_reader.pages: | |
pdf_writer.add_page(page) | |
print(f"done") | |
print(f"writing file...") | |
pdf_writer.write(out_path) | |
print(f"done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment