Skip to main content

Convert PDF files to Markdown with OCR support

Project description

Smart File to Markdown Converter

PyPI version Python Support License: MIT

A powerful and flexible tool to convert PDF files to Markdown format with intelligent OCR fallback support.

Features

  • 🚀 Fast text extraction from text-based PDFs
  • 🔍 Automatic OCR fallback for scanned documents
  • 📦 Batch processing of multiple files
  • 🔄 Recursive directory processing
  • ⚙️ Flexible configuration options
  • 🎯 Smart converter selection based on PDF type
  • 📝 Clean, well-structured code following OOP and SOLID principles

Installation

From PyPI (recommended)

pip install smart-file2md

From source

git clone https://github.com/ezazulhaq/smart-file2md.git
cd smart-file2md
pip install -e .

System Requirements

This package requires Tesseract OCR to be installed on your system for OCR functionality:

Ubuntu/Debian:

sudo apt-get install tesseract-ocr

macOS:

brew install tesseract

Windows: Download the installer from GitHub

Usage

Command Line Interface

# Convert a single PDF file
smart-file2md document.pdf

# Convert multiple PDF files
smart-file2md file1.pdf file2.pdf file3.pdf

# Convert all PDFs in a directory
smart-file2md /path/to/pdfs/

# Convert all PDFs in a directory and subdirectories
smart-file2md /path/to/pdfs/ --recursive

# Specify an output directory
smart-file2md document.pdf --output-dir output/

# Process only the first 10 pages
smart-file2md document.pdf --max-pages 10

# Force OCR even for text-based PDFs
smart-file2md document.pdf --force-ocr

# Overwrite existing output files
smart-file2md document.pdf --overwrite

Programmatic API

from markdown_convert import convert_pdf

# Simple conversion
convert_pdf('document.pdf')

# With custom options
convert_pdf(
    'document.pdf',
    output_dir='output/',
    max_pages=10,
    force_ocr=False
)

Advanced Usage

from pathlib import Path
from markdown_convert import ConverterConfig, ConverterFactory

# Create custom configuration
config = ConverterConfig(
    output_dir=Path('output/'),
    max_pages=20,
    force_ocr=False,
    skip_existing=True,
    ocr_dpi=2,
    progress_interval=5
)

# Create converter and process
pdf_path = Path('document.pdf')
converter = ConverterFactory.create(pdf_path, config)
output_path = converter.convert(pdf_path)

print(f"Converted to: {output_path}")

Output Location

If no output directory is specified with --output-dir, the tool creates a markdown directory in the same location as the PDF files and saves the converted files there.

Options

Option Description
-o, --output-dir Specify output directory for markdown files
-m, --max-pages Maximum number of pages to process per PDF
--force-ocr Force using OCR even if text can be extracted directly
-r, --recursive Recursively process directories
--overwrite Overwrite existing output files (default: skip existing)
--version Show version information

Performance Notes

  • Text-based PDFs: Very fast extraction using direct text extraction
  • Scanned PDFs: Slower due to OCR processing, but automatic fallback ensures compatibility

Architecture

This project follows best practices in software design:

  • OOP Principles: Encapsulation, inheritance, polymorphism, and abstraction
  • SOLID Principles: Single responsibility, open/closed, Liskov substitution, interface segregation, and dependency inversion
  • Design Patterns: Strategy pattern for converters, Factory pattern for converter selection, Template Method for conversion workflow
  • Type Safety: Full type hints throughout the codebase
  • Separation of Concerns: Clear separation between CLI, business logic, and utilities

Development

Setup Development Environment

# Clone the repository
git clone https://github.com/ezazulhaq/markdown-convert.git
cd markdown-convert

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install in development mode with dev dependencies
pip install -e ".[dev]"

Running Tests

# Run all tests
pytest

# Run with coverage
pytest --cov=markdown_convert --cov-report=html

Code Quality

# Format code
black src/ tests/

# Lint code
ruff check src/ tests/

# Type checking
mypy src/

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Ezaz Ul Haq

Acknowledgments

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

smart_file2md-1.0.0.tar.gz (15.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

smart_file2md-1.0.0-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

Details for the file smart_file2md-1.0.0.tar.gz.

File metadata

  • Download URL: smart_file2md-1.0.0.tar.gz
  • Upload date:
  • Size: 15.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for smart_file2md-1.0.0.tar.gz
Algorithm Hash digest
SHA256 9170357427dfaf6cdb8f229005957f86a473e5a76694fb361863800dc2a246f4
MD5 6d2e1c0a052f9b6f115744bb0da5cfd9
BLAKE2b-256 e181e8db6798d332d3f8e9851ef33efed0c81745d13500465503c2adc33da9f1

See more details on using hashes here.

File details

Details for the file smart_file2md-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: smart_file2md-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 15.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for smart_file2md-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4630700d879c0f6858a23cdbe1b11126696d800956e0bfd8571b0e56b25cd251
MD5 306e6fb3058fcfae226b19c63251de1d
BLAKE2b-256 8e50cb4c72e203f96855cc0baa53eca59bbb9a66be5942b5e74e266bba7de894

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page