-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergePDFs.py
More file actions
34 lines (26 loc) · 1.02 KB
/
Copy pathMergePDFs.py
File metadata and controls
34 lines (26 loc) · 1.02 KB
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
26
27
28
29
30
31
32
33
34
#! python3
# Get the libraries we need for OS handling
import os, sys
# Get the PDF libararies (needs to be installed first with `pip3 install PyPDF2`)
import PyPDF2
from PyPDF2 import PdfFileMerger
# Input path from the first argument passed to the command line. Index 0 is the script itself so need to start at 1
inputPath = sys.argv[1]
# Output path is the second argument passed to the script,index 2.
outputPath = sys.argv[2]
# Get all the PDF filenames in the path from the CLI arguments.
pdfFiles = []
for filename in os.listdir(inputPath):
if filename.endswith('.pdf'):
pdfFiles.append(inputPath + '/' + filename)
# Sort the PDFs then reverse the sort. The output prepends the date and we want the newest files at the top of the merged PDF.
pdfFiles.sort()
pdfFiles.reverse()
# Loop through all the PDF files.
merger = PdfFileMerger()
# Merging them
for pdf in pdfFiles:
merger.append(pdf)
# Write the file to the path from above and name it.
merger.write(outputPath)
merger.close()