小屌丝:鱼哥,新年快乐!
小鱼:无事不登三宝殿,有啥事,你直说吧…
小屌丝:别说的这么直接,这大过年的…
小鱼:别整没用的,就你那点小心思,我还能不知道。
小屌丝:…
小屌丝:鄙视就鄙视,只要能帮我解决问题,我然你鄙视三连!
小鱼:…还可以这样,那你说吧,啥事?
小屌丝:就是…就是…
小鱼:能不能不这么磨磨唧唧,赶紧立马撒冷的!!
小屌丝:就是,能不能帮我给pdf文件加个水印…
小鱼:就这…
小屌丝:对,就这!
小鱼:别整这么无辜又可爱的小表情。
小屌丝:谁曾经还不是个小可爱 ^ ^
小鱼:我擦,破防了,我整还不行嘛!
小屌丝:奈斯 ~ ~
今天我们就来分享 pdf文档如何添加水印。
小鱼分享过图片添加水印的方法,很简单,直接watermark模块的add_watermark 就可以,
如果不知道的,可以看这篇《Python3,2行代码添加水印,发朋友圈,图片再也不怕被盗了!!!》
但是,pdf文件添加水印,就需要用到另外两个库,即:
这两个库有什么特殊的"魔法",可以给pdf添加水印呢?
别走开,稍后回来!
因为reportlab库是python的第三方库,所以,
第一步,安装:
1 | pip install reportlab |
其他方式安装:
《Python3,选择Python自动安装第三方库,从此跟pip说拜拜!!》
《Python3:我低调的只用一行代码,就导入Python所有库!!》
1、要设置水印字体填充:
所以,我们就需要对字体设置一些基本信息,例如:
2、输出水印字体到pdf文档
canvas.Canvas.save()方法,把输出的字体保存到pdf文档
直接上代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | # -*- coding:utf-8 -*- # @Time : 2022-02-10 # @Author : carl_DJ from typing import Union, Tuple from reportlab.lib import units from reportlab.pdfgen import canvas from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.ttfonts import TTFont pdfmetrics.registerFont(TTFont( 'msyh' ,r './msyh.ttc' )) ''' 用于生成包含content文字内容的水印pdf文件 content: 水印文本内容 filename: 导出的水印文件名 width: 画布宽度,单位:mm height: 画布高度,单位:mm font: 对应注册的字体代号 fontsize: 字号大小 angle: 旋转角度 text_stroke_color_rgb: 文字轮廓rgb色 text_fill_color_rgb: 文字填充rgb色 text_fill_alpha: 文字透明度 ''' def create_wartmark(content: str , filename: str , width: Union[ int , float ], height: Union[ int , float ], font: str , fontsize: int , angle: Union[ int , float ] = 45 , text_stroke_color_rgb: Tuple [ int , int , int ] = ( 0 , 0 , 0 ), text_fill_color_rgb: Tuple [ int , int , int ] = ( 0 , 0 , 0 ), text_fill_alpha: Union[ int , float ] = 1 ) - > None : #创建PDF文件,指定文件名及尺寸,以像素为单位 c = canvas.Canvas(f '{filename}.pdf' ,pagesize = (width * units.mm,height * units.mm)) #画布平移保证文字完整性 c.translate( 0.1 * width * units.mm, 0.1 * height * units.mm) #设置旋转角度 c.rotate(angle) #设置字体大小 c.setFont(font,fontsize) #设置字体轮廓彩色 c.setStrokeColorRGB( * text_stroke_color_rgb) #设置填充色 c.setFillColorRGB( * text_fill_color_rgb) #设置字体透明度 c.setFillAlpha(text_fill_alpha) #绘制字体内容 c.drawString( 0 , 0 ,content) #保存文件 c.save() create_wartmark(content = '关注carl_奕然,学习更多有趣的python知识' , filename = '小鱼watermarkDemo' , width = 200 , height = 200 , font = 'msyh' , fontsize = 35 , text_fill_alpha = 0.3 ) |
运行结果:
因为pikepdf库是python的第三方库,所以,
第一步,安装:
1 | pip install pikepdf |
其他方式安装:
《Python3,选择Python自动安装第三方库,从此跟pip说拜拜!!》
《Python3:我低调的只用一行代码,就导入Python所有库!!》
小屌丝:鱼哥,为什么我们还需要安装pikepdf这个模块?
小鱼:因为我们要把已生成的pdf水印覆盖到目标pdf文档。
小屌丝:你的意思是,上面的代码,就仅仅是生成一个水印文档,其他的没有了?
小鱼:对的,你可以使用上面已生成的pdf水印文档,也可以随便找一个pdf文档作为水印文档,去覆盖到目标pdf文档。
小屌丝:还可以这么玩?
小鱼:对的,待会给你演示。
1、需要准备pdf文件:
2、我们把已生成的pdf文档来覆盖到目标pdf文档,同样,需要设置的参数:
目标pdf文档:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | # -*- coding:utf-8 -*- # @Time : 2022-02-10 # @Author : carl_DJ from typing import List from pikepdf import Pdf,Page,Rectangle ''' 向目标pdf文件批量添加水印 target_pdf_path:目标pdf文件路径+文件名 watermark_pad_path:水印pdf文件路径+文件名 nrow:水印平铺的行数 ncol:水印平铺的列数 skip_pages:需要跳过不添加水印的页数 ''' def add_watemark(target_pdf_path: str , watermark_pdf_path: str , nrow: int , ncol: int , skip_pages: List [ int ] = []) - > None : #选择需要添加水印的pdf文件 target_pdf = Pdf. open (target_pdf_path) #读取水印pdf文件并提取水印 watermark_pdf = Pdf. open (watermark_pdf_path) watermark_page = watermark_pdf.pages[ 0 ] #遍历目标pdf文件中的所有页,批量添加水印 for idx,target_page in enumerate (target_pdf.pages): for x in range (ncol): for y in range (nrow): #向目标页指定范围添加水印 target_page.add_overlay(watermark_page, Rectangle(target_page.trimbox[ 2 ] * x / ncol, target_page.trimbox[ 3 ] * y / nrow, target_page.trimbox[ 2 ] * (x + 1 ) / ncol, target_page.trimbox[ 3 ] * (y + 1 ) / nrow )) #保存PDF文件,同时对pdf文件进行重命名,从文件名第7位置写入后缀名 target_pdf.save(target_pdf_path[: 6 ] + '_已添加水印.pdf' ) add_watemark(target_pdf_path = '跟小鱼学水印.pdf' , #把生成的水印示例,添加到目标水印文件中 watermark_pdf_path = '小鱼watermarkDemo.pdf' , nrow = 3 , ncol = 2 , skip_pages = [ 0 ]) |
运行结果:
写到这里,今天的分享就差不多快结束了。
今天主要是对reportlab 库和pikepdf库进行了拓展,让添加水印,不在花钱。
以上就是2行Python代码实现给pdf文件添加水印的详细内容
联系客服