Ensure skipped pages are explained in sidecars
This commit is contained in:
+13
-4
@@ -882,16 +882,25 @@ def merge_sidecars(
|
||||
log,
|
||||
context):
|
||||
options = context.get_options()
|
||||
pdfinfo = context.get_pdfinfo()
|
||||
|
||||
txt_files = sorted(f for f in flatten_groups(input_files_groups)
|
||||
if f.endswith('.txt'))
|
||||
txt_files = [None] * len(pdfinfo)
|
||||
|
||||
for infile in flatten_groups(input_files_groups):
|
||||
if infile.endswith('.txt'):
|
||||
idx = page_number(infile) - 1
|
||||
txt_files[idx] = infile
|
||||
|
||||
def write_pages(stream):
|
||||
for page_number, txt_file in enumerate(txt_files):
|
||||
if page_number != 0:
|
||||
stream.write('\f') # Form feed between pages
|
||||
with open(txt_file, 'r') as in_:
|
||||
stream.write(in_.read())
|
||||
if txt_file:
|
||||
with open(txt_file, 'r') as in_:
|
||||
stream.write(in_.read())
|
||||
else:
|
||||
stream.write('[OCR skipped on page {}]'.format(
|
||||
page_number + 1))
|
||||
|
||||
if output_file == '-':
|
||||
write_pages(sys.stdout)
|
||||
|
||||
+21
-1
@@ -970,4 +970,24 @@ def test_compression_changed(spoof_tesseract_noop, ocrmypdf_exec,
|
||||
"Colorspace changed"
|
||||
elif im.mode.startswith('L'):
|
||||
assert pdfimage['color'] == 'gray', \
|
||||
"Colorspace changed"
|
||||
"Colorspace changed"
|
||||
|
||||
|
||||
def test_sidecar_pagecount(spoof_tesseract_cache, resources, outpdf):
|
||||
sidecar = outpdf + '.txt'
|
||||
check_ocrmypdf(
|
||||
resources / 'multipage.pdf', outpdf,
|
||||
'--skip-text',
|
||||
'--sidecar', sidecar,
|
||||
env=spoof_tesseract_cache)
|
||||
|
||||
pdfinfo = pdf_get_all_pageinfo(str(resources / 'multipage.pdf'))
|
||||
num_pages = len(pdfinfo)
|
||||
|
||||
with open(sidecar, 'r') as f:
|
||||
ocr_text = f.read()
|
||||
|
||||
# There should a formfeed between each pair of pages, so the count of
|
||||
# formfeeds is the page count less one
|
||||
assert ocr_text.count('\f') == num_pages - 1, \
|
||||
"Sidecar page count does not match PDF page count"
|
||||
|
||||
Reference in New Issue
Block a user