在Python中创建PDF签名

本文介绍了一个使用Python的os和PyPDF2库创建PDF文件签名的函数,通过读取模板PDF和签名PDF,将签名页合并到模板上,生成已签名的PDF文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import os
from PyPDF2 import PdfFileReader, PdfWriter


def create_sign(input_pdf, sign):
    work_path = "C:\\Leon\\pdf_signature"
    input_file_path = os.path.join(work_path, input_pdf)
    sign_file_path = os.path.join(work_path, sign)

    # 检查文件是否存在
    if not os.path.exists(input_file_path) or not os.path.exists(sign_file_path):
        print(f"One or both files do not exist: {input_file_path}, {sign_file_path}")
        return None

    try:
        # 读取PDF文件
        template_pdf = PdfFileReader(input_file_path)
        sign_pdf = PdfFileReader(sign_file_path)

        # 检查PDF文件是否有效
        if template_pdf.getNumPages() == 0 or sign_pdf.getNumPages() == 0:
            print("One or both PDF files are empty.")
            return None

        pdf_writer = PdfWriter()
        template_pdf_page = template_pdf.getPage(-1)
        sign_pdf_page = sign_pdf.pages[0]
        sign_pdf_page.mergePage(template_pdf_page)
        pdf_writer.add_page(sign_p
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值