How to extract charts/tables/graphs from PDF files using Python? - python

Searched quite a bit but as I couldn't find a solution for this kind of problem, hence posting a clear question on the same. Most answers cover image/text extraction which are comparatively easier.
I've a requirement of extracting tables and graphs as text (csv) and images respectively from PDFs.
Can anyone help me with an efficient python 3.6 code to solve the same?
Till now I could achieve extracting jpgs using startmark = b"\xff\xd8" and endmark = b"\xff\xd9", but not all tables and graphs in a PDF are plain jpgs, hence my code fails badly in achieving that.
Example, I want to extract table from page 11 and graphs from page 12 as image or something which is feasible from the below given link. How to go about it?
https://hartmannazurecdn.azureedge.net/media/2369/annual-report-2017.pdf

For extracting tables you can use camelot
Here is an article about it.
For images I've found this question and answer Extract images from PDF without resampling, in python?

Try using PyMuPdf(https://github.com/pymupdf/PyMuPDF/tree/1.18.3) for amalgamation of texts, bars, lines and axis. It has so many extra utilities.

Related

Extracting text from two column pdf using python

I am trying to extract text from a two-column pdf. On using pypdf2 and pdfplumber it is reading the page horizontally, i.e. after reading first line of column one it goes on to read first line of column two instead of second line of column one. I have also tried this code githubusercontent
as it is, but I have the same issue. I also saw this How to extract text from two column pdf with Python? but I dont want to convert to image as I have thousands of pages.
Any help will be appreciated. Thannnks!
You can check this blog here which uses PyMuPDF to extract two column pdfs like research papers.
https://towardsdatascience.com/read-a-multi-column-pdf-using-pymupdf-in-python-4b48972f82dc
From what I have tested so far, it works quite well. I would highly recommend the "blocks" option.
# OCR the PDF using the default 'text' parameter
with fitz.open(DIGITIZED_FILE_PATH) as doc:
for page in doc:
text = page.get_text("blocks")
print(text)
Note: It does not work for scanned images. It works only for searchable pdf files.

Combining all Elements with PYPDF (like one big image)

I wrote a code in python, which creates music notation based on a user input. It generates a PDF and everything works fine.
The only problem I have is that the notation consists of 100's of small images and I wanted to ask if there is a possibility to merge them all together into one big image. I don't want them to be selectable or anything. Basically I want the PDF to be like one big picture per page.
Is that possible using PyPDF2?
Thanks in advance and have a great weekend!

How to extract particular block of text from pdf file with Python?

I wanted to extract blocks of text as image (here the questions and options in pdf) from pdf and then store each block of text as a image separately.
The length of questions varies a lot, so using pyPDF2 for splitting the pdf at regular intervals is out of question.
Any suggestion? I have read few posts that mention using OCR to get question numbers and then splitting but I didn't completely get what they were trying to say. Is there any easier method?

Searching for data in a PDF

I've got a PDF file that I'm trying to obtain specific data from.
I've been able to parse the PDF via PyPDF2 into one long string but searching for specific data is difficult because of - I assume - formatting in the original PDF.
What I am looking to do is to retrieve specific known fields and the data that immediately follows (as formatted in the PDF) and then store these in seperate variables.
The PDFs are bills and hence are all presented in the exact same way, with defined fields and images. So what I am looking to do is to extract these fields.
What would be the best way to achieve this?
I've got a PDF file that I'm trying to obtain specific data from.
In general, it is probably impossible (or extremely difficult), and details (than you don't mention) are very important. Study in details the complex PDF specification. Notice that PDF is (more or less accidentally) Turing complete (so your problem is undecidable in general, since equivalent to the halting problem).
For example, a normal human reader could read digits in the document as text, or as a JPEG image, etc. And in practice many PDF documents have such kind of data.... Practically speaking, PDF is an output-only format and is designed for screen displaying and printing, not for extracting data from it.
You need to understand how exactly that PDF file was generated (with what exact software, from what actual data). That could take a lot of time (maybe several years of full time reverse-engineering work) without help.
A much better approach is to contact the person or entity providing that PDF file and negotiate some way of accessing the actual data (or at least get detailed explanation about the generation of that particular PDF file). For example, if the PDF file is computed from some database, you'll better access that database.
Perhaps using metadata or comments in your PDF file might help in guessing how it was generated.
The source of the data might produce various kinds of PDF file. For example, my cheap scanner is able to produce PDF. But your program would have hard time in extracting some numerical data from it (because that kind of PDF is essentially wrapping a pixelated image à la JPEG) and would need to deploy image recognition techniques (i.e. OCR) to do so.

How to find the source data of a series chart in Excel

I have some pretty strange data i'm working with, as can be seen in the image. Now I can't seem to find any source data for the numbers these graphs are presenting.
Furthermore if I search for the source it only points to an empty cell for each graph.
Ideally I want to be able to retrieve the highlighted labels in each case using python, and it seems finding the source is the only way to do this, so if you know of a python module that can do that i'd be happy to use it. Otherwise if you can help me find the source data that would be even perfecter :P
So far i've tried the XLDR module for python as well as manually showing all hidden cells, but neither work.
Here's a link to the file: Here
EDIT I ended up just converting the xlsx to a pdf using cloudconvert.com API
Then using pdftotext to convert the data to a .txt which just analyses everything including the numbers on the edge of the chart which can then be searched using an algorithm.
If a hopeless internet wanderer comes upon this thread with the same problem, you can PM me for more details :P

Categories

Resources