Batch processing

This article provides information about running OCRmyPDF on multiple files or configuring it as a service triggered by file system events.

Batch jobs

Consider using the excellent GNU Parallel to apply OCRmyPDF to multiple files at once.

Both parallel and ocrmypdf will try to use all available processors. To maximize parallelism without overloading your system with processes, consider using parallel -j 2 to limit parallel to running two jobs at once.

This command will run all ocrmypdf all files named *.pdf in the current directory and write them to the previous created output/ folder. It will not search subdirectories.

The --tag argument tells parallel to print the filename as a prefix whenever a message is printed, so that one can trace any errors to the file that produced them.

parallel --tag -j 2 ocrmypdf '{}' 'output/{}' ::: *.pdf

OCRmyPDF automatically repairs PDFs before parsing and gathering information from them.

Directory trees

This will walk through a directory tree and run OCR on all files in place, printing the output in a way that makes

find . -printf '%p' -name '*.pdf' -exec ocrmypdf '{}' '{}' \;

Alternatively, with a docker container (mounts a volume to the container where the PDFs are stored):

find . -printf '%p' -name '*.pdf' -exec docker run --rm -v <host dir>:<container dir> jbarlow83/ocrmypdf '<container dir>/{}' '<container dir>/{}' \;

This only runs one ocrmypdf process at a time. This variation uses find to create a directory list and parallel to parallelize runs of ocrmypdf, again updating files in place.

find . -name '*.pdf' | parallel --tag -j 2 ocrmypdf '{}' '{}'

In a Windows batch file, use

for /r %%f in (*.pdf) do ocrmypdf %%f %%f

Sample script

This user contributed script also provides an example of batch processing.

#!/usr/bin/env python3
# Original version by DeliciousPickle@github; modified

# This script must be edited to meet your needs.

import logging
import os
import sys

import ocrmypdf

# pylint: disable=logging-format-interpolation
# pylint: disable=logging-not-lazy

script_dir = os.path.dirname(os.path.realpath(__file__))
print(script_dir + '/ Start')

if len(sys.argv) > 1:
    start_dir = sys.argv[1]
    start_dir = '.'

if len(sys.argv) > 2:
    log_file = sys.argv[2]
    log_file = script_dir + '/ocr-tree.log'

    format='%(asctime)s %(message)s',


for dir_name, subdirs, file_list in os.walk(start_dir): + '\n')
    for filename in file_list:
        file_ext = os.path.splitext(filename)[1]
        if file_ext == '.pdf':
            full_path = dir_name + '/' + filename
            result = ocrmypdf.ocr(filename, filename, deskew=True)
            if result == ocrmypdf.ExitCode.already_done_ocr:
                print("Skipped document because it already contained text")
            elif result == ocrmypdf.ExitCode.ok:
                print("OCR complete")

Synology DiskStations

Synology DiskStations (Network Attached Storage devices) can run the Docker image of OCRmyPDF if the Synology Docker package is installed. Attached is a script to address particular quirks of using OCRmyPDF on one of these devices.

This is only possible for x86-based Synology products. Some Synology products use ARM or Power processors and do not support Docker. Further adjustments might be needed to deal with the Synology’s relatively limited CPU and RAM.

misc/ - Sample script for Synology DiskStations
#!/bin/env python3
# Contributed by

# This script must be edited to meet your needs.

import logging
import os
import shutil
import subprocess
import sys
import time

# pylint: disable=logging-format-interpolation
# pylint: disable=logging-not-lazy

script_dir = os.path.dirname(os.path.realpath(__file__))
timestamp = time.strftime("%Y-%m-%d-%H%M_")
log_file = script_dir + '/' + timestamp + 'ocrmypdf.log'
    format='%(asctime)s %(message)s',

if len(sys.argv) > 1:
    start_dir = sys.argv[1]
    start_dir = '.'

