Awesome-Python-Scripts/images2pdf/imges2pdf.py

28 lines
929 B
Python
Raw Permalink Normal View History

2022-06-23 00:43:49 +00:00
import PyPDF2
from os import path
2018-10-02 21:25:05 +00:00
import sys
2022-06-23 00:43:49 +00:00
def File_existance_checker(filePath):
if path.isfile(filePath):
return filePath
else:
print("[-] Provide a valid File")
sys.exit(1)
pdf_stored_path=input("Enter the name of you pdf file (please use backslash when typing in directory path):")
2018-10-02 21:25:05 +00:00
2022-06-23 00:43:49 +00:00
textFile_stored_path=path.join(path.dirname(pdf_stored_path),path.basename(pdf_stored_path).replace(".pdf",".txt"))
pdf_stored_path=File_existance_checker(pdf_stored_path)
2018-10-02 21:25:05 +00:00
2022-06-23 00:43:49 +00:00
print(textFile_stored_path)
2018-10-02 21:25:05 +00:00
2022-06-23 00:43:49 +00:00
with open(pdf_stored_path,'rb') as pdf_object:
pdf_read=PyPDF2.PdfFileReader(pdf_object)
2018-10-02 21:25:05 +00:00
2022-06-23 00:43:49 +00:00
pdf_pages=pdf_read.numPages
for i in range(pdf_pages):
page_object=pdf_read.getPage(i)
with open(textFile_stored_path,'a+') as f:
f.write((page_object.extract_text()))
print(f"[+] Pdf Text has been extracted and written to {path.basename(textFile_stored_path)}")