Python3, 2 pieces of code, add watermark to pdf file, the original watermark can also play like this.

1 Introduction

Little Diaosi : Brother Yu, Happy New Year!
Xiaoyu : Don't go to the Three Treasures Hall for anything. If you have anything, just tell me...
Xiao Diaosi : Don't be so direct, it's a big New Year...
Xiaoyu : Don't be useless, just think carefully, I Still don't know.
Xiao Diaosi : ...
insert image description here
Xiao Diaosi : Despise it if you despise it, as long as you can help me solve the problem, I will make you despise Sanlian!
Xiaoyu : ... that's okay, then tell me, what's the matter?
Xiao Diaosi : It's just... it's just...
Xiaoyu : Can you stop chasing like this, and get rid of it right away! !
Xiao Diaosi : Yes, can you add a watermark to the pdf file for me...
Xiaoyu : That's it...
Xiao Diaosi : Yes, that's it!

insert image description here
Xiaoyu : Don't make such an innocent and cute little expression.
Xiao Diaosi : Who used to be not a cute little ^ ^
Xiaoyu : I wiped it, I broke the defense, I can't do it!
Little Diaosi : Nice~~

Today we will share how to add watermark to pdf document.
Xiaoyu shared the method of adding watermarks to pictures. It is very simple. You can directly add_watermark of the watermark module.

If you don't know, you can read this " Python3, 2 lines of code to add a watermark, send it to a circle of friends, and the pictures are no longer afraid of being stolen! ! !

However, adding a watermark to a pdf file requires the use of two other libraries, namely:

  • reportlab
  • pikepdf

What special "magic" do these two libraries have to add watermark to pdf?
Don't go away, come back later!

2. Specify the watermark content to output to the pdf file

2.1 Module Installation

Because the reportlab library is a third-party library for python, the
first step is to install:

pip install reportlab

Other ways to install :

" Python3, choose Python to automatically install third-party libraries, and say goodbye to pip! ! "
" Python3: I only use one line of code to import all Python libraries! !

2.2 Ideas

1. To set the watermark font fill:
Therefore, we need to set some basic information on the font, such as:

  • content: watermark text content
  • filename: the exported watermark filename
  • width: canvas width, unit: mm
  • height: canvas height, unit: mm
  • font: corresponds to the registered font code
  • fontsize: font size
  • angle: rotation angle
  • text_stroke_color_rgb: text outline rgb color
  • text_fill_color_rgb: text fill rgb color
  • text_fill_alpha: text transparency

2. Output the watermark font to the pdf document
canvas.Canvas.save() method, save the output font to the pdf document

2.3 Code Examples

Go directly to the code:

# -*- 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) 
    

Running result :
insert image description here

3. Batch output watermark content to pdf files

3.1 Module Installation

Because the pikepdf library is a third-party library for python, the
first step is to install:

pip install pikepdf

Other ways to install :

" Python3, choose Python to automatically install third-party libraries, and say goodbye to pip! ! "
" Python3: I only use one line of code to import all Python libraries! !

Xiaodiaosi: Brother Yu, why do we still need to install the pikepdf module?
Xiaoyu : Because we want to overwrite the generated pdf watermark to the target pdf document.
Xiao Diaosi : Do you mean that the above code just generates a watermark document, and the others are gone?
Xiaoyu : Yes, you can use the pdf watermark document generated above, or you can find any pdf document as a watermark document to cover the target pdf document.
Little Diaosi : Can you still play like this?
Xiaoyu : Yes, I will show you later.

3.2 Ideas

1. You need to prepare a pdf file :

  • target pdf file
  • Watermarked pdf file

2. We overwrite the generated pdf document to the target pdf document. Similarly, the parameters that need to be set :

  • target_pdf_path: target pdf file path + file name
  • watermark_pad_path: watermark pdf file path + file name
  • nrow: the number of rows to tile the watermark
  • ncol: the number of columns to tile the watermark
  • skip_pages: The number of pages that need to be skipped without adding a watermark

Target pdf document :
insert image description here

3.3 Code Examples


# -*- 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])

Running result :

insert image description here

4. Summary

Writing here, today's sharing is almost over.
Today, the reportlab library and pikepdf library are mainly expanded, so that watermarks can be added without spending money.

Follow Xiaoyu Blog to learn more and more interesting python knowledge.

Guess you like

Origin blog.csdn.net/wuyoudeyuer/article/details/122858139