我正在使用 PyMuPDF 替换图像。但是当我有一个映射到它们的 bbox 坐标的图像字典时,只有第一页中的图像被替换。如何让字典中的所有图像都被替换?这是我的代码:'bbval' 是这样的字典: 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()