How to Extract Some Mathematical Expressionfrom Pdf Using Python?

I have a pdf which has math equations like this

I am trying to extract the objective questions from a pdf file and convert them into csv file using python in such a way that each row of table contain a question, four options in each column and a correct option (so total six columns). But that pdf also have those mathematical equations which I can't write them into csv file as they are . Is it possible to write those equations in my csv file as they are in pdf file ?

3

1 Answer

This depends on how the formula is represented in PDF. It can be either XObject, inline image or unicode text.

Try pdfreader. It can extract plain texts, texts containing PDF commands and images from PDF documents.

from pdfreader import SimplePDFViewer, PageDoesNotExist

fd = open(you_pdf_file_name, "rb")
viewer = SimplePDFViewer(fd)

plain_text = ""
pdf_markdown = ""
images = []
try:
    while True:
        viewer.render()
        pdf_markdown += viewer.canvas.text_content
        plain_text += "".join(viewer.canvas.strings)
        images.extend(viewer.canvas.inline_images)
        images.extend(viewer.canvas.images.values())
        viewer.next()
except PageDoesNotExist:
    pass

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest