用Python将docx转换为PDF格式并添加水印、设置PDF安全策略

在日常工作中,我们经常需要对PDF文件进行一些处理,比如添加水印、加密保护等。本文将介绍如何使用Python中的PyPDF2库来添加水印和保护PDF文件。

安装PyPDF2、docx2pdf库

首先,我们需要安装PyPDF2、docx2pdf库。可以使用pip命令来进行安装:

pip install PyPDF2
pip install docx2pdf

这里我的版本是
docx2pdf: 0.1.8
PyPDF2:3.0.1

docx文件转换为PDF文件

import docx2pdf

	# 解析命令行参数
	input_file = sys.argv[1]
	# 将Word文档转换为PDF
    docx2pdf.convert(input_file)

添加水印

添加水印是指在PDF文件中的每一页都加上一个背景图或者文字。这样做的目的可以是为了防止文件被恶意复制或者传播。

我们可以使用PyPDF2库中的PdfReader和PdfWriter类来读取和写入PDF文件。下面是一个添加水印的例子:

import PyPDF2

def add_watermark(input_pdf, output_pdf, watermark_text):
    # 加水印
    watermark = PyPDF2.PdfReader(open(watermark_text, "rb"))
    output = PyPDF2.PdfWriter()

    with open(input_pdf, "rb") as input_file:
        pdf = PyPDF2.PdfReader(input_file)

        for page in range(len(pdf.pages)):
            page = pdf.pages[page]
            page.merge_page(watermark.pages[0])
            output.add_page(page)

        with open(output_pdf, "wb") as output_file:
            output.write(output_file)

    return output_pdf

在这个例子中,我们读取了一个PDF文件,然后使用PdfWriter创建一个新的PDF文件。接着,我们读取了一个水印文件,然后将它添加到每一页中。最后,我们将新的PDF文件写入到磁盘上。

保护PDF文件

保护PDF文件是指对PDF文件进行加密,使得只有授权的用户才能打开或者修改它。在PyPDF2库中,我们可以使用PdfFileWriter类的encrypt方法来实现这个功能。下面是一个例子:

import PyPDF2

def apply_security_policy(input_pdf, output_pdf, password):
    # 加安全保护
    with open(input_pdf, "rb") as input_file:
        pdf = PyPDF2.PdfReader(input_file)
        output = PyPDF2.PdfWriter()

        for i in range(len(pdf.pages)):
            output.add_page(pdf.pages[i])

        # 设置PDF权限:不允许修改、不允许提取页面,输入密码后可完全控制文件
        output.encrypt(user_password='', owner_password=password, use_128bit=True, permissions_flag=-11)

        with open(output_pdf, "wb") as output_file:
            output.write(output_file)

    return output_pdf

在这个例子中,我们使用PdfReader类来读取PDF文件。接着,我们使用PdfWriter创建一个新的PDF文件。然后,我们将原始PDF文件中的所有页添加到新的PDF文件中。最后,我们使用encrypt方法来加密PDF文件并将其写入到磁盘上。

main函数的写法

读取传入的参数(docx文件路径),将docx转换为pdf,加水印,加安全策略保护。

import sys
import os
import docx2pdf
import PyPDF2

if __name__ == '__main__':
    # 解析命令行参数
    input_file = sys.argv[1]
    output_file = os.path.splitext(input_file)[0] + '_converted.pdf'
    # 读取水印文件watermark_file.pdf,为确保水印效果,pdf尺寸需要与目标pdf一致
    watermark_file = 'C:\\Users\\xxx\\Desktop\\WORD2PDFnwp\\watermark_file.pdf'
    password = 'abc852456'

    # 将Word文档转换为PDF
    docx2pdf.convert(input_file)

    # 加水印
    add_watermark(input_file.replace('.docx', '.pdf'), output_file, watermark_file)

    # 加安全保护
    apply_security_policy(output_file, output_file, password)

    print(f'处理完成,生成文件为:{output_file}')
	# 删除过程文档
    os.remove(os.path.splitext(input_file)[0] + '.pdf')

bat文件的写法

用bat文件启动python程序,能实现windows环境下docx文件拖拽到bat文件上执行程序,提升效率

@echo off

REM 检查是否拖放了一个文件
if [%1] == [] (
    echo Please drag and drop a file onto this script.
    pause
    exit /b 1
)

REM 检查文件是否存在
if not exist %1 (
    echo The specified does not exist.
    pause
    exit /b 1
)

REM 运行 Python 程序
python C:\Users\xxx\Desktop\WORD2PDFnwp\add_watermark.py %1

本文基本上由ChatGPT生成,我修正了一些代码上的错误,补充了一些章节,供参考。

更多推荐

用Python将docx转换为PDF格式并添加水印、设置PDF安全策略