1

I am following this guide on how to extract data from Unstructured PDFs using PyMuPDF.

https://www.analyticsvidhya.com/blog/2021/06/data-extraction-from-unstructured-pdfs/

I am getting an AttributeError: 'NoneType' object has no attribute 'rect' error when I followed the code and not sure what is going on since I am new to Python.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-7f394b979351> in <module>
      1 first_annots=[]
      2 
----> 3 rec=page1.first_annot.rect
      4 
      5 rec

AttributeError: 'NoneType' object has no attribute 'rect'

Code

import fitz
import pandas as pd 
doc = fitz.open('Mansfield--70-21009048 - ConvertToExcel.pdf')
page1 = doc[0]
words = page1.get_text("words")
words[0]

first_annots=[]

rec=page1.first_annot.rect

rec

#Information of words in first object is stored in mywords

mywords = [w for w in words if fitz.Rect(w[:4]) in rec]

ann= make_text(mywords)

first_annots.append(ann)

def make_text(words):

    line_dict = {} 

    words.sort(key=lambda w: w[0])

    for w in words:  

        y1 = round(w[3], 1)  

        word = w[4] 

        line = line_dict.get(y1, [])  

        line.append(word)  

        line_dict[y1] = line  

    lines = list(line_dict.items())

    lines.sort()  

    return "n".join([" ".join(line[1]) for line in lines])

print(rec)
print(first_annots)
shuynh84
  • 59
  • 8

1 Answers1

0

right after this line:

doc = fitz.open('Mansfield--70-21009048 - ConvertToExcel.pdf')

add this to check if there is any annots in pdf, you might end up with no annotations at all in your pdf, so your page.first_annot is NoneType.

if doc.has_annots():

print("has annots")

else:

print("no annots")
david
  • 1
  • 1