pythonmatplotlibcoordinates

Draw lines between several points with known coordinates


My task: to connect points with lines using known coordinates.

What I have already done: using the doctr library, I find the coordinates of the words I am interested in on a pdf file. (My python code is not important for this question, below I will show what data I get from a specific file)

For example, there is a pdf file in which I am looking for the word "INVOICE"

enter image description here

After doctr has run, I get the following data

((0.09370404411764705, 0.0439453125), (0.33140099789915967, 0.09765625));

((0.5925912552521009, 0.1796875), (0.6575433298319328, 0.1953125));

((0.5925912552521009, 0.2041015625), (0.6575433298319328, 0.21875));

My question: how can I draw lines between these three points? To get a figure at the output. For a better understanding of the situation for you: the figure on this file will be like a template for me. I receive a large number of documents, and in order not to look through everything, I will check whether this figure is on the received document, and look through only the necessary document.


Solution

  • You can use PyMuPDF Library. The following code snippet will help you.

    import fitz  # PyMuPDF
    doc = fitz.open("Document_Name_Original.pdf")
    page = doc[pno]  # page number
    p1 = fitz.Point(x_1, y_1)  # First point
    p2 = fitz.Point(x_2, y_2)  # Second point
    p3 = fitz.Point(x_3, y_3)  # Third point
    page.draw_line(p1, p2, color=fitz.pdfcolor["red"], width=2)
    page.draw_line(p2, p3, color=fitz.pdfcolor["red"], width=2)
    page.draw_line(p3, p1, color=fitz.pdfcolor["red"], width=2)
    doc.save("Document_Name_Modified.pdf")
    

    Through this library, you can modify, add graph and add any plotting. If you want to know detail, this link 1 and link 2 help you for guide drawing on PDF document.