Converting all PDF files in the current directory to TXT files can be accomplished using various methods depending on your operating system and preferred tools. Below are several approaches you can choose from:
Method 1: Using pdftotext Command-Line Tool (Recommended for Linux and macOS)
pdftotext is a command-line utility that's part of the Poppler package. It's efficient and widely used for extracting text from PDF files.
Step 1: Install pdftotext
-
For Linux (Debian/Ubuntu-based):
sudo apt-get update
sudo apt-get install poppler-utils
-
For macOS (using Homebrew):
brew install poppler
-
For Windows:
- You can download precompiled binaries from Poppler for Windows.
- After downloading, add the
bin folder to your system's PATH environment variable to access pdftotext from the command prompt.
Step 2: Navigate to Your Directory
Open your terminal or command prompt and navigate to the directory containing the PDF files:
cd /path/to/your/directory
Step 3: Batch Convert PDFs to TXT
Run the following command to convert all PDF files in the current directory to TXT:
-
For Linux and macOS:
for file in *.pdf; do
pdftotext "$file" "${file%.pdf}.txt"
done
-
For Windows (Using Command Prompt):
for %f in (*.pdf) do pdftotext "%f" "%~nf.txt"
-
For Windows (Using PowerShell):
Get-ChildItem *.pdf | ForEach-Object {
pdftotext $_.FullName "$($_.BaseName).txt"
}
Explanation:
*.pdf: Selects all PDF files in the directory.
pdftotext "$file" "${file%.pdf}.txt": Converts each PDF to a TXT file with the same base name.
Method 2: Using Python with PyPDF2
If you prefer using Python, you can utilize the PyPDF2 library to extract text from PDFs. Note that PyPDF2 may not handle complex PDFs as effectively as pdftotext.
Step 1: Install PyPDF2
pip install PyPDF2
Step 2: Create a Python Script
Create a new Python script, e.g., pdf_to_txt.py, and add the following code:
import os
from PyPDF2 import PdfReader
def pdf_to_txt(pdf_path, txt_path):
reader = PdfReader(pdf_path)
with open(txt_path, 'w', encoding='utf-8') as f:
for page_num, page in enumerate(reader.pages):
text = page.extract_text()
if text:
f.write(text)
else:
f.write(f"\n--- No text found on page {page_num + 1} ---\n")
def main():
current_dir = os.getcwd()
for filename in os.listdir(current_dir):
if filename.lower().endswith('.pdf'):
pdf_path = os.path.join(current_dir, filename)
txt_filename = os.path.splitext(filename)[0] + '.txt'
txt_path = os.path.join(current_dir, txt_filename)
print(f"Converting '{filename}' to '{txt_filename}'...")
pdf_to_txt(pdf_path, txt_path)
print("Conversion completed.")
if __name__ == "__main__":
main()
Step 3: Run the Script
Navigate to your directory and execute:
python pdf_to_txt.py
Method 3: Using Adobe Acrobat (GUI Method)
If you prefer a graphical interface and have Adobe Acrobat (not the free Reader), you can convert PDFs to TXT manually or in batches.
Step 1: Open Adobe Acrobat
Launch Adobe Acrobat (ensure you have the full version, not just Adobe Reader).
Step 2: Use the Export Function
- Go to File > Export To > Text (Plain).
- Select the destination folder and save the TXT file.
Batch Conversion:
- Go to File > Action Wizard > Create New Action.
- Add Export action specifying Text (Plain) as the output format.
- Specify the source folder containing PDFs and the output folder for TXT files.
- Run the action to process all PDFs in the folder.
Note: Batch processing might require Adobe Acrobat Pro.
Method 4: Using Online Conversion Tools
Several online services allow you to upload PDF files and convert them to TXT. Examples include:
Caution: Be mindful of privacy and data security when uploading sensitive documents to online platforms.
Additional Tips
By following one of the methods above, you should be able to convert all PDF files in your current directory to TXT files efficiently. Choose the approach that best fits your technical comfort level and the tools available on your system.