diff --git a/src/ocrmypdf/api.py b/src/ocrmypdf/api.py index 700a3a98..69de7fbd 100644 --- a/src/ocrmypdf/api.py +++ b/src/ocrmypdf/api.py @@ -209,6 +209,9 @@ def create_options( cmdline.append('stream://output_file') else: cmdline.append(os.fspath(output_file)) + if 'sidecar' in kwargs and isinstance(kwargs['sidecar'], BinaryIO | IOBase): + cmdline.append('--sidecar') + cmdline.append('stream://sidecar') parser.enable_api_mode() options = parser.parse_args(cmdline) @@ -219,6 +222,8 @@ def create_options( options.input_file = input_file if options.output_file == 'stream://output_file': options.output_file = output_file + if options.sidecar == 'stream://sidecar': + options.sidecar = kwargs['sidecar'] return options @@ -230,7 +235,7 @@ def ocr( # noqa: D417 language: Iterable[str] | None = None, image_dpi: int | None = None, output_type: str | None = None, - sidecar: StrPath | None = None, + sidecar: PathOrIO | None = None, jobs: int | None = None, use_threads: bool | None = None, title: str | None = None, diff --git a/tests/test_api.py b/tests/test_api.py index 540d4ac0..f5eea148 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -29,6 +29,18 @@ def test_stream_api(resources: Path): assert b'%PDF' in out.read(1024) +def test_sidecar_stringio(resources: Path, outdir: Path, outpdf: Path): + s = BytesIO() + ocrmypdf.ocr( + resources / 'ccitt.pdf', + outpdf, + plugins=['tests/plugins/tesseract_cache.py'], + sidecar=s + ) + s.seek(0) + assert b'the' in s.getvalue() + + def test_hocr_api_multipage(resources: Path, outdir: Path, outpdf: Path): ocrmypdf.api._pdf_to_hocr( resources / 'multipage.pdf', @@ -62,3 +74,4 @@ def test_hocr_to_pdf_api(resources: Path, outdir: Path, outpdf: Path): text = extract_text(outpdf) assert 'hocr' in text and 'the' not in text +