-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPdfToText.py
More file actions
32 lines (23 loc) · 778 Bytes
/
Copy pathPdfToText.py
File metadata and controls
32 lines (23 loc) · 778 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import PyPDF2
def pdf_to_text(pdf_path):
try:
with open(pdf_path, 'rb') as file:
reader = PyPDF2.PdfReader(file)
text = ''
for page in reader.pages:
text += page.extract_text()
return text
except FileNotFoundError:
return "The specified file was not found."
def main():
path_to_pdf = input("Enter pdf file: ").strip()
text = pdf_to_text(path_to_pdf)
if text:
text_file = path_to_pdf.replace('.pdf', '_text.txt')
with open(text_file, 'w', encoding='utf-8') as file:
file.write(text)
print(f"Successfully extracted and saved to {text_file}")
else:
print("No text was found in pdf")
if __name__ == "__main__":
main()