2022-01-04 10:42:55 +00:00
|
|
|
#!/usr/bin/env python3.7
|
|
|
|
# coding=utf-8
|
|
|
|
|
2022-01-27 12:40:23 +00:00
|
|
|
''' Combine multiple hOCR files. '''
|
2022-01-04 10:42:55 +00:00
|
|
|
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
from lxml import html
|
|
|
|
|
|
|
|
|
|
|
|
parser = ArgumentParser(description='Combine multiple hOCR files.')
|
2022-01-27 12:40:23 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'-i', '--input-file',
|
|
|
|
help='Input file',
|
|
|
|
nargs='+',
|
|
|
|
required=True
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'-o', '--output-file',
|
|
|
|
help='Output file',
|
|
|
|
required=True
|
|
|
|
)
|
2022-01-04 10:42:55 +00:00
|
|
|
args = parser.parse_args()
|
2022-01-27 12:40:23 +00:00
|
|
|
print(args)
|
2022-01-04 10:42:55 +00:00
|
|
|
|
2022-01-27 12:40:23 +00:00
|
|
|
for input_file in args.input_file:
|
|
|
|
input_files = []
|
|
|
|
if input_file.startswith('@'):
|
|
|
|
with open(input_file[1:], 'r') as f:
|
|
|
|
input_files += [x for x in f.read().split("\n") if x != '']
|
2022-01-04 10:42:55 +00:00
|
|
|
else:
|
2022-01-27 12:40:23 +00:00
|
|
|
input_files.append(input_file)
|
|
|
|
if len(input_files) == 0:
|
2022-01-04 10:42:55 +00:00
|
|
|
exit(1)
|
|
|
|
|
|
|
|
|
2022-01-27 12:40:23 +00:00
|
|
|
hocr = html.parse(input_files[0])
|
2022-01-04 10:42:55 +00:00
|
|
|
hocr_body = hocr.find('body')
|
2022-01-27 12:40:23 +00:00
|
|
|
for input_file in input_files[1:]:
|
|
|
|
for ocr_page in html.parse(input_file).findall('//div[@class="ocr_page"]'):
|
2022-01-04 10:42:55 +00:00
|
|
|
hocr_body.append(ocr_page)
|
|
|
|
|
|
|
|
|
|
|
|
with open(args.output_file, 'wb') as f:
|
|
|
|
hocr.write(f, encoding='UTF-8', method='html')
|