I'm using PyMuPDF to replace images. But when I have a dictionary of images mapped to their bbox coordinates only the image in the first page gets replaced. How can I get all the images in the dictionary to be replaced? Here's my code: 'bbval' is the dictionary that looks like this: bbval dictionary
import fitz
src_pdf_filename = file_path
dst_pdf_filename = '/content/dest.pdf' #destination pdf
img_filename = '/content/logo.jpg' #the image that will be replacing the old ones
test=[]
val=0
for k in bbval:
test.append(str(bbval[i])[5:int(str(bbval[i]).index(str(bbval[i])[-1]))])
string=test[val].split(',')
img_rect = fitz.Rect(float(string[0]),float(string[1]),float(string[2]),float(string[3]))#str(bbval[i])[5:int(str(bbval[i]).index(str(bbval[i])[-1]))]
#print(bbval[k])
document = fitz.open(src_pdf_filename)
page = document[int(k[k.index('e')+1:k.index('-')])]
#print('page: ',page)
page.clean_contents()
page.insertImage(img_rect, filename=img_filename,keep_proportion=False)
val+=1
document.save(dst_pdf_filename)
document.close()