Post

PDF Tool

PDF Tool

Merge PDF

I dont want to upload my files

Usage

put all files and script into folder

1
2
3
4
python3 -m venv PDF_ENV
source ./PDF_ENV/bin/activate
pip install pypdf "pypdf[crypto]"
python3 merge.py

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from pypdf import PdfWriter
import os
import glob

# List of files to merge
pdfwriter = PdfWriter()
pdf_files = glob.glob("*.pdf")

# optional file sort
pdf_files.sort(key=str.lower)

output_filename = "merged.pdf"

n = 0
for filename in pdf_files:
    n += 1
    pdfwriter.append(filename)
    print(f"{n} appending {filename}")

with open(output_filename, "wb") as output_file:
    pdfwriter.write(output_file)

pdfwriter.close()

print(f"{n} files output to {output_filename}")
This post is licensed under CC BY 4.0 by the author.