python 在pdf指定位置添加公章或者照片

<>-- coding: utf-8 --

import fitz
import os
from django.conf import settings
from PIL import Image as pilImage

author = ‘JayChen’

class ProcessPDF:
def init(self, pdf_path, pdf_name, pdf_out_path, pdf_out_name, seal,
clarity=1.34):
self.pdf_path = pdf_path
self.pdf_name = pdf_name
self.pdf_out_path = pdf_out_path
self.pdf_out_name = pdf_out_name
self.clarity = clarity
self.pdf = r"D:\2-python\8-河源\90、PDF添加公章\temp\宗地图.pdf"
self.pdf_out = r"D:\2-python\8-河源\90、PDF添加公章\temp\宗地图1.pdf"
self.seal = seal
def merge_img(self): """ 图片叠加 """ img1 = pilImage.open(self.img_path) # PDF图片
seal_img = pilImage.open(self.seal) # 公司印章图片 layer = pilImage.new('RGBA',
img1.size, (0, 0, 0, 0)) print(img1.size[0] - 200, img1.size[1] - 250)
layer.paste(seal_img, (0, 500)) out = pilImage.composite(layer, img1, layer)
self.merge_path = os.path.join(self.pdf_path, 'out.png')
out.save(self.merge_path) def pdf_to_img(self): """ PDF转化为图片 """ doc =
fitz.open(self.pdf) for pg in range(doc.pageCount): page = doc[pg] rotate =
int(0) zoom_x = self.clarity zoom_y = self.clarity trans = fitz.Matrix(zoom_x,
zoom_y).preRotate(rotate) pm = page.getPixmap(matrix=trans, alpha=False) path =
r"D:\2-python\8-河源\90、PDF添加公章\1.png" pm.writePNG(path) self.img_path =
r"D:\2-python\8-河源\90、PDF添加公章\1.png" def img_to_pdf(self): """ 图片转化为PDF """ doc
= fitz.open() imgdoc = fitz.open(self.merge_path) pdfbytes =
imgdoc.convertToPDF() # 使用图片创建单页的 PDF imgpdf = fitz.open("pdf", pdfbytes)
doc.insertPDF(imgpdf) # 将当前页插入文档 doc.save(self.pdf_out) # 保存pdf文件 doc.close()
def out(self): self.pdf_to_img() # PDF转化为图片 self.merge_img() # 合并图片
self.img_to_pdf() # 图片转化为PDF
def run():
“”"
pdf_path: PDF文件的路径
pdf_name:PDF文件名
pdf_out_path:PDF输出路径
pdf_out_name:PDF输出文件名
seal: 公章路径
clarity:可选参数,可以调整pdf清晰度,默认1.34,数值越大,清晰度越高
“”"
pdf_path = r"D:\2-python\8-河源\90、PDF添加公章\temp"
pdf_name = ‘宗地图.pdf’
pdf_out_path = r"D:\2-python\8-河源\90、PDF添加公章\temp\宗地图1.pdf"
pdf_out_name = ‘out.pdf’
seal = r"D:\2-python\8-河源\90、PDF添加公章\aaa.png"
pdf = ProcessPDF(pdf_path=pdf_path,
pdf_name=pdf_name,
pdf_out_path=pdf_out_path,
pdf_out_name=pdf_out_name,
seal=seal,
clarity=1.34)
pdf.out()

if name == ‘main’:
run()

技术
今日推荐
PPT
阅读数 109
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信