A Python script to convert PDFs, EPUBs, and HTML files to Markdown format using the Datalab.to Conversion API. The script processes multiple files asynchronously with progress tracking and error handling.
- ✅ Convert PDFs, EPUBs, and HTML files to Markdown
- ✅ Asynchronous processing for faster batch conversions
- ✅ Progress bar with real-time status updates
- ✅ Automatic skipping of already converted files
- ✅ Comprehensive error handling and reporting
- ✅ Support for custom input and output directories
- ✅ Uses Datalab.to API for high-quality document conversion
- Python 3.8 or higher
- A Datalab.to API key (Get one here)
-
Clone or download this repository
-
Create a virtual environment (recommended):
python3 -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Set up your API key:
- Create a
.envfile in the project root - Add your Datalab API key:
- Create a
DATALAB_API_KEY=your_api_key_hereAlternatively, you can use the legacy variable name:
MARKER_PDF_KEY=your_api_key_hereConvert all PDFs in a directory (output will be saved to <input_directory>_Markdown):
python convert_pdfs_to_markdown.py -i path/to/pdf/directoryConvert PDFs and save to a custom output directory:
python convert_pdfs_to_markdown.py -i path/to/pdf/directory -o path/to/output/directoryConvert EPUB files:
python convert_pdfs_to_markdown.py -i path/to/epub/directory -f epubConvert HTML files:
python convert_pdfs_to_markdown.py -i path/to/html/directory -f htmlConvert all supported formats (PDF, EPUB, HTML):
python convert_pdfs_to_markdown.py -i path/to/mixed/directory -f all| Argument | Short | Required | Description |
|---|---|---|---|
--input |
-i |
Yes | Input directory containing files to convert |
--output |
-o |
No | Output directory for Markdown files (default: <input>_Markdown) |
--format |
-f |
No | File format to convert: pdf, epub, html, or all (default: pdf) |
python convert_pdfs_to_markdown.py -i ./PDFsOutput: Creates PDFs_Markdown/ directory with converted files
python convert_pdfs_to_markdown.py -i ./documents -o ./markdown_outputpython convert_pdfs_to_markdown.py -i ./ebooks -f epub -o ./ebooks_markdown- File Discovery: The script scans the input directory for files matching the specified format(s)
- Async Processing: Files are processed asynchronously (up to 10 concurrent conversions) using the Datalab.to API
- Progress Tracking: Real-time progress bar shows conversion status for each file
- Error Handling: Failed conversions are tracked and reported in the summary
- Skip Existing: Already converted files (existing
.mdfiles) are automatically skipped
- Each converted file is saved as
<filename>.mdin the output directory - The script prints a summary showing:
- Number of successful conversions
- Number of failed conversions
- Location of output files
- Error messages for any failures
If a conversion fails, the script will:
- Continue processing other files
- Display an error message in the progress bar
- Include the error in the final summary report
- Preserve any successfully converted files
The script uses a semaphore to limit concurrent requests to 10 at a time to avoid overwhelming the API. If you encounter rate limit errors, you can modify the semaphore = asyncio.Semaphore(10) line in the script to reduce concurrency.
- Ensure your
.envfile exists in the project root - Verify the API key is set correctly:
DATALAB_API_KEY=your_key_here - Make sure
python-dotenvis installed
- Check that the input directory path is correct
- Verify files have the correct extension (
.pdf,.epub,.html, etc.) - Ensure the
--formatflag matches your file types
- Check your API key is valid and has sufficient credits
- Verify the input files are not corrupted
- Review error messages in the summary output
datalab-python-sdk: Official Datalab.to Python SDK for API integrationpython-dotenv: Load environment variables from.envfiletqdm: Progress bar for visual feedback
This project is licensed under the MIT License. See the LICENSE file for details.
See CHANGELOG.md for a detailed list of changes and version history.
For issues related to:
- This script: Open an issue in this repository
- Datalab.to API: Visit Datalab.to Documentation
- API Key issues: Contact Datalab.to support
Feel free to submit issues, fork the repository, and create pull requests for any improvements.