热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

python批量添加水印_怎样用python给pdf批量添加水印并加密

很多时候需要给pdf添加水印,而且还要加密文件,这些在Python中是如何实现的呢?学过编程的小伙伴准备好迎接今天的挑战吧。1.设置路径

很多时候需要给pdf添加水印,而且还要加密文件,这些在Python中是如何实现的呢?学过编程的小伙伴准备好迎接今天的挑战吧。

1.设置路径

import os

os.getcwd()

os.chdir('E:\\python\\test\\pdf批量加水印\\')

先设置路径,把需要加水印的相关文档放入一个目录下。我的目录是:E:\python\test\pdf批量加水印os.chdir('E:\\python\\test\\pdf批量加水印\\')

2.准备水印pdf文件

from reportlab.pdfgen import canvas

from reportlab.lib.units import cm

from reportlab.pdfbase import pdfmetrics

from reportlab.pdfbase.ttfonts import TTFont

pdfmetrics.registerFont(TTFont('song', 'C:/Windows/Fonts/simsun.ttc'))#宋体

from PyPDF2 import PdfFileWriter,PdfFileReader

import xlrd

def create_watermark(content):

#默认大小为21cm*29.7cm

c = canvas.Canvas('mark.pdf', pagesize = (30*cm, 30*cm))

c.translate(10*cm, 10*cm) #移动坐标原点(坐标系左下为(0,0)))

c.setFont('song',22)#设置字体为宋体,大小22号

c.setFillColorRGB(0.5,0.5,0.5)#灰色

c.rotate(45)#旋转45度,坐标系被旋转

c.drawString(-7*cm, 0*cm, content)

c.drawString(7*cm, 0*cm, content)

c.drawString(0*cm, 7*cm, content)

c.drawString(0*cm, -7*cm, content)

c.save()#关闭并保存pdf文件

系统默认识别英文作为水印,但若水印为中文会无法显示。解决办法是先from reportlab.pdfbase.ttfonts import TTFont

然后找到电脑中字体路径,如我希望找到宋体,路径为“C:/Windows/Fonts/simsun.ttc”,命名为"song"(如下图所示,其他字体也可任君挑选)。

应用到后续create_watermarkh函数中即可:c.setFont('song',22)#设置字体为宋体,大小22号

1603958934813535.png

另,希望页面上贴四个水印,通过函数c.drawString(-7*cm, 0*cm, content)

改变坐标重复4次便可实现。由此最终生成水印pdf文件。

1603958985519600.png

3.准备水印pdf文件

def add_watermark2pdf(input_pdf,output_pdf,watermark_pdf):

watermark = PdfFileReader(watermark_pdf)

watermark_page = watermark.getPage(0)

pdf = PdfFileReader(input_pdf,strict=False)

pdf_writer = PdfFileWriter()

for page in range(pdf.getNumPages()):

pdf_page = pdf.getPage(page)

pdf_page.mergePage(watermark_page)

pdf_writer.addPage(pdf_page)

pdfOutputFile = open(output_pdf,'wb')

pdf_writer.encrypt('scb2018')#设置pdf密码

pdf_writer.write(pdfOutputFile)

pdfOutputFile.close()

只要安装了该安装的模块,这一步骤基本没有什么问题,提醒给pdf设置密码的语法为.encrypt('scb2018')#设置pdf密码

若需更改密码,改变引号中内容即可。注:input_pdf为需要打上水印的pdf,watermark_pdf为水印pdf,output_pdf为最终输出的pdf。

4.准备水印pdf文件

ExcelFile = xlrd.open_workbook('商家名单.xlsx')

sheet=ExcelFile.sheet_by_name('Sheet2')#打开有商家名单那个sheet

print('———————已导入商家名单———————')

col = sheet.col_values(3)#第4列内容为商家名称

id = sheet.col_values(0)#第1列内容为ID

del col[0];del id[0]#去掉标题

id2 = [str(int(i)) for i in id]

merchant_as_mark_cOntent=[(i+&#39; &#39;)*4 if len(i)<=5 else i for i in col]#如果名称太短则重复4个为一行

我是放在一个excel中的,截图入下,需要把第4列商家名称作为水印内容印到目标pdf上,对应代码为sheet.col_values(3)

1603959158285700.png

5.调用函数最终批量生成想要的pdf

if __name__==&#39;__main__&#39;:

for i,j,k in zip(merchant_as_mark_content,,id2):#i制作水印,j文件名,k对应ID

create_watermark(i)#创造了一个水印pdf:mark.pdf

add_watermark2pdf(&#39;需要加水印的源文件.pdf&#39;,k+&#39;通知(&#39;+j+&#39;).pdf&#39;,&#39;mark.pdf&#39;)

print(&#39;———————已制作好第&#39;+k+&#39;个pdf,正在准备下一个———————&#39;)

print(&#39;———————所有文件已转化完毕———————&#39;)

调用本步骤时我遇到一个错误UnicodeEncodeError: &#39;latin-1&#39; codec can&#39;t encode characters in position 8-9: ordinal not in range(256)

说什么latin-1不能编码字符,是个编码问题。解决办法:找到PyPDF2下utils.py的238行,我的路径为:D:\Program Files (x86)\Python\lib\site-packages\PyPDF2\utils.py。然后把r = s.encode(&#39;latin-1&#39;)

替换为如下代码即可try:

r = s.encode(&#39;latin-1&#39;)

if len(s) <2:

bc[s] = r

return r

except Exception as e:

print(s)

r = s.encode(&#39;utf-8&#39;)

if len(s) <2:

bc[s] = r

return r

到此所有程序已梳理完毕,所遇问题已解决,大家就可以愉快的打水印了!我出来的效果

1603959419868425.png

1603959433362433.png

今天的内容比较复杂,没学会的小伙伴可以再多练习几遍。更多Python学习推荐:PyThon学习网教学中心。


推荐阅读
author-avatar
zhaoyunnidaye_260
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有