审计文件标识作为水印打印在pdf页面边角

目录

说明

将审计文件的所需要贴的编码直接作为水印贴在页面四个角落,节省辨别时间

python 复制代码
我曾经写过一个给pdf页面四个角落加上文件名水印的python脚本,现在需要加一个图形界面进一步加强其实用性。首先通过路径浏览指定文件路径,先检测该路径是不是已经存在的文件夹,如果不是,再判断是不是txt如果也不是,提示需要txt路径列表或者提供根路径,如果是文件路径,提示该路径下子文件夹的文件也会被做同样处理,做好文件备份隔离防护工作。如果是txt则按行读取,并且提示其中的路径有多少是有效的。
提供一个勾选框,决定是否要将路径中的各种图片格式转化为同名pdf。然后提供两个文本框指定图片转化线程数和水印添加线程数
然后再提供2个参数文本框,第一个文本框内数字为g(成为打印页边距)分别用来调节水印离两个方向页面边界的距离(以%为单位的相对距离,实际距离取决于读取到的页面尺寸,长和宽的乘积取平方根再乘以g%),第2文本框用来指定相对字体大小f%(%为单位,计算一个参考高度等于页面长宽乘积取平方根再乘以5%再乘以f%,然后计算选定字体显示高度与参考高度相等的字号),用标签提示最好在实验文件夹中测试好需要的相对字体大小
font_family根据系统可选提供下拉菜单,改为用户指定,前两个默认为Times New Roman和楷体
水印内容和示例代码中一样根据文件名、当前页数和总页数来确定
由于插入点是文本左上角,而四个角文本的方向不同,为了让文本更好贴合打印边界又不超出打印边界,该代码根据页面尺寸、打印边距和指定字体字号情况下文本显示所占矩形空间来确定插入点的具体位置
点击执行按钮,遍历一次路径如果有图片需要转化又未被转化,先转化图片为pdf(多线程执行),同时记录所有有待处理的pdf文件数(包括图片转化出来的)
再遍历一次路径,将每个pdf的按照设定参数添加水印(用多线程执行),并且替换原文件。按已处理文件数/总文件数显示进度条,完成后弹窗提示

import os
import re
import fitz
from PIL import ImageFont
def text_position(w,h,x,y,width,height,gap):
    if y<=height/2:
        if x<=width/2:
            x=max(x,gap)+h
            y=gap+w
            return [x,y]
        else:
            x=width-gap-w
            y=max(gap,y)+h
            return [x,y]
    else:
        if x<=width/2:
            x=gap+w
            y=height-max(gap,height-y)-h
            return [x,y]
        else:
            x=width-max(gap,width-x)-h
            y=height-gap-w
            return [x,y]
def text_size(line,font_family,font_size):
    font = ImageFont.truetype(font_family, font_size, 0)
    width, height = font.getsize(line)
    #DeprecationWarning: getsize is deprecated and will be removed in Pillow 10 (2023-07-01). Use getbbox or getlength instead.
    return [width,height]
def text_insert_once(x1,y1,x2,y2,x3,y3,x4,y4,text,fname,fsize,page):
    p = fitz.Point(x2,y2)#右上角
    page.insert_text(p,  # bottom-left of 1st char
                         text,  # the text (honors '\n')
                         fontname = fname,  # the default font
                         fontsize = fsize,  # the default font size
                         rotate = 0,  # also available: 90, 180, 270
                         )
    p = fitz.Point(x3,y3)#左下角,从右往左
    page.insert_text(p,  # bottom-left of 1st char
                         text,  # the text (honors '\n')
                         fontname = fname,  # the default font
                         fontsize = fsize,  # the default font size
                         rotate = 180,  # also available: 90, 180, 270
                         )
    p = fitz.Point(x1,y1)#左上角,从下到上
    page.insert_text(p,  # bottom-left of 1st char
                         text,  # the text (honors '\n')
                         fontname = fname,  # the default font
                         fontsize = fsize,  # the default font size
                         rotate = 90,  # also available: 90, 180, 270
                         )
    p = fitz.Point(x4,y4)#右下角,从上到下
    page.insert_text(p,  # bottom-left of 1st char
                         text,  # the text (honors '\n')
                         fontname = fname,  # the default font
                         fontsize = fsize,  # the default font size
                         rotate = 270,  # also available: 90, 180, 270
                         )
