Python AttributeError: 'Page' object has no attribute 'insertImage'

Viewed 72

I'am trying to add a png sign to the PDF by using a python code and the code that i am running is I am using PyMuPDF and have used fitz library.

import fitz

input_file = "example.pdf"
output_file = "example-with-sign.pdf"
barcode_file = "sign.png"

# define the position (upper-right corner)
image_rectangle = fitz.Rect(450,20,550,120)

# retrieve the first page of the PDF
file_handle = fitz.open(input_file)
first_page = file_handle[0]

# add the image
first_page.insertImage(image_rectangle, fileName=barcode_file)

file_handle.save(output_file)
1 Answers

Thank you for 'insert_image' correction. It currently works as follows:

import fitz

input_file = "example.pdf"
output_file = "example-with-sign.pdf"


# define the position (upper-right corner)
image_rectangle = fitz.Rect(450,20,550,120)

# retrieve the first page of the PDF
file_handle = fitz.open(input_file)
first_page = file_handle[0]

img = open("sign.png", "rb").read()  # an image file
img_xref = 0

first_page.insert_image(image_rectangle,stream=img,xref=img_xref)

file_handle.save(output_file)
Related