You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello,
I would like to extract the supplier's information (top left block: Bourgeois Frères, . (see attached image) from the attached pdf.
What is the solution?
Os : MacOs Bigsur 11.6
Python : 3.9
PyMuPDF : 1.19.0
>>>importfitz>>>doc=fitz.open("BOURGEOISFacture.21053886.pdf")
>>>page=doc[0]
>>>frompprintimportpprint>>># no standard images there:>>>pprint(page.get_images())
[]
>>># neither embedded images:>>>pprint(page.get_image_info())
[]
And you will have determined that what seems to be text neither is text.
>>>page.get_text(sort=True) # "sort" ensures text visible at the top indeed comes firstBENOITCASTELMENILMONTANTBoulangeriePâtisserie150ruedeMénilmontant75020PARIS0621082968Tél. Client :
FACTURE/1
... (moredata) ...
So the apparent text must be encoded as drawing primitives, like a capital "B" drawn as a vertical line | followed by two little semi-circles above each other, etc.
The only way is therefore OCRing this area. Let's find a suitable sub-rectangle: left of "BENOIT CASTEL MENILMONTANT" and above the first "FACTURE":
>>>rl1=page.search_for("BENOIT CASTEL MENILMONTANT")
>>>len(rl1)
3>>>rl2=page.search_for("FACTURE")
>>>len(rl2)
2>>># sort both rectangle lists to be sure: vertical, then horizontal>>>rl1.sort(key=lambdar: (r.y1, r.x0))
>>>rl2.sort(key=lambdar: (r.y1, r.x0))
>>># right border is left or first rl1 rect:>>>rborder=rl1[0].x0>>># bottom is top coord of first rect of rl2:>>>bottom=rl2[0].y0>>># define sub rect to OCR:>>>clip=fitz.Rect(0,0,rborder,bottom)
>>>clipRect(0.0, 0.0, 339.3900146484375, 220.2386474609375)
>>># make a pixmap of that rect:>>>pix=page.get_pixmap(dpi=300,clip=clip)
>>># make a new 1 page PDF with OCRed text>>>pdfbytes=pix.pdfocr_tobytes()
>>>ocrpdf=fitz.open("pdf", pdfbytes)
>>>ocrpage=ocrpdf[0]
>>>print(ocrpage.get_text())
BourgeoisFréresS.A.SauCapitalde330000Euro77510VERDELOTTEL
: 0164048104FAX
: 0164048143SITEINTERNET
: WWW.MOULINS-BOURGEOIS.COMN° SIRET74605008700012R.C. MEAUXB746050087CODEAPE: 1061AN° deTVAINTRACOMMUNAUTAIRE
: FR15746050087>>>
Of course that OCRed text could also have been extracted with coordinates, e.g. using ocrpage.get_text("dict").
Those coordinates obviously are relative to that ocrpage's dimensions. If required translate them back to the original page's positions using the matrix mat = ocrpage.rect.torect(clip).
Thanks a lot
Which package must be imported to works?
Sincerely
Yves Larbodiere
EVARD
3, rue des Courtes Terres
95220 HERBLAY
portable : 07 81 08 41 00
mail : ***@***.***
Activity
JorjMcKie commentedon Jan 21, 2022
First problem is, that it is no image.
And you will have determined that what seems to be text neither is text.
So the apparent text must be encoded as drawing primitives, like a capital "B" drawn as a vertical line
|followed by two little semi-circles above each other, etc.The only way is therefore OCRing this area. Let's find a suitable sub-rectangle: left of "BENOIT CASTEL MENILMONTANT" and above the first "FACTURE":
JorjMcKie commentedon Jan 21, 2022
Of course that OCRed text could also have been extracted with coordinates, e.g. using
ocrpage.get_text("dict").Those coordinates obviously are relative to that ocrpage's dimensions. If required translate them back to the original page's positions using the matrix
mat = ocrpage.rect.torect(clip).Larbo53 commentedon Jan 21, 2022