def pnum_print(pdf,file,flag=1):
    pagenum=pdf.page_count    
    i=0
    for page in pdf:
        content=[]
        #假定短边留白5%,长边留白3.3%
        i=i+1
        width=page.rect.width
        height=page.rect.height
        [wx,hx]=[0.06,0.04]
        if width>= height:
            #w=round(width*wx,0)
            h=round(height*wx,0)
            w=h
        else:
            w=round(width*wx,0)
            h=w
        fs=int(w/4)
        text=str(i)+'/'+str(pagenum)
        content.append(os.path.splitext(file)[0])
        content.append(text)       
        ff=page.insert_font(fontname="HT",fontfile=r"C:\Windows\Fonts\simhei.ttf", fontbuffer=None , set_simple=False )
        [x1,y1,x2,y2,x3,y3,x4,y4]=[0,0,width,0,0,height,width,height]
        for c in content:
            [w,h]=text_size(c,"simhei.ttf",fs)
            [x1,y1]=text_position(w,h,x1,y1,width,height,0*w)
            [x2,y2]=text_position(w,h,x2,y2,width,height,0*w)
            [x3,y3]=text_position(w,h,x3,y3,width,height,0*w)
            [x4,y4]=text_position(w,h,x4,y4,width,height,0*w)
            text_insert_once(x1,y1,x2,y2,x3,y3,x4,y4,c,"HT",fs,page)
    if pdf.can_save_incrementally():
        if flag==1:
            pdf.saveIncr()
        else:
            pdf.save(os.path.splitext(file)[0]+'(共'+str(pagenum)+'页)'+'.pdf')
        print(file+"***********processed")
        pdf.close()
        if flag!=1:
            os.remove(file)
        #os.remove(file)
    else:
        print("Can't save Incermentally")#增量保存的文件损坏和签名问题
def path_read(flag,source):
    path=[]
    if flag=="父节点":
        for p in os.listdir(source):
            if os.path.isdir(source+'\\'+p):
                path.append(source+'\\'+p)
        return path
    if flag=="列表文件":
        with open(source,'r') as f :
            for p in f.readlines():
                pp=p.replace('\n','')
                if os.path.isdir(pp):
                    path.append(pp)
        return path
    return None
def pic2pdf(file):
    img_file=['.png','.jpg',',jepg']
    title=os.path.splitext(file)[0]
    if os.path.splitext(file)[1] in img_file:
        imgdoc = fitz.open(file) # 打开图片
        pdfbytes = imgdoc.convert_to_pdf() # 使用图片创建单页的 PDF
        imgpdf = fitz.open("pdf", pdfbytes)
        doc=fitz.open()
        doc.insert_pdf(imgpdf) # 将当前页插入文档
        if os.path.exists(title+".pdf"):
            os.remove(title+".pdf")
        doc.save(title+".pdf") # 保存pdf文件
        doc.close()
        imgdoc.close()
        os.remove(file)
img_file=['.png','.jpg',',jepg']
img_convert=True #False  #True
#path=path_read(flag="列表文件",source=r'E:\huang\Desktop\路径列表.txt')
path=path_read(flag="父节点",source=r'E:\huang\Desktop\浙江通力传动科技股份有限公司\乐总底稿整理\新建文件夹\内控')
print(path)
for p in path:
    os.chdir(p)
    print(p)
    if img_convert:
        for file in os.listdir():
            if os.path.splitext(file)[1] in img_file:
                pic2pdf(file)
    for file in os.listdir():
        if os.path.splitext(file)[1]=='.pdf':
            print(file)
            if re.search(r'\(共\d+?页\)',file)==None:
                pdf_book=fitz.open(file)
                pnum_print(pdf_book,file,0)
                    
相关推荐
界面开发小八哥几秒前
「Java EE开发指南」如何使用MyEclipse的可视化JSF编辑器设计JSP?(二)
java·ide·人工智能·java-ee·myeclipse
森叶10 分钟前
Electron 主进程中使用Worker来创建不同间隔的定时器实现过程
前端·javascript·electron
霸王蟹19 分钟前
React 19 中的useRef得到了进一步加强。
前端·javascript·笔记·学习·react.js·ts
霸王蟹19 分钟前
React 19版本refs也支持清理函数了。
前端·javascript·笔记·react.js·前端框架·ts
繁依Fanyi24 分钟前
ColorAid —— 一个面向设计师的色盲模拟工具开发记
开发语言·前端·vue.js·编辑器·codebuddy首席试玩官
孙胜完不了30 分钟前
Day29
python
lkx0978830 分钟前
第四天的尝试
python
lcccyyy11 小时前
day 29
python
明似水1 小时前
Flutter 开发入门:从一个简单的计数器应用开始
前端·javascript·flutter
沐土Arvin1 小时前
前端图片上传组件实战:从动态销毁Input到全屏预览的全功能实现
开发语言·前端·javascript