本文最后更新于836 天前,其中的信息可能已经过时,如有错误请发送邮件到blue16@email.swu.edu.cn
(其实之前有好几篇文章已经发布了,可是这个让人无语的服务器丢数据)
这里我是使用Python实现的,其实这个加水印反而是帮助我去掉一些水印(添加白色水印然后盖住已有的水印哈哈)
首先你需要一个Python环境,百度一下就能解决
然后在下一个pip
解压,进入目录,在该目录下打开CMD(或者shift+右键),执行命令
python setup.py install
输入命令python setup.py install进行安装
再安装一个依赖pypdf2
pip install pypdf2
当然如果有新版本你也可以升级
pip install --upgrade pip

接下来开始码代码,首先说一下,这个算法实现的功能主要是
1.遍历目录,寻找所有pdf文件
2.给PDF加上程序同目录的水印文件
下面附上我写的代码
#encoding=utf-8
#author: blue16
#date: 2021-4-5
#summary: 遍历本目录所有文件,去除PDF文件右上角水印
import sys
import os
import os.path
import shutil
from PyPDF2 import PdfFileReader, PdfFileWriter
def extract_information(pdf_path):
with open(pdf_path, 'rb') as f:
pdf = PdfFileReader(f)
information = pdf.getDocumentInfo()
number_of_pages = pdf.getNumPages()
txt = f"""
Information about {pdf_path}:
Author: {information.author}
Creator: {information.creator}
Producer: {information.producer}
Subject: {information.subject}
Title: {information.title}
Number of pages: {number_of_pages}
"""
print(txt)
return information
def create_watermark(input_pdf, output, watermark):
watermark_obj = PdfFileReader(watermark)
watermark_page = watermark_obj.getPage(0)
pdf_reader = PdfFileReader(input_pdf)
pdf_writer = PdfFileWriter()
# 给所有页面添加水印
for page in range(pdf_reader.getNumPages()):
page = pdf_reader.getPage(page)
page.mergePage(watermark_page)
pdf_writer.addPage(page)
with open(output, 'wb') as out:
pdf_writer.write(out)
if __name__ == '__main__':
#读入指定目录并转换为绝对路径
dirname, filename = os.path.split(os.path.abspath(sys.argv[0]))
os.path.realpath(sys.argv[0])
rootdir = dirname
rootdir = os.path.abspath(rootdir)
print('absolute root path:\n*** ' + rootdir + ' ***')
#先修改文件名
for parent, dirnames, filenames in os.walk(rootdir):
#filename文件名 parent所在路径 pathfile 绝对路径
for filename in filenames:
pathfile = os.path.join(parent, filename)#路径名
print(pathfile[0])
if filename[0] != "_" and pathfile[-3:]=="pdf" and filename != "watermark.pdf":
#os.rename(pathfile, pathfile+"_p")
print("Proccess:"+pathfile)
output= os.path.join(parent,"_"+filename)
create_watermark(pathfile,
output,
watermark='watermark.pdf')
if os.path.isfile(output):
os.remove(pathfile)
print("Output:"+output)
else:
print("Skip:"+pathfile)
continue
这个算法其实是用来批量处理我的错题集的,下面是我的错题目录




