<GPT의 주니어 Human 비서 요한 👨💻>
GPT와 함께할 첫 번째 프로젝트는 GPT의 도움을 받아 머리 속에 논문을 효율적으로 때려 넣을 수 있는 프로세스를 만들어 보려고 한다. 먼저 GPT에게 논문의 내용을 알려주기 위해 PDF에서 텍스트를 뽑아내는 python 프로그램을 만들어 보자. 물론 코드는 GPT가 짜고, Human 비서는 시키는데로 디버깅만 한다.
(Prompt #1) I would like to write a python program that reads a pdf paper and saves the text inside it as a txt file. How can I write it?
PyPDF2라는 라이브러리를 알려주고 코드를 던져주었으나 다양한 error가 쏟아진다. "Please fix following error (error msg)" 형식으로 디버깅을 하다보니, PyPDF2 3.0.0에서 함수명들이 바뀌어서 발생하는 error 같다.
(Prompt #2) Could you please rewrite the code using pypdf2 3.0.0?
정중하게 다시 요청을 하니 이제 잘 동작한다. 논문 PDF 파일을 읽어 각 페이지의 텍스트를 txt 파일로 출력해준다. 그림이나 수식 등이 섞여 있는 경우 어질어질하게 정리되는 단점이 있지만 GPT님에게 던져주기 한결 수월해졌다.
import PyPDF2
# Open the pdf file
pdf_file = open('Attention Is All You Need (2017).pdf', 'rb')
# Create a pdf reader object
pdf_reader = PyPDF2.PdfReader(pdf_file)
# Loop through each page in the pdf
for page_num, page in enumerate(pdf_reader.pages):
# Extract the text from the page
text = page.extract_text()
# Write the text to a txt file
with open(f'page_{page_num}.txt', 'w', encoding='utf-8') as text_file:
text_file.write(text)
# Close the pdf file
pdf_file.close()
✅ 총 작업 소요 시간: 17분 08초