pdf批量加水印及加密、解密
1)批量加水印
from PyPDF2 import PdfFileReader, PdfFileWriter
from copy import copy
water = PdfFileReader(r"G:\6Tipdm\7python办公自动化\concat_pdf\水印.pdf")
water_page = water.getPage(0)
pdf_reader = PdfFileReader(r"G:\6Tipdm\7python办公自动化\concat_pdf\aa.pdf")
pdf_writer = PdfFileWriter()
for page in range(pdf_reader.getNumPages()):
my_page = pdf_reader.getPage(page)
new_page = copy(water_page)
new_page.mergePage(my_page)
pdf_writer.addPage(new_page)
with open("G:\\6Tipdm\\7python办公自动化\\concat_pdf\\添加水印后的aa.pdf", "wb") as out:
pdf_writer.write(out)
"""
这里有一点需要注意:进行pdf合并的时候,我们希望“水印”在下面,文字在上面,因此是“水印”.mergePage(“图片页”)
"""
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
结果如下:
2)批量加密、解密
这里所说的“解密”,是在知道pdf的密码下,去打开pdf,而不是暴力破解;
① 加密pdf
from PyPDF2 import PdfFileReader, PdfFileWriter
pdf_reader = PdfFileReader(r"G:\6Tipdm\7python办公自动化\concat_pdf\时间序列.pdf")
pdf_writer = PdfFileWriter()
for page in range(pdf_reader.getNumPages()):
pdf_writer.addPage(pdf_reader.getPage(page))
# 添加密码
pdf_writer.encrypt("a123456")
with open("G:\\6Tipdm\\7python办公自动化\\concat_pdf\\时间序列.pdf", "wb") as out:
pdf_writer.write(out)
1
2
3
4
5
6
7
8
9
10
结果如下:
② 解密pdf并保存为未加密的pdf
from PyPDF2 import PdfFileReader, PdfFileWriter
pdf_reader = PdfFileReader(r"G:\6Tipdm\7python办公自动化\concat_pdf\时间序列.pdf")
# 解密pdf
pdf_reader.decrypt("a123456")
pdf_writer = PdfFileWriter()
for page in range(pdf_reader.getNumPages()):
pdf_writer.addPage(pdf_reader.getPage(page))
with open("G:\\6Tipdm\\7python办公自动化\\concat_pdf\\未加密的时间序列.pdf", "wb") as out:
pdf_writer.write(out)
1
2
3
4
5
6
7
8
9
10
网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。
添加我为好友,拉您入交流群!
请使用微信扫一扫!