pymupdf文档地址
https://pymupdf.readthedocs.io/en/latest/index.html
import fitz # PyMuPDF
def add_watermark(pdf_path, output_path, watermark_text):
doc = fitz.open(pdf_path)
#doc = fitz.open("pdf",pdfStream(pdf)) #加载文件流
w = 595
h = 40
for page in doc:
width, height = page.mediabox.width, page.mediabox.height
rect = fitz.Rect(width-50, 0, w, h) #[水印位置](https://pymupdf.readthedocs.io/en/latest/rect.html#rect)
page.insert_image(rect, filename=watermark_text,overlay=True)
doc.save(output_path)
doc.close()
# 调用
pdf_path1 = 'D:\python7.5\output_with_watermark.pdf'
output_path1 = 'D:\python7.5\modified_example.pdf'
watermark_text1 = "D:\python7.5\watermark.png" # 水印
add_watermark(pdf_path1, output_path1, watermark_text1)