Python Code

PDF 파일 Concat

Kimhj 2023. 9. 20. 17:50
  • PDF read 할 수 있는 패키지 설치
pip install PyPDF2

Collecting PyPDF2
  Downloading pypdf2-3.0.1-py3-none-any.whl (232 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 232.6/232.6 kB 5.2 MB/s eta 0:00:00a 0:00:01
Installing collected packages: PyPDF2
Successfully installed PyPDF2-3.0.1


pip install PdfMerger

Requirement already satisfied: PdfMerger in ./anaconda3/lib/python3.10/site-packages (0.5.0)
Requirement already satisfied: PyPDF2 in ./anaconda3/lib/python3.10/site-packages (from PdfMerger) (3.0.1)

 

  • 한 폴더에 PDF 파일 몰아넣고, Read 해서 concat
from PyPDF2 import PdfFileMerger, PdfMerger
# import PdfMerger
import os, tqdm

file_path = '/Users/hjkim/Desktop/integrate_pdf/'
pdf_list = [x for x in os.listdir(file_path) if x[-3:]=='pdf']
print('PDF FILES: {}'.format(len(pdf_list)))
merger = PdfMerger()

for pdf in tqdm.tqdm(pdf_list):
    merger.append(file_path + pdf)

merger.write(file_path + "integrated_file.pdf")
merger.close()