Easynow
Easynow

Reputation: 201

PyMuPDF - read/write text box

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?

Upvotes: 3

Views: 4441

Answers (1)

J. Owens
J. Owens

Reputation: 852

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()

Upvotes: 3

Related Questions