Python

PDF 파일 합치기 (Merge PDF Files)

AndrewJ 2025. 3. 10. 15:12
반응형
작성일: 2025년 3월 10일

 

 

현재 폴더에 있는 모든 PDF 파일을 1개의 PDF 파일로 합치고 싶다면, 아래의 Python script를 실행하면 된다.

 

## File name: main.py

from PyPDF2 import PdfMerger

import os
files = os.listdir('./')

def merge_pdf_files():
    merger = PdfMerger()
    pdf_files = [x for x in files if x.endswith(".pdf")]
    [merger.append(pdf) for pdf in pdf_files]
    with open("merged_pdf_all.pdf", "wb") as new_file:
        merger.write(new_file)

if __name__ == "__main__":
    merge_pdf_files()

 

 

위 Python script를 아래와 같이 실행한다.

(만약 PyPDF2 패키지가 아직 설치되지 않았다면, PyPDF2 패키지를 먼저 설치하고 python script를 실행할 것!)

$ pip3 install PyPDF2

$ python3 main.py