반응형
PDF 에서 일부 페이지를 추출해야 할 일이 종종 있다.
특정 프로그램을 깔기에는 귀찮고 온라인 사이트에서 해결하자니 개인정보가 걱정이 될 때가 있다.
그래서 Python 으로 해결할 수 있는 방법을 찾았다.
파이썬 파일 접근을 위해 PyPDF2 라는 패키지가 사용된다.
pip install PyPDF2
특정 파일
특정 페이지
특정 파일명
으로 저장할 수 있는 코드이다.
from PyPDF2 import PdfFileReader, PdfFileWriter
#파일명 받기
path = str(input("What is the file name?-ex.test.pdf\n"))
pdfReader = PdfFileReader(path, "rb")
newpdfWriter = PdfFileWriter()
#원하는 페이지 번호
while True:
pgnum = int(input("Which page do you want to extract?-if finished please type 0\n"))
if pgnum==0:
break
newpdfWriter.addPage(pdfReader.getPage(pgnum))
print("Added page number "+str(pgnum)+"\n")
#저장 이름 설정
newfile = str(input("Please type the new file name - ex.test\n"))
newpdfWriter.write(open("./"+newfile+".pdf", "wb"))
print("Saved successfully")
아래의 깃허브 레퍼지토리에서도 확인할 수 있다.
반응형