Skip to content

Instantly share code, notes, and snippets.

@ChlorUpload
Created June 23, 2021 08:20
Show Gist options
  • Save ChlorUpload/8e93c4481a8c0335925e761198c35e93 to your computer and use it in GitHub Desktop.
Save ChlorUpload/8e93c4481a8c0335925e761198c35e93 to your computer and use it in GitHub Desktop.
merge.py
import os
import re
import sys
from PyPDF2 import PdfFileMerger
import re
def extractNum(string):
nums = re.findall("\d+", string)
if len(nums) == 0:
return 0
return int(nums[0])
dirNames = [d for d in os.listdir("./") if os.path.isdir(d)]
dirNames.sort(key=extractNum)
studentPdfListDict = {}
for d in dirNames:
files = [f for f in os.listdir(d) if f.endswith('.pdf')]
for f in files:
if f not in studentPdfListDict:
studentPdfListDict[f] = []
studentPdfListDict[f].append(os.path.join(d, f))
os.makedirs('./merged', exist_ok=True)
for (student, pdfList) in studentPdfListDict.items():
merge = PdfFileMerger()
print(f'{student} 학생 {len(pdfList)}개 파일 병합중...')
for pdf in pdfList:
merge.append(pdf)
merge.write(os.path.join('./merged', student))
merge.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment