import pdfplumber import os import time import subprocess import config def draw_boxes(page, boxes): im = debug_im(page) for box in boxes: im.draw_line(((box["x0"], page.height - box["y0"]), (box["x1"], page.height - box["y0"]))) im.draw_line(((box["x1"], page.height - box["y0"]), (box["x1"], page.height - box["y1"]))) im.draw_line(((box["x1"], page.height - box["y1"]), (box["x0"], page.height - box["y1"]))) im.draw_line(((box["x0"], page.height - box["y1"]), (box["x0"], page.height - box["y0"]))) if "debug_label" in box: im.draw.text( xy=(box["x0"], page.height-box["y0"]), text=str(box["debug_label"]), ) debug_show(im) def debug_im(page): return page.to_image(height=config.DEBUG_HEIGHT) def debug_show(im, name=None): #im.show() im.save(f"/tmp/dnd-pdf-to-txt{'' if not name else '-'+name}.jpg") if not config.DEBUG_NO_SHOW: go(f"qlmanage -p /tmp/dnd-pdf-to-txt{'' if not name else '-'+name}.jpg &> /dev/null") __subprocesses__ = [] def go(cmd): global __subprocesses__ __subprocesses__.append(subprocess.Popen(cmd, shell=True))