-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf_merger.py
47 lines (31 loc) · 1.04 KB
/
pdf_merger.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
import PyPDF2
import sys
from sys import argv
import os
import re
def check_string(s):
is_pdf = s.lower().endswith('.pdf')
# Check for a digit
has_number = re.search(r'\d', s) is not None
# Check for 'lesson' in any case
has_lesson = 'lecture' in s.lower()
return is_pdf and has_number and has_lesson
def get_lesson_number(s):
digits = re.findall(r'\d', s)
# Join the digits to form a number and convert to integer
number = int(''.join(digits))
return number
def get_relevant_files():
all_files = os.listdir(os.curdir)
pdf_files = [file for file in all_files if check_string(file)]
pdf_files = sorted(pdf_files, key=get_lesson_number)
return pdf_files
def merge_pdfs(paths, output):
pdf_merger = PyPDF2.PdfMerger()
for path in paths:
pdf_merger.append(path)
with open(output, 'wb') as fileobj:
pdf_merger.write(fileobj)
pdf_merger.close()
if __name__ == "__main__":
merge_pdfs(get_relevant_files(), 'final_exam.pdf') # 'merged.pdf' is the output file