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)

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