2008发布asp网站,国外网站要备案吗,网站设计有哪些,做婚庆网站的想法目录 说明 说明
将审计文件的所需要贴的编码直接作为水印贴在页面四个角落#xff0c;节省辨别时间
我曾经写过一个给pdf页面四个角落加上文件名水印的python脚本#xff0c;现在需要加一个图形界面进一步加强其实用性。首先通过路径浏览指定文件路径#xff0c;先检测该路… 目录 说明 说明
将审计文件的所需要贴的编码直接作为水印贴在页面四个角落节省辨别时间
我曾经写过一个给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 yheight/2:if xwidth/2:xmax(x,gap)hygapwreturn [x,y]else:xwidth-gap-wymax(gap,y)hreturn [x,y]else:if xwidth/2:xgapwyheight-max(gap,height-y)-hreturn [x,y]else:xwidth-max(gap,width-x)-hyheight-gap-wreturn [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 chartext, # the text (honors \n)fontname fname, # the default fontfontsize fsize, # the default font sizerotate 0, # also available: 90, 180, 270)p fitz.Point(x3,y3)#左下角从右往左page.insert_text(p, # bottom-left of 1st chartext, # the text (honors \n)fontname fname, # the default fontfontsize fsize, # the default font sizerotate 180, # also available: 90, 180, 270)p fitz.Point(x1,y1)#左上角从下到上page.insert_text(p, # bottom-left of 1st chartext, # the text (honors \n)fontname fname, # the default fontfontsize fsize, # the default font sizerotate 90, # also available: 90, 180, 270)p fitz.Point(x4,y4)#右下角从上到下page.insert_text(p, # bottom-left of 1st chartext, # the text (honors \n)fontname fname, # the default fontfontsize fsize, # the default font sizerotate 270, # also available: 90, 180, 270)
def pnum_print(pdf,file,flag1):pagenumpdf.page_count i0for page in pdf:content[]#假定短边留白5%长边留白3.3%ii1widthpage.rect.widthheightpage.rect.height[wx,hx][0.06,0.04]if width height:#wround(width*wx,0)hround(height*wx,0)whelse:wround(width*wx,0)hwfsint(w/4)textstr(i)/str(pagenum)content.append(os.path.splitext(file)[0])content.append(text) ffpage.insert_font(fontnameHT,fontfilerC:\Windows\Fonts\simhei.ttf, fontbufferNone , set_simpleFalse )[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 flag1: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(Cant 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 pathif flag列表文件:with open(source,r) as f :for p in f.readlines():ppp.replace(\n,)if os.path.isdir(pp):path.append(pp)return pathreturn None
def pic2pdf(file):img_file[.png,.jpg,,jepg]titleos.path.splitext(file)[0]if os.path.splitext(file)[1] in img_file:imgdoc fitz.open(file) # 打开图片pdfbytes imgdoc.convert_to_pdf() # 使用图片创建单页的 PDFimgpdf fitz.open(pdf, pdfbytes)docfitz.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_convertTrue #False #True
#pathpath_read(flag列表文件,sourcerE:\huang\Desktop\路径列表.txt)
pathpath_read(flag父节点,sourcerE:\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_bookfitz.open(file)pnum_print(pdf_book,file,0)