Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyMuPDF - read/write text box

Tags:

python

pdf

I've been able to read the content of PDFs with: PYMuPDF using code similar to the following:

myfile = r"C:\users\xxx\desktop\testpdf1.pdf"
doc  =fitz.open(myfile)
page=doc[1]
text = page.getText("text")

to read the contents of PDF files, however I can't read text box annotations is there a way to do this?

like image 654
Easynow Avatar asked Mar 28 '26 02:03

Easynow


1 Answers

Use firstAnnot on the page object. Once you have an annotation object it looks like you can call next on it and get the others. Note the example at the bottom of the Annot page.

I created a PDF from a Word document and added one text box and one sticky note. The following code printed the contents of each. Look inside info for other information you may want.

import fitz

pdf = fitz.open('WordTest.pdf')
page = pdf[0]
annot = page.firstAnnot
print(annot.info['content'])
next_annot = annot.next
print(next_annot.info['content'])
pdf.close()
like image 113
J. Owens Avatar answered Mar 31 '26 03:03

J. Owens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!