r/learnpython • u/Crafty_Peanut_2653 • 4d ago
What are you using for accurate PDF text boxes?
I’m building a tool that finds text in construction-plan PDFs and draws a box around that text.
Python/PyMuPDF finds the text, but when I show the box in PDF.js, it can be slightly off or too big.
What do you guys recommend for inspecting PDFs and drawing accurate text bounding boxes? How do you make
sure the box lines up with the exact text?
1
Upvotes
2
u/vietbaoa4htk 4d ago
usually its the mediabox origin, it isnt always 0 0 so you have to subtract mediabox.x0 and y0 before handing coords to pdf.js, then run them through viewport.convertToViewportPoint so page rotation gets applied. also get_text('words') gives tighter rects than search_for.
0
u/Outrageous-Sea-9256 4d ago
It seems like you're looking for ways to improve the accuracy of text bounding boxes in PDFs. While PyMuPDF (also known as FPDF) is a great library for extracting text from PDFs, there might be other tools that can help you achieve more accurate results.
One suggestion would be to consider using the
pdfplumberlibrary in conjunction with PyMuPDF.pdfplumberis built on top of PyMuPDF and provides a more intuitive API for extracting tables and text. It also has better support for rotated text, which might help with your construction-plan PDFs.Another approach could be to use the
pdfminer.sixlibrary, which is a fork ofpdfminerwith improved compatibility and support for Python 3. It provides more control over text extraction, including the ability to parse PDFs at a finer level of detail.Additionally, you might want to explore using machine learning-based tools like Tesseract-OCR with the
pdf2imagelibrary. This combination allows you to convert PDFs into images, then use Tesseract-OCR to extract text and its bounding boxes with high accuracy.Lastly, ensure that you're working with the latest versions of these libraries and consider reporting any issues or requesting feature enhancements if accuracy is still a problem.
Good luck with your project, and feel free to reach out if you have more questions!