How to debug - no insert_text found in new pdf

Hi, my goal is to write text to a pdf. When I open the pdf I only see a white page.
I think I have an error in my setup and not in my code. Below a minimal working example.

Q: How can I debug this, to find out what is going on?

Thanks, regards Edzo

  • Linux fedora 6.17.7-300.fc43.x86_64 #1 SMP PREEMPT_DYNAMIC Sun Nov 2 15:30:09 UTC 2025 x86_64 GNU/Linux
  • Python 3.14.0
  • pymupdf 1.26.6

MWE

 import pymupdf from pymupdf.utils import getColor # set defaults file_name = “PyMuPDF_test.pdf” text_color = getColor(“black”) font_size = 32.0 font_name = “spacemo” p = pymupdf.Point(10, 10) # debug reasons text = “Some text.” # create pdf newDocument = pymupdf.open() fmt = pymupdf.paper_rect(“a4”) newPage = newDocument.new_page( width = fmt.width, height = fmt.height) # write to new page newPage.clean_contents() newPage.insert_text = ( p, text, fontsize := font_size, fontname := font_name, color := text_color, border_width := 1, overlay := True ) # write to disk newDocument.save(file_name, garbage = 3, deflate = True,) newDocument.close() 

Hi @edzob Welcome to the forum!

looking at your code doesn’t newPage.insert_text = ( ... redefine the function?

If I do:

 newPage.insert_text( p, text, fontsize = font_size, color = text_color, border_width = 1, overlay = True ) 

Then it works okay.

Additional comments:
Please do not use page.clean_contents() here. This is no longer necessary since a many versions.

Dear @Jamie_Lemon . TY for your advice.

I thought at the start also this was the issue, but with this code I recieve a strange error. I thought I sort of had a new version of test_insert that needed a different syntax. Apparantly that is not the issue.

I also tried to remove all the spaces and pot it on 1 line, same error.

So I am a bit stuck

@HaraldLieder : Thanks. I removed the line in the code below.

Error

$ python3 write_pdf.py File "******/write_pdf.py", line 20 fontsize = font_size, ^^^^^^^^^^^^^^^^^^^^^^^^ SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? $ 

With code

import pymupdf from pymupdf.utils import getColor # set defaults file_name = "PyMuPDF_test.pdf" text_color = getColor("black") font_size = 32.0 font_name = "spacemo" p = pymupdf.Point(10, 10) # debug reasons text = "Some text." # create pdf newDocument = pymupdf.open() fmt = pymupdf.paper_rect("a4") newPage = newDocument.new_page(width = fmt.width, height = fmt.height) # write to new page newPage.insert_text = ( p, text, fontsize = font_size, fontname = font_name, color = text_color, border_width = 1, overlay = True ) # write to disk newDocument.save(file_name, garbage = 3, deflate = True,) newDocument.close() 

I think this should be:
newPage.insert_text( ...

1 Like

@edzob Please also confirm your version of PyMuPDF that you are using!

haha .. the = should be the thing. I will test..
my dyslexia hits hard in this one

1 Like

@Jamie_Lemon the wrong calling of the function was the error. TY so much.
I had no clue where to find the error and now I know a little bit more.

The version of pymupdf is 1.26.6

the code now is..

import pymupdf from pymupdf.utils import getColor # set defaults file_name = “PyMuPDF_test.pdf” text_color = getColor(“black”) font_size = 32.0 font_name = “spacemo” p = pymupdf.Point(10, 10) # debug reasons text = “Some text.” # create pdf newDocument = pymupdf.open() fmt = pymupdf.paper_rect(“a4”) newPage = newDocument.new_page( width = fmt.width, height = fmt.height ) # write to new page newPage.insert_text( p, text, fontsize = font_size, fontname = font_name, color = text_color, border_width = 1, overlay = True ) # write to disk newDocument.save(file_name, garbage = 3, deflate = True,) newDocument.close() 

@edzob No worries! Glad you got it working okay.