From c7b8b6e18ba7fac3db23eb0a87c79ed401a77ee1 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Thu, 26 Oct 2017 18:15:31 -0700 Subject: [PATCH] Fix issue #194 - --sidecar creates blank txt file --- docs/release_notes.rst | 6 ++++++ ocrmypdf/pipeline.py | 8 ++++---- tests/test_main.py | 14 +++++++++++++- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/docs/release_notes.rst b/docs/release_notes.rst index 9534b46a..9982070e 100644 --- a/docs/release_notes.rst +++ b/docs/release_notes.rst @@ -5,6 +5,12 @@ OCRmyPDF uses `semantic versioning `_ for its command line i The OCRmyPDF package itself does not contain a public API, although it is fairly stable and breaking changes are usually timed with a major release. A future release will clearly define the stable public API. +v5.4.2 +------ + +- Fixed a regression from v5.4.1 that caused sidecar files to be created as empty files + + v5.4.1 ------ diff --git a/ocrmypdf/pipeline.py b/ocrmypdf/pipeline.py index e23c085b..77f36a60 100644 --- a/ocrmypdf/pipeline.py +++ b/ocrmypdf/pipeline.py @@ -945,7 +945,7 @@ def merge_sidecars( if page_num != 0: stream.write('\f') # Form feed between pages if txt_file: - with open(txt_file, 'r') as in_: + with open(txt_file, 'r', encoding="utf-8") as in_: txt = in_.read() # Tesseract v4 alpha started adding form feeds in # commit aa6eb6b @@ -953,9 +953,9 @@ def merge_sidecars( # for consistency just ignore its form feeds and insert our # own if txt.endswith('\f'): - stream.write(in_.read()[:-1]) + stream.write(txt[:-1]) else: - stream.write(in_.read()) + stream.write(txt) else: stream.write('[OCR skipped on page {}]'.format( page_num + 1)) @@ -964,7 +964,7 @@ def merge_sidecars( write_pages(sys.stdout) sys.stdout.flush() else: - with open(output_file, 'w', encoding='utf-8') as out: + with open(output_file, 'w', encoding="utf-8") as out: write_pages(out) diff --git a/tests/test_main.py b/tests/test_main.py index 523f1387..10094741 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -805,7 +805,6 @@ def test_user_words(resources, outdir): '--image-dpi', 150, '--sidecar', sidecar_before ) - assert 'cromulent' not in sidecar_before.open().read() with word_list.open('w') as f: @@ -984,6 +983,19 @@ def test_sidecar_pagecount(spoof_tesseract_cache, resources, outpdf): "Sidecar page count does not match PDF page count" +def test_sidecar_nonempty(spoof_tesseract_cache, resources, outpdf): + sidecar = outpdf + '.txt' + check_ocrmypdf( + resources / 'ccitt.pdf', outpdf, + '--sidecar', sidecar, + env=spoof_tesseract_cache + ) + + with open(sidecar, 'r') as f: + ocr_text = f.read() + assert 'the' in ocr_text + + def test_pdfa_1(spoof_tesseract_cache, resources, outpdf): check_ocrmypdf( resources / 'ccitt.pdf', outpdf,