r/CodingHelp • • 24d ago

[Python] How do I extract mathematical language accurately from a pdf using python

Tried alot, can't get to any solution. Special characters or diamond question marks appear etc but the accurate and understandable text doesn't. I'm looking for a library or a method which can address this, extract complex characters e.g. set notations or special characters in maths and fractions etc etc

if anybody can help,

thanks alot

APPRECIATE THAT

5 Upvotes

6 comments sorted by

•

u/AutoModerator 24d ago

Thank you for posting on r/CodingHelp!

Please check our Wiki for answers, guides, and FAQs: https://coding-help.vercel.app

Our Wiki is open source - if you would like to contribute, create a pull request via GitHub! https://github.com/DudeThatsErin/CodingHelp

We are accepting moderator applications: https://forms.fillout.com/t/ua41TU57DGus

We also have a Discord server: https://discord.gg/geQEUBm

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/gofuckadick 24d ago

For mathematical notation, you can try looking into tools like these:

  • PyMuPDF - Good for extracting text from digitally generated PDFs, although it may struggle with mathematical notation and custom font encodings.
  • Marker - Converts PDFs into Markdown and supports mathematical expressions, including LaTeX output.
  • pix2tex - Specifically designed to recognize mathematical equations from images and convert them into LaTeX.
  • Mathpix - Another option for extracting mathematical expressions and converting entire documents, although it's a paid service.

One thing worth keeping in mind is that PDFs don't necessarily store equations as structured mathematical expressions. Fractions, superscripts, and special symbols may be stored as separately positioned characters or glyphs, sometimes without proper Unicode mappings.

If you're getting replacement characters (�) or other garbage, it could be a font encoding issue rather than a problem with the extraction library itself. Some PDFs use custom font encodings without proper Unicode mappings, so even though the characters display correctly, extracting them as text doesn't work.

In cases like that, a math-aware OCR tool like Marker or Mathpix may give you better results, even if the PDF isn't scanned, since OCR can recognize the visible mathematical notation rather than relying on the PDF's underlying character encoding.

1

u/colnelali 24d ago

Thanks a lot bud :)

1

u/atamicbomb 23d ago

How is the PDF encoded? If it’s just images, you will need some kind of image recognition and it won’t be perfect.

A PDF is a binary file and I imagine you’re getting gibberish by having Python open it as a text file.

1

u/colnelali 23d ago

No its like a selectable text pdf file, not an image for which id need an ocr

1

u/medialantern 23d ago

That does not invalidate their comment.

A lot of folks don't understand how PDF documents work. There is no "integral" text formula that gets drawn to look like a properly formatted integral. It is (usually, there are complexities even here) a stream of "commands" and those commands look like "go to 141,277 and draw glyph 81723 from character map 5123". OCR is actually the better/easier/faster option to accurately recognize what's in there.