-
Notifications
You must be signed in to change notification settings - Fork 0
/
PDF-to-Text-A.py
32 lines (22 loc) · 1.28 KB
/
PDF-to-Text-A.py
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
import os
# Directory where the PDF files are located
pdf_directory = '/path/to/pdf/files' # Replace with the actual directory path
# Output directory for text files
output_directory = '/path/to/output/directory' # Replace with the desired output directory path
# Create the output directory if it doesn't exist
os.makedirs(output_directory, exist_ok=True)
# List PDF files in the specified directory
pdf_files = [pdf_file_name for pdf_file_name in os.listdir(pdf_directory) if pdf_file_name.endswith('.pdf')]
for pdf_file_name in pdf_files:
pdf_file_path = os.path.join(pdf_directory, pdf_file_name)
pdf_reader = PyPDF2.PdfReader(pdf_file_path)
# Create the output text file path by changing the extension to .txt
output_text_file_path = os.path.join(output_directory, os.path.splitext(pdf_file_name)[0] + '.txt')
with open(output_text_file_path, 'w', encoding='utf-8') as text_file:
for page_num, page in enumerate(pdf_reader.pages, start=1):
page_text = page.extract_text()
# Write extracted text to the output text file
text_file.write(page_text)
print(f"Text extracted from {pdf_file_name} and saved to {output_text_file_path}")
print(f"Text extracted and saved to individual text files in {output_directory}")