用Python对PDF去水印(终极版)原则上所有水印都可去除(Pdftron)

本人一直想做DWG文件批量处理成PDF文件,无意中从网上查找到Pdftron这个软件,当我用DWG批量处理成PDF文件时,用demo试用版本发现导出的文件会出现如下图的水印

 那如果我们运用时需要去除上面的水印,鄙人从网上查阅好多资料都是转成图片再根据色差去除水印然后再转回pdfk但是会得到以下效果

 可以看出pdf非常不清晰,这不能到达我们的需求,究其根本原因就是不能从pdf本身,解决这些问题。我在想能不能从pdf本身来解析其中信息,找到水印的信息直接删除呢,最终运用PyPDF4达到自己的目的,这里代码只列出一个文件去除pdf水印,多文件就需要写几个for循环即可,这对于大家来说小菜一碟,这里这是给大家提供一个可靠的思路,原则上所有水印即文字的,图画的,图形的都可以去除代码如下:

import os
from PyPDF4 import PdfFileReader,PdfFileWriter
output = PdfFileWriter()
pdf = PdfFileReader("C:/Users/13283/Desktop/hahaha/output/Drawing1.pdf", 'rb')
pdf1=pdf.getPage(0)
pdf1['/Resources']['/XObject']['/Trn3dK9'].clear()
output.addPage(pdf1)
with open("C:/Users/13283/Desktop/hahaha/output/out.pdf",'wb') as ouf:
      output.write(ouf)

其中"C:/Users/13283/Desktop/hahaha/output/Drawing1.pdf"为带有水印的文件pdf1=pdf.getPage(0)为第一页,因为这里只有一页,多页的话直接遍历就可,因为水印的形式大多数都差不多,遍历都就可以都删除pdf1['/Resources']为页面结构,本人经验来说大多数元素都在pdf1['/Resources']['/XObject']之中,其中['/Trn3dK9']就为中间的水印,我们把它删除掉再加入pdf页面中水印就消失。下图为处理之后的

​​​​​​​

 

 是不是和原来一样。

这里说一下,Pdftron是商业软件,此方法仅供学习使用禁止商用,谢谢。

如果你你们满意的话,请多多支持,本人为R语言与python学习者,有啥问题可私信一起学习哈。

  • 5
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Paperback: 172 pages Publisher: Packt Publishing (December 30, 2015) Language: English ISBN-10: 1785885995 ISBN-13: 978-1785885990 Key Features Write smarter, bug-free, high performance code with minimal effort Uncover the best tools and options available to Python developers today Deploy decorators, design patters, and various optimization techniques to use Python 3.5 effectively Book Description Python is a versatile programming language that can be used for a wide range of technical tasks―computation, statistics, data analysis, game development, and more. Though Python is easy to learn, it's range of features means there are many aspects of it that even experienced Python developers don't know about. Even if you're confident with the basics, its logic and syntax, by digging deeper you can work much more effectively with Python – and get more from the language. Python Unlocked walks you through the most effective techniques and best practices for high performance Python programming - showing you how to make the most of the Python language. You'll get to know objects and functions inside and out, and will learn how to use them to your advantage in your programming projects. You will also find out how to work with a range of design patterns including abstract factory, singleton, strategy pattern, all of which will help make programming with Python much more efficient. Finally, as the process of writing a program is never complete without testing it, you will learn to test threaded applications and run parallel tests. If you want the edge when it comes to Python, use this book to unlock the secrets of smarter Python programming. What you will learn Manipulate object creation processes for instances, classes, and functions Use the best possible language constructs to write data structures with super speed and maintainability Make efficient use of design patterns to decrease development time and make your code more maintainable Write better test cases with an impro

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值