PDFtoWord
PDFtoWord
A powerful Python script for extracting images from PDF files, performing OCR (Optical Character Recognition), translating text, and converting to DOCX format.
Features
- 🔍 PDF Image Extraction: Automatically extracts all images from PDF files
- 🔄 Smart Image Rotation: Detects and corrects image orientation for better OCR accuracy
- 📝 OCR Processing: Converts images to text using Tesseract OCR
- 🌍 Multi-language Support: Supports Italian and English OCR with automatic translation
- 📄 Multiple Output Formats: Generates TXT, HTML, HOCR, and DOCX files
- 🔗 Batch Processing: Processes multiple PDF files in a single run
- 🎯 Auto-detection: Automatically finds Tesseract installation
Prerequisites
Required Software
- Python 3.8+
- Tesseract OCR - Download from GitHub Releases
- Windows: Install the
.exefile - The script will automatically detect Tesseract installation
- Windows: Install the
Required Python Libraries
All dependencies are listed in requirements.txt:
PyMuPDF==1.23.0
Pillow==10.0.0
pytesseract==0.3.10
beautifulsoup4==4.12.2
python-docx==0.8.11
googletrans==4.0.0rc1
Installation
-
Clone the repository:
git clone https://github.com/rsodvd79/PDFtoWord.git cd PDFtoWord -
Install Python dependencies:
pip install -r requirements.txt
-
Install Tesseract OCR:
- Download from Tesseract releases
- Follow the installation instructions for your operating system
- The script will automatically detect the installation
Usage
Basic Usage
-
Prepare your PDF files:
- Create a
PDFfolder in the project directory - Place your PDF files inside the
PDFfolder
- Create a
-
Run the script:
python PDFtoWord.py
-
Select OCR language:
- Choose
1for Italian (with English translation) - Choose
2for English (with Italian translation)
- Choose
Output Structure
The script creates the following directory structure:
📁 IMG/
└── 📁 [PDF_NAME]/
├── img_0001.png
├── img_0002.png
└── ...
📁 TXT/
└── 📁 [PDF_NAME]/
├── img_0001.png.txt
├── img_0002.png.txt
└── ...
📁 HTML/
└── 📁 [PDF_NAME]/
├── img_0001.png.html
├── img_0001.png.hocr
├── [PDF_NAME]_libro.txt.html
└── ...
📁 risultato/
├── [PDF_NAME]_libro.txt
├── [PDF_NAME]_libro.docx
├── [PDF_NAME]_libro_translated_[LANG].txt
├── [PDF_NAME]_libro_translated_[LANG].docx
├── libro.txt (aggregated)
├── libro.docx (aggregated)
├── libro_translated_[LANG].txt (aggregated)
└── libro_translated_[LANG].docx (aggregated)
How It Works
- Image Extraction: The script uses PyMuPDF to extract all images from PDF files
- Orientation Detection: Uses Tesseract's OSD (Orientation and Script Detection) to detect and correct image rotation
- OCR Processing: Converts images to text using Tesseract OCR
- Text Processing: Merges hyphenated words that are split across lines
- Translation: Uses Google Translate API to translate text to the target language
- Format Conversion: Creates multiple output formats (TXT, HTML, HOCR, DOCX)
Configuration
Tesseract Path
The script automatically detects Tesseract installation in common locations:
C:\Program Files\Tesseract-OCR\tesseract.exeC:\Program Files (x86)\Tesseract-OCR\tesseract.exe- User AppData directories
- Current directory
- System PATH
If automatic detection fails, you can manually set the path in the script:
pytesseract.pytesseract.tesseract_cmd = r'C:\Path\To\Your\tesseract.exe'
Supported Languages
- Italian (
ita) - with English translation - English (
eng) - with Italian translation
Additional languages can be added by modifying the script and ensuring the corresponding Tesseract language packs are installed.
Error Handling
The script includes robust error handling for:
- Missing Tesseract installation
- Corrupted or unreadable images
- Translation service errors
- File I/O operations
Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Author
Davide Rosa - rsodvd79
Acknowledgments
- Tesseract OCR for optical character recognition
- PyMuPDF for PDF processing
- Google Translate for translation services
Troubleshooting
Common Issues
-
Tesseract not found:
- Ensure Tesseract is properly installed
- Check that the installation path is correct
- Verify Tesseract is in your system PATH
-
Poor OCR quality:
- Ensure images have good resolution
- Check if images need manual rotation
- Consider preprocessing images for better contrast
-
Translation errors:
- Check internet connectivity
- Verify Google Translate service availability
- Consider rate limiting for large documents
-
Memory issues with large PDFs:
- Process PDFs one at a time
- Reduce image resolution if necessary
- Consider splitting large PDFs into smaller files
Support
If you encounter any issues or have questions, please open an issue on GitHub.
Per il codice sorgente e gli altri progetti visita il profilo GitHub di rsodvd79 .