工作需要,加个转单层PDF的功能
#! C:\ProgramData\Miniconda3\
#!/usr/bin/python3
# -*- encoding: utf-8 -*-
'''
@File : FJ_Main.py
@Time : 2020/07/18 08:24:06
@Author : GonerY
@Version : 1.0
@Contact : Gonery@qq.com
@WebSite : https://blog.csdn.net/gonery/
'''
# Start typing your code from here
import tkinter as tk
import tkinter.filedialog
import xlrd
import os
import shutil
import fitz
import glob
from tkinter import END
from tkinter import scrolledtext
def get_path(): # 输入框获取目录路径
text = tkinter.filedialog.askdirectory(title='选择目录')
txt_info.delete(1.0, END)
txt_info.update()
txt_path.delete(0, END)
txt_path.insert(0, text)
txt_path.update()
def search_xlsx(path): # 查找excel文件,有返回选择的目录路径,无返回False
juan = "案卷级目录数据库.xlsx"
jian = "卷内级目录数据库.xlsx"
for root, dirs, files in os.walk(path):
if juan in files and jian in files:
return root
else:
return False
def check_data(path): # 录入数量与文件数量检查
xr = xlrd.open_workbook(path + "/案卷级目录数据库.xlsx",
on_demand=True) # 打开 案卷级目录数据库.xlsx
sheet1 = xr.sheet_by_name("案卷级目录数据库") # 获取sheet
i = 1
check_err = "no"
u_list = []
# 卷页数、图片数量检验
while i < sheet1.nrows: # 依次读取行信息
rowlist = sheet1.row_values(i)
ys = int(rowlist[8])
dh = str(rowlist[1])
u_tup = (dh, ys) # 案卷 档号、页数 数据存入元组
u_list.append(u_tup) # 案卷 档号、页数 数据添加到列表
j = 0
for files in os.listdir(path + '/' + dh):
j += 1
if j != ys:
check_err = "yes"
txt_info.insert(
"end", dh + " 卷级页数:" + str(ys) + " 图片页数:" + str(j) + "\n")
txt_info.update()
txt_info.see(END)
i += 1
txt_info.insert("end", dh + "\n")
txt_info.update()
txt_info.see(END)
xr.release_resources()
# 读取件级 档号、页数 数据
xr = xlrd.open_workbook(path + "/卷内级目录数据库.xlsx", on_demand=True)
sheet1 = xr.sheet_by_name("卷内级目录数据库")
i = 1
j_list = []
while i < sheet1.nrows:
rowlist = sheet1.row_values(i)
ys = rowlist[3]
dh = rowlist[8] + '-' + rowlist[9] + '-' + rowlist[10]
j_tup = (dh, ys)
j_list.append(j_tup)
i += 1
xr.release_resources()
# 卷、件页数检验
for u in u_list: # 遍历卷数据
y = 0 # 初始化 件页数
for j in j_list: # 遍历件数据
if j[0] == u[0]: # 档号相同,页数累计
y += int(j[1])
if y != u[1]: # 页数不相等,输出错误信息
check_err = "yes"
txt_info.insert(
"end",
u[0] + " 卷级页数:" + str(u[1]) + " 件级页数:" + str(y) + "\n")
txt_info.update()
txt_info.see(END)
if check_err == "yes":
txt_info.insert("end", "页数数据不相符,请检查!\n")
txt_info.update()
txt_info.see(END)
return check_err
else:
return check_err
def fj(path): # 分件
xr = xlrd.open_workbook(path + "/卷内级目录数据库.xlsx", on_demand=True)
sheet1 = xr.sheet_by_name("卷内级目录数据库")
i = 1
while i < sheet1.nrows:
rowlist = sheet1.row_values(i)
dh = rowlist[8] + '-' + rowlist[9] + '-' + rowlist[10]
ys = rowlist[3]
lh = rowlist[7]
JuanPath = path + '/' + dh + '/'
JianPath = path + '/JPG/' + dh + '/' + dh + '-' + lh + '/'
# 根据分类号创建文件夹
if os.path.exists(JianPath):
# 异常提示
txt_info.insert("end", dh + "-" + lh + "\n该目录已存在,请检查!\n")
txt_info.update()
txt_info.see(END)
return
else:
os.makedirs(JianPath)
i += 1
j = 1
while j <= int(ys):
for picfiles in os.listdir(JuanPath):
# dnp: dir and pic 图片完全路径
dnp = os.path.join(JuanPath, picfiles)
# nn: newname 图片重命名,按0001,0002......
nn = str(10000 + int(j))[-4:]
shutil.move(dnp, JianPath + nn + '.jpg')
j += 1
# 移动一个图片文件后终止FOR循环,再移动下一张
break
txt_info.insert("end", dh + '-' + lh + '\n')
txt_info.update()
txt_info.see(END)
xr.release_resources()
txt_info.insert("end", "\n----------分件完成!----------\n")
txt_info.update()
txt_info.see(END)
def delpath(path): # 删除空目录
for root, dirs, files in os.walk(path):
if not os.listdir(root):
os.rmdir(root)
def pic2pdf(path): # 转PDF
jpg_path = path + "/JPG/"
for u_dirs in os.listdir(jpg_path): # 案卷级目录 u_dirs
for i_dirs in os.listdir(jpg_path + u_dirs): # 卷内级目录 i_dirs
txt_info.insert("end",i_dirs + '\n')
txt_info.update()
txt_info.see(END)
doc = fitz.open()
for img in sorted(glob.glob(jpg_path + u_dirs + "/" + i_dirs + "/*.jpg")):
imgdoc = fitz.open(img)
pdfbytes = imgdoc.convertToPDF()
imgpdf = fitz.open("pdf", pdfbytes)
doc.insertPDF(imgpdf)
if os.path.exists("allimages.pdf"):
os.remove("allimages.pdf")
if os.path.exists(path + "/PDF/" + u_dirs):
pass
else:
os.makedirs(path + "/PDF/" + u_dirs)
doc.save(path + "/PDF/" + u_dirs + "/" + i_dirs + ".pdf")
doc.close()
txt_info.insert("end", "\n----------PDF转换完成!----------\n")
txt_info.update()
txt_info.see(END)
'''
def click_cs(): # 点击 测试 按钮
fj_path = txt_path.get()
pic2pdf(fj_path)
'''
def click_fj(): # 点击 分件 按钮
txt_info.delete(1.0, END)
fj_path = txt_path.get()
if fj_path == "": # 判断有无选择目录
return
else:
xlsx = search_xlsx(fj_path)
if not xlsx: # 判断目录下有无Excel文件
txt_info.insert("end", "目录下无对应的Excel文件!\n")
txt_info.update()
txt_info.see(END)
return
txt_info.insert("end", "正在检验数据,请稍候…………\n")
txt_info.update()
txt_info.see(END)
if check_data(xlsx) == "yes":
return
txt_info.insert("end", "数据检验通过,正在分件,请稍候…………\n")
txt_info.update()
txt_info.see(END)
fj(fj_path)
delpath(fj_path)
txt_info.insert("end", "\n正在转PDF,请稍候…………\n")
txt_info.update()
txt_info.see(END)
pic2pdf(fj_path)
# 主窗体
window = tk.Tk()
window.title('分件')
window.geometry('450x400+300+50')
window.resizable(0, 0)
# 显示提示文本
tk.Label(
window,
text='请将“案卷级目录数据库.xlsx”及“卷内级目录数据库.xlsx”放在分件目录内',
font=('宋体', 9),
).place(x=20, y=10)
# 选择目录 按钮
btn_choose = tk.Button(window, text='选择目录:', command=get_path)
btn_choose.place(x=20, y=40)
# 文本输入框
txt_path = tk.Entry(window, font=('宋体', 9), width=55)
txt_path.place(x=95, y=45)
# 分件 按钮
btn_fj = tk.Button(window, text='分件&转PDF', width=10, command=click_fj)
btn_fj.place(x=190, y=75)
'''
# 测试 按钮
btn_cs = tk.Button(window, text='TEST', width=10, command=click_cs)
btn_cs.place(x=20, y=75)
'''
# 信息显示框
txt_info = scrolledtext.ScrolledText(window,
font=('宋体', 9),
width=68,
height=21)
txt_info.place(x=20, y=120)
# 处理过程显示
lbl_info = tk.Label(window, font=('宋体', 9))
lbl_info.place(x=20, y=380)
# 主窗体循环
window.mainloop()