美文网首页
使用python去除pdf水印

使用python去除pdf水印

作者: DayDayUp_hhxx | 来源:发表于2022-09-09 16:41 被阅读0次

以下方法主要是处理在页眉页脚处的水印,其他情况未测试。
安装pypdf2

pip install pypdf2

主要步骤:
1.读取pdf文件;
2.遍历pdf文件;
3.分析文件结构,找到水印图片,删除;页面元素不会太多,多试两次就能找到对应元素;
4.输出到新文件;

from PyPDF2 import PdfFileReader,PdfFileWriter

output = PdfFileWriter()
with open("XXXX/xxxx.pdf",'rb') as pf:
    pin = PdfFileReader(pf)
    for i in range(pin.getNumPages()):       
        page = pin.getPage(i)
        page['/Resources']['/XObject']['/QQAPXO65685fd8'].clear()
        page['/Resources']['/XObject']['/QQAPXO49127425'].clear()    
        output.addPage(page)
    with open("XXXX/out.pdf",'wb') as ouf:
        output.write(ouf)

page['/Resources']即为页面结构,分析结构,找出其中需要删除的元素即可。

相关文章

网友评论

      本文标题:使用python去除pdf水印

      本文链接:https://www.haomeiwen.com/subject/ewqznrtx.html