A Python-based tool for converting PDF files to DOCX format with enhanced accuracy and customizable conversion parameters. This project is optimized for AWS SageMaker environments.
This converter uses the pdf2docx library with fine-tuned parameters to provide high-quality PDF to DOCX conversion. It automatically processes all PDF files in the current directory and generates corresponding DOCX files with improved layout detection, image handling, and text formatting.
- Batch Processing: Converts all PDF files in the current directory
- Multi-processing Support: Utilizes multiple CPU cores for faster conversion
- Enhanced Accuracy: Fine-tuned parameters for better layout and formatting preservation
- Image Handling: Optimized image resolution and size controls
- Error Handling: Robust error handling with detailed feedback
- AWS SageMaker Ready: Pre-configured for SageMaker environments
- AWS SageMaker environment
- Conda package manager
- Python 3.10+
- Access to private GitHub repository
pdf-to-docx-converter/
├── environment.yml # Conda environment configuration
├── requirements.txt # Python dependencies
├── main.py # Main conversion script
└── README.md # This file
In your SageMaker terminal, clone this private repository:
git clone https://github.com/your-username/your-private-repo.git
cd your-private-repoNote: Ensure you have proper authentication set up for accessing private GitHub repositories in SageMaker. You may need to configure SSH keys or personal access tokens.
Create the conda environment using the provided configuration:
conda env create -f environment.ymlThis will create an environment named myenv with all required dependencies.
Initialize conda for your bash shell (required for SageMaker):
conda init bashReload your bash configuration to apply conda initialization:
source ~/.bashrcActivate the newly created environment:
conda activate myenvYour prompt should now show (myenv) indicating the environment is active.
- Place your PDF files in the same directory as
main.py - Run the conversion script:
python main.pyThe script will:
- Automatically detect all PDF files in the current directory
- Convert each PDF to DOCX format
- Save output files with the prefix
convert_(e.g.,document.pdf→convert_document.docx) - Display progress and status messages
Converting: document1.pdf
Converted: convert_document1.docx
Converting: report.pdf
Converted: convert_report.docx
The converter uses optimized parameters for high-quality conversion:
- Multi-processing: Enabled for faster processing
- Border Detection: Fine-tuned tolerance and clearance settings
- Image Processing: Optimized resolution and gap handling
- Text Alignment: Enhanced threshold settings for better layout preservation
- Margin Handling: Balanced top and bottom margin factors
pdf2docx>=0.5.6- Main conversion libraryPyMuPDF>=1.19.0- PDF processing backendpython-docx>=0.8.10- DOCX file handlinglxml>=4.6.0- XML processingfonttools>=4.25.0- Font handling
- Python 3.10
- Conda package manager
1. Conda activation error:
CondaError: Run 'conda init' before 'conda activate'
Solution: Run conda init bash followed by source ~/.bashrc
2. No PDF files found:
No PDF files found
Solution: Ensure PDF files are in the same directory as main.py
3. Permission errors: Solution: Check file permissions and ensure write access to the directory
4. Memory issues with large PDFs: Solution: Process files individually or increase SageMaker instance memory
If you encounter authentication issues when cloning private repositories:
Option 1: Personal Access Token
git clone https://username:token@github.com/username/repo.gitOption 2: SSH Key Setup
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# Add the public key to your GitHub accountFor better performance in SageMaker:
- Use appropriate instance types: Choose instances with sufficient CPU cores for multi-processing
- Batch processing: Process multiple files in sequence rather than parallel for memory efficiency
- Monitor resources: Use SageMaker's monitoring tools to track CPU and memory usage
For issues related to:
- PDF conversion quality: Adjust parameters in
main.py - Performance: Consider instance type and file size optimization
- Dependencies: Check
environment.ymlandrequirements.txt
Note: This project is designed specifically for AWS SageMaker environments. For local development, you may need to adjust the setup steps accordingly.