Created
June 23, 2021 08:20
-
-
Save ChlorUpload/8e93c4481a8c0335925e761198c35e93 to your computer and use it in GitHub Desktop.
merge.py
This file contains hidden or 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
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