正如 gaw89 已经提到的,Tesseract 可以输出比仅作为流的文本更多的信息。 hocr fileformat 还为您提供每个段落、行、单词的位置(边界框):
$ tesseract 4LV05.png out -l eng hocr
然后你可以通过简单的方法找到“Inventory”这个词的边界框
$ grep 'Inventory' out.hocr
<span class='ocr_line' id='line_1_5' title="bbox 23 183 112 204; baseline 0 -5; x_size 21; x_descenders 5; x_ascenders 4"><span class='ocrx_word' id='word_1_15' title='bbox 23 183 112 204; x_wconf 93'>Inventory</span>
因此,这个词的边界框垂直跨度从 183 到 204,对于这个标签的对应值,我们现在必须在相同的垂直空间中搜索框。例如,这可以通过
来实现
$ grep 'bbox [0-9]* 18[0-9]' out.hocr
<p class='ocr_par' id='par_1_4' lang='eng' title="bbox 23 183 112 204">
<span class='ocr_line' id='line_1_5' title="bbox 23 183 112 204; baseline 0 -5; x_size 21; x_descenders 5; x_ascenders 4"><span class='ocrx_word' id='word_1_15' title='bbox 23 183 112 204; x_wconf 93'>Inventory</span>
<span class='ocr_line' id='line_1_30' title="bbox 1082 183 1178 202; baseline 0 -3; x_size 22; x_descenders 5.5; x_ascenders 5.5"><span class='ocrx_word' id='word_1_82' title='bbox 1082 183 1178 202; x_wconf 93'>1,277,838</span>
<span class='ocr_line' id='line_1_54' title="bbox 1301 183 1379 202; baseline 0 -3; x_size 22; x_descenders 5.5; x_ascenders 5.5"><span class='ocrx_word' id='word_1_107' title='bbox 1301 183 1379 202; x_wconf 95'>953,675</span>
第二个结果包含目标值。可以比较bbox的纵坐标,确保提取出第一列。
命令grep 在这个例子中就足够了,但当然还有其他方法可以做类似的事情。另请注意,正则表达式可能应替换为其他计算,具体取决于页面的倾斜程度。
或者,您可以尝试开源Tabula,它会尝试从 pdf 中提取表格数据。