for dir_name, subdirs, file_list in os.walk(start_dir):
    for filename in file_list:
        file_stem, file_ext = os.path.splitext(filename)
        if file_ext != '.pdf':
        full_path = os.path.join(dir_name, filename)
        timestamp_ocr = time.strftime("%Y-%m-%d-%H%M_OCR_")
        filename_ocr = timestamp_ocr + file_stem + '.pdf'
        # create string for pdf processing
        # the script is processed as root user via chron
        cmd = [
        full_path_ocr = os.path.join(dir_name, filename_ocr)
        with open(filename, 'rb') as input_file, open(
            full_path_ocr, 'wb'
        ) as output_file:
            proc =
        os.chmod(full_path_ocr, 0o664)
        os.chmod(full_path, 0o664)
        full_path_ocr_archive = sys.argv[2]
        full_path_archive = sys.argv[2] + '/no_ocr'
        shutil.move(full_path_ocr, full_path_ocr_archive)
        shutil.move(full_path, full_path_archive)'Finished.\n')

Huge batch jobs

If you have thousands of files to work with, contact the author. Consulting work related to OCRmyPDF helps fund this open source project and all inquiries are appreciated.

Hot (watched) folders

Watched folders with

OCRmyPDF has a folder watcher called, which is currently included in source distributions but not part of the main program. It may be used natively or may run in a Docker container. Native instances tend to give better performance. works on all platforms.

Users may need to customize the script to meet their requirements.

pip3 install -r requirements/watcher.txt

env OCR_INPUT_DIRECTORY=/mnt/input-pdfs \
    OCR_OUTPUT_DIRECTORY=/mnt/output-pdfs \

One could configure a networked scanner or scanning computer to drop files in the watched folder.

Watched folders with Docker

The watcher service is included in the OCRmyPDF Docker image. To run it:

docker run \
    -v <path to files to convert>:/input \
    -v <path to store results>:/output \
    -e OCR_DESKEW=1 \
    -it --entrypoint python3 \
    jbarlow83/ocrmypdf \

This service will watch for a file that matches /input/\*.pdf and will convert it to a OCRed PDF in /output/. The parameters to this image are: parameters for Docker
Parameter Description
-v <path to files to convert>:/input Files placed in this location will be OCRed
-v <path to store results>:/output This is where OCRed files will be stored
-e OCR_ON_SUCCESS_DELETE=1 Define environment variable
-e OCR_DESKEW=1 Define environment variable
-e PYTHONBUFFERED=1 This will force STDOUT to be unbuffered and allow you to see messages in docker logs

This service relies on polling to check for changes to the filesystem. It may not be suitable for some environments, such as filesystems shared on a slow network.

A configuration manager such as Docker Compose could be used to ensure that the service is always available.

version: "3.3"
    restart: always
    container_name: ocrmypdf
    image: jbarlow83/ocrmypdf
      - "/media/scan:/input"
      - "/mnt/scan:/output"
    entrypoint: python3


  • watchmedo may not work properly on a networked file system, depending on the capabilities of the file system client and server.
  • This simple recipe does not filter for the type of file system event, so file copies, deletes and moves, and directory operations, will all be sent to ocrmypdf, producing errors in several cases. Disable your watched folder if you are doing anything other than copying files to it.
  • If the source and destination directory are the same, watchmedo may create an infinite loop.
  • On BSD, FreeBSD and older versions of macOS, you may need to increase the number of file descriptors to monitor more files, using ulimit -n 1024 to watch a folder of up to 1024 files.


  • On Linux, systemd user services can be configured to automatically perform OCR on a collection of files.
  • Watchman is a more powerful alternative to watchmedo.

AWS Lambda is not viable

AWS Lambda and its equivalents have low limits on execution time and payload size, relative to OCRmyPDF’s needs. As of this writing, the request/response payload for AWS Lambda was 6 MB, which means many PDFs will not fit.

macOS Automator

You can use the Automator app with macOS, to create a Workflow or Quick Action. Use a Run Shell Script action in your workflow. In the context of Automator, the PATH may be set differently your Terminal’s PATH; you may need to explicitly set the PATH to include ocrmypdf. The following example may serve as a starting point:

Example macOS Automator workflow

You may customize the command sent to ocrmypdf